我有一个工作脚本可以复制(通过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 + "-------------------------------------------------------------------------------------")
答案 0 :(得分:0)
只是answered类似的问题。你应该先使用搜索。
但简而言之:为每个Copy-Item运行Start-Job
,然后使用Wait-Job
等待副本完成,Receive-Job
收集结果(如果您的复制任务返回结果) )。
$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