【发布时间】:2017-08-21 16:03:46
【问题描述】:
我目前正在编写一个简单的 PowerShell 脚本。 基本上,它应该从记事本中获取服务器列表并开始解压缩每个服务器上的 .zip 文件并将其解压缩到新文件夹中。
但是,该脚本并未提取 zip 文件下的所有文件。 它只会从中提取一个文件,我不确定为什么 foreach 循环不能正常工作。
请对这个问题有所了解。谢谢。
$servers = Get-Content "C:\tmp\script\new_unzip\servers.txt"
$Date = ((Get-Date).ToString('dd-MM-yyyy_HH-mm-ss'))
foreach ($server in $servers) {
$shell = new-object -com shell.application
$target_path = "\\$server\c$\Temp\FFPLUS_Temp"
$location = $shell.namespace($target_path)
$ZipFiles = Get-ChildItem -Path $target_path -Filter *.zip
$ZipFiles | Unblock-File
foreach ($ZipFile in $ZipFiles) {
$ZipFile.fullname | out-default
$NewLocation = "\\$server\c$\Temp\FFPLUS_Temp\$Date"
New-Item $NewLocation -type Directory -Force -ErrorAction SilentlyContinue
Move-Item $ZipFile.fullname $NewLocation -Force -ErrorAction SilentlyContinue
$NewZipFile = Get-ChildItem $NewLocation *.zip
$NewLocation = $shell.namespace($NewLocation)
$ZipFolder = $shell.namespace($NewZipFile.fullname)
$NewLocation.copyhere($ZipFolder.items())
}
}
【问题讨论】:
-
我正在尝试在多个远程服务器上提取文件,但它只解压缩了 3 个文件中的一个文件。我没有收到任何错误,但不知何故它不正确。
-
我正在使用 .NET Framework 4.5 中包含的 ExtractToDirectory 方法作为替代方法。
-
@MuthukumaranThavarajoo 我也是,它也有同样的问题。它在本地运行良好,但在我尝试解压缩的服务器上运行
Invoke-Command时无法复制所有文件。 -
@Matt Arnold,我用另一种方式做了,它可以工作
标签: powershell unzip