2017-02-13 4 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

[I가 임시 사용 PowerShell에서 특정 디렉토리에있는 모든 .ZIP 파일을 추출 할]의 사용 가능한 복제 (http://stackoverflow.com/questions/28448202/i-want-to-extract -all-zip-files-in-a-given-directory-in-temp-using-powershell) – 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

여러 서버에서 동일한 파일의 압축을 해제하는 다른 방법입니다. –