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:\Temp\appone\ROOT.war
C:\Temp\appone\DEV\deployfiles\file1.xls
C:\Temp\appone\UAT\deployfiles\file1.xls
C:\Temp\appone\deployfiles\DEV\file2.xls
C:\Temp\appone\deployfiles\UAT\file2.xls
Powershell:
$workspace = "C:\Temp\appone"
$zipFileName = "$workspace\yello.zip"
$tempZipFileName = "$workspace\temp.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
}
The above creates temp.zip recursively and never completes execution. See output below:
Kindly suggest.
