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


i have working script copy (via smb) few files different locations. unfortunately, taking long since copying 1 file after other.

i need re-work script use jobs , transfer files in parallel.

i'm relatively new powershell. how can this?

the following current script:

$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 + "-------------------------------------------------------------------------------------") 

just answered similar question. should use search first.

but in brief: run start-job each of copy-item, use wait-job wait copy complete , receive-job gather results (if copy task returns result).

$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 custom logging"}  copy-item "$source\wintsziparchiv.zip" $destination -passthru -erroraction silentlycontinue  if(-not $?) {#write-warning "replace 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 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -