如何使用 PowerShell 5.0 Compress-Archive
cmdlet 以递归方式获取目录中的任何 .config 文件并将其压缩,同时保持目录结构。例子:
Directory1
Config1.config
Directory2
Config2.config
目标是一个包含上述目录结构和仅配置文件的单个 zip 文件。
如何使用 PowerShell 5.0 Compress-Archive
cmdlet 以递归方式获取目录中的任何 .config 文件并将其压缩,同时保持目录结构。例子:
Directory1
Config1.config
Directory2
Config2.config
目标是一个包含上述目录结构和仅配置文件的单个 zip 文件。
我建议将文件复制到一个临时目录并压缩它。前任:
$path = "test"
$filter = "*.config"
#To support both absolute and relative paths..
$pathitem = Get-Item -Path $path
#If sourcepath exists
if($pathitem) {
#Get name for tempfolder
$tempdir = Join-Path $env:temp "CompressArchiveTemp"
#Create temp-folder
New-Item -Path $tempdir -ItemType Directory -Force | Out-Null
#Copy files
Copy-Item -Path $pathitem.FullName -Destination $tempdir -Filter $filter -Recurse
#Get items inside "rootfolder" to avoid that the rootfolde "test" is included.
$sources = Get-ChildItem -Path (Join-Path $tempdir $pathitem.Name) | Select-Object -ExpandProperty FullName
#Create zip from tempfolder
Compress-Archive -Path $sources -DestinationPath config-files.zip
#Remove temp-folder
Remove-Item -Path $tempdir -Force -Recurse
}