簡體   English   中英

通過Powershell在多個遠程服務器上解壓縮文件

[英]Unzip a file on multiple remote servers via Powershell

我當前正在編寫一個簡單的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())
    }
} 
$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        
    }           

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM