简体   繁体   中英

Unzip a file on multiple remote servers via Powershell

I'm currently writing a simple PowerShell script. Basically, it should get the list of servers from a notepad and start to unzip the .zip file on each server and extract it to the new folder.

However, the script is not extracting all files under the zip file. It would only extract one file from it and I'm not sure why the foreach loop not working properly.

Please shed some light on this issue. Thanks.

$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        
    }           

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM