需要重新编写脚本以使用作业进行并行传输

Need to re-work script to use jobs for parallel transfer

我有一个工作脚本可以(通过 SMB)从不同位置复制一些文件。不幸的是,这花费了太长时间,因为它是一个接一个地复制文件。

我需要重新编写此脚本以使用作业并并行传输所有文件。

我对 PowerShell 比较陌生。我该怎么做?

以下是我目前的脚本:

$DATA = Import-Csv -Path F:\TDB_Server.csv -Delimiter ";"
$LOGTIME = Get-Date -Format "yyyy-MM-dd_HH:mm:ss"

   Function LogWrite  {
        Param ([string]$logstring)
        Add-content $LOG -value $logstring
    }

LogWrite ($LOGTIME + "-------------------------------------------------------------------------------------")

    $copyfiles = [scriptblock] {
        Param ($d)
        $DOW = [int] (get-date).DayOfWeek
        $DATE = (Get-Date -format "yyyy-MM-dd")
        $source = "\$($d.SERVER)\f$\TDB\ZIP_SICH\ZIP$DOW"
        $dest = "F:\system2$($d.TDB)"
        $LOG = "F:\LOGs$DATE.log"

        Copy-Item "$source\datum.txt" $destination -PassThru -errorAction SilentlyContinue
        if(-not $?) {LogWrite ($LOGTIME + " - **************** !! Exception!! ********************"}

        Copy-Item "$source\hash_org.txt" $destination -PassThru -errorAction SilentlyContinue
        if(-not $?) {LogWrite ($LOGTIME + " - **************** !! Exception!! ********************"}

        Copy-Item "$source\WinTSzipArchiv.zip" $destination -PassThru -errorAction SilentlyContinue
        if(-not $?) {LogWrite ($LOGTIME + " - **************** !! Exception!! ********************"}

        #alte Daten auf FTP pro NL löschen
        Remove-Item F:\system2$TDB\* -Force 
        $LOGTIME = Get-Date -Format "yyyy-MM-dd_HH:mm:ss"
        LogWrite ($LOGTIME + " - alte Dateien vom FTP Server gelöscht")

        #Erzeuge datum.txt auf dem NT Server der Niederlassungen
        (get-item $DIR\WinTSzipArchiv.zip).lastWriteTime.ToString() | out-file -encoding ASCII $DIR/datum.txt
        $LOGTIME = Get-Date -Format "yyyy-MM-dd_HH:mm:ss"
        LogWrite ($LOGTIME + " - datum.txt erzeugt auf $TDB / $SERVER")

        # Hash-Werte der Kopierten Datei (WinTSzipArchiv.zip) überprüfen
        # Zuerst generieren wir einen Hash-Wert für die neue kopierte Datei
        $file = "F:\system2$TDB\WinTSzipArchiv.zip"
        & "C:\Windows\fciv.exe" $file > F:\system2$TDB\hash_copy.txt

        # Hash-Dateien anpassen und Hash-Werte zum vergleich auslesen
        # hash_org.txt
        $file = "F:\system2$TDB\hash_org.txt"
        (Get-Content $file | Select-Object -Skip 3) | Set-Content $file
        (([char[]](Get-Content $file -Encoding byte -TotalCount 32)) -join '') | Set-Content $file

        $hash_org = [IO.File]::ReadAllText("F:\system2$TDB\hash_org.txt")

        # hash_copy.txt
        $file = "F:\system2$TDB\hash_copy.txt"
        (Get-Content $file | Select-Object -Skip 3) | Set-Content $file
        (([char[]](Get-Content $file -Encoding byte -TotalCount 32)) -join '') | Set-Content $file

        $hash_copy = [IO.File]::ReadAllText("F:\system2$TDB\hash_copy.txt")

        # Schreiben der Hash-Werte ins Log    
        $LOGTIME = Get-Date -Format "yyyy-MM-dd_HH:mm:ss"
        LogWrite ($LOGTIME + " - Hash_org : $hash_org")
        LogWrite ($LOGTIME + " - Hash_copy: $hash_copy")
    }

    $copyjobs = @()

    ForEach ($d in $DATA) {
        $copyjobs += start-job -scriptblock $copyfiles -ArgumentList $d
    }

    wait-job $copyjobs

    Send-MailMessage -To "mon_tdb@halltabakwaren.de" -Subject "TDB FTP" -From "TDB@halltabakwaren.de" -Body "TDB_Sicherung ist fertig, Logdatei im Anhang" -SmtpServer "exchange02.hall.intern" -Attachments $LOG
    $LOGTIME = Get-Date -Format "yyyy-MM-dd_HH:mm:ss"
    LogWrite ($LOGTIME + " - E-Mail wurde verschickt - ENDE -")
    LogWrite ($LOGTIME + "-------------------------------------------------------------------------------------")

只是 answered 类似的问题。您应该先使用搜索。

但简而言之:运行 Start-Job 用于每个 Copy-Item,然后使用 Wait-Job 等待复制完成并 Receive-Job 收集结果(如果你的复制任务 returns 结果)。

$copyfiles = [scriptblock] {
  Param ($d)
 $DOW = [int] (get-date).DayOfWeek 
 $DATE = (Get-Date -format "yyyy-MM-dd")
 $source = "\$($d.SERVER)\f$\TDB\ZIP_SICH\ZIP$DOW"
 $dest = "F:\system2$($d.TDB)"

 Copy-Item "$source\datum.txt" $destination -PassThru -errorAction SilentlyContinue
 if(-not $?) {#write-warning "Replace this with your custom logging"}
 Copy-Item "$source\WinTSzipArchiv.zip" $destination -PassThru -errorAction SilentlyContinue
 if(-not $?) {#write-warning "Replace this with your custom logging"}
 # other files ......
}

$DATA = Import-Csv -Path F:\TDB_Server.csv -Delimiter ";"
$copyjobs = @()

ForEach ($d in $DATA) {
  $copyjobs += start-job -scriptblock $copyfiles -ArgumentList $d
}
wait-job $copyjobs