I require to create powershell script for yello.zip archive by searching inside $workspace and including comma separate files mentioned in $itemsToInclude

There should be a single copy of all files but in case of the deployfiles folder it should include multiple files having the same filenames inside the deployfiles folder.

Thus, the following files are expected to be seen in the archive.

C:TempapponeROOT.war
C:TempapponeDEVdeployfilesfile1.xls
C:TempapponeUATdeployfilesfile1.xls
C:TempapponedeployfilesDEVfile2.xls
C:TempapponedeployfilesUATfile2.xls

Powershell:

$workspace = "C:Tempappone"

$zipFileName = "$workspaceyello.zip"

$tempZipFileName = "$workspacetemp.zip"

cd $workspace

$itemsToInclude = "ROOT.war,deployfiles"

Write-Host "itemsToInclude is- $itemsToInclude"

 

if ($itemsToInclude -eq '*') {

    # Include all files, including files from subdirectories

    Write-Host "Include all files, including files from subdirectories"

    Get-ChildItem -Path $workspace -Recurse -File |

        Compress-Archive -DestinationPath $tempZipFileName -Force

} else {

    # Include specific files as per the comma-separated list

    Write-Host "Include specific files as per the comma-separated list"

    $pattern = $itemsToInclude.Split(',').ForEach({ [regex]::Escape($_) }) -join '|'

    Get-ChildItem $workspace -Recurse -File |

        Where-Object FullName -Match $pattern |

        ForEach-Object {

            $relativePath = $_.FullName.Replace($workspace, "")

            $relativePath = $relativePath -replace "^\", "" # Remove leading backslash

            $destinationPath = Join-Path -Path $tempZipFileName -ChildPath $relativePath

            $destinationFolder = Split-Path -Path $destinationPath

            if (-not (Test-Path -Path $destinationFolder)) {

                New-Item -ItemType Directory -Path $destinationFolder | Out-Null

            }

            Copy-Item -Path $_.FullName -Destination $destinationPath -Force

        }

    Compress-Archive -Path $tempZipFileName -DestinationPath $zipFileName -Update

}

Kindly suggest.