2017-02-13 67 views
0

我目前正在編寫一個簡單的PowerShell腳本。 基本上,它應該從記事本中獲取服務器列表,並開始解壓每臺服務器上的.zip文件,並將其解壓縮到新文件夾中。通過Powershell在多個遠程服務器上解壓文件

但是,該腳本並未提取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()) 
    } 
} 
+0

的[我要提取臨時使用PowerShell在一個給定的目錄中的所有.zip文件]可能的複製(http://stackoverflow.com/questions/28448202/i-want-to-extract -all-zip-files-in-a-given-directory-in-temp-using-powers) – BenH

+0

我試圖在多個遠程服務器上解壓文件,但它只解壓出3個文件中的一個文件。我沒有收到任何錯誤,但不知怎的,它不正確。 –

+0

我正在使用.NET Framework 4.5中包含的ExtractToDirectory方法作爲替代方法。 –

回答

0
$servers = Get-Content "C:\tmp\script\updated\servers.txt" 
$Date = ((Get-Date).ToString('dd-MM-yyyy_HH-mm-ss')) 

foreach ($server in $servers) 
{ 

$zipFolder = "\\$server\c$\Temp\FFPLUS_Temp" 

Add-Type -assembly System.IO.Compression.Filesystem 

$zipFiles = Get-ChildItem -Path $zipFolder -Filter *.zip 

foreach($zip in $zipFiles) 
    { 
     $destPath = "\\$server\c$\Temp\FFPLUS_Temp\$Date"  

     New-Item -ItemType Directory $destPath 

     [io.compression.zipfile]::ExtractToDirectory([string]$zip.FullName, "$destPath") 

     Move-Item $zip.fullname $destPath -Force -ErrorAction SilentlyContinue   
    }   

} 
+0

這是在多臺服務器上解壓縮相同文件的替代方法 –

相關問題