Advertisement
kohijones

RoboJobs

Feb 13th, 2014
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##########################################################
  2. #                                                        #
  3. #  Name    : Start-RoboJobs.ps1                          #
  4. #  Author  : kohijones                                   #
  5. #  Created : 7/2/2013                                    #
  6. #  Company : xxx                                         #
  7. #                                                        #
  8. ##########################################################
  9.  
  10. cls;
  11. $JOB = "$($env:COMPUTERNAME)"
  12. $DEST = "yourDestination"
  13. $SHARES =@("test1","test2","test3")
  14. $EMAILLOG = ".\email.log"
  15. $JOBS =@()
  16. $RESULTS=@()
  17.  
  18. foreach ($SHARE in $SHARES) {
  19.     Write-Host "Starting Robocopy Sync for: \\$($JOB)\$($SHARE)"
  20.     $jobs += Start-Job -Name "Copyjob $SHARE" -ArgumentList $JOB,$DEST,$SHARE -ScriptBlock {
  21.         param($JOB,$DEST,$SHARE)
  22.                
  23.         $SOURCE = "\\$($JOB)\$($SHARE)"
  24.         $DESTINATION = "\\$($DEST)\$($JOB)\$($SHARE)"
  25.         $INCLUDEFILES = @("*.*")
  26.         $WHAT = @("/COPYALL")
  27.         $OPTIONS = @("/R:3","/W:1","/E","/V","/NP","/FP","/ZB","/XO","/FFT")
  28.         $DATESTAMP = get-date -uformat "%Y-%m%-%d"
  29.         $DATESTAMPEXT = Get-Date -format "ddd MMM dd HH:mm:ss yyy"
  30.         $checkdir = Test-Path -PathType Container "\\$($DEST)\$($JOB)"
  31.  
  32.         if($checkdir -eq $false){
  33.             New-Item "\\$($DEST)\$($JOB)" -type Directory | Out-Null
  34.         }
  35.        
  36.         $checkLogDir = Test-Path -PathType Container "\\$($DEST)\Logs\$($JOB)\"
  37.  
  38.         if($checkLogDir -eq $false){
  39.             New-Item -path "\\$($DEST)\Logs\$($JOB)\" -ItemType  Directory | Out-Null
  40.         }
  41.  
  42.         $LOGFILE = "\\$($DEST)\Logs\$($JOB)\$($JOB)_$($SHARE)_$($DATESTAMP).log"
  43.         $ROBOCOPYLOG = "/LOG:$LOGFILE"
  44.         $ROBOCOPYEXEC = "C:\Windows\System32\Robocopy.exe"
  45.        
  46.         $cmdArgs = @("$SOURCE","$DESTINATION",$INCLUDEFILES,$WHAT,$ROBOCOPYLOG,$OPTIONS)
  47.        
  48.         $LogMsg = ("-" * 79) + "`r`n$($DATESTAMPEXT) $(${JOB}): Starting robocopy"
  49.         Add-content $LOGFILE $LogMsg -PassThru | out-null
  50.        
  51.         & $ROBOCOPYEXEC @cmdArgs | out-null
  52.        
  53.         $ExitCode = $LastExitCode
  54.        
  55.         $LogMsg = ("-" * 79) + "`r`nSynch finished with exit code: $($exitCode)"
  56.         Add-content $LOGFILE $LogMsg -PassThru | out-null
  57.        
  58.         $MSGType=@{
  59.             "16"="Error"
  60.             "15"="Information"
  61.             "14"="Error"
  62.             "13"="Information"
  63.             "12"="Error"
  64.             "11"="Information"
  65.             "10"="Error"
  66.             "9"="Inforamtion"
  67.             "8"="Error"
  68.             "7"="Information"
  69.             "6"="Information"
  70.             "5"="Information"
  71.             "4"="Warning"
  72.             "3"="Information"
  73.             "2"="Information"
  74.             "1"="Information"
  75.             "0"="Information"
  76.         }
  77.        
  78.         $MSG=@{
  79.             "16"="[ERRR] - Serious error. Robocopy did not copy any files."
  80.             "15"="[INFO] - OKCOPY + FAIL + MISMATCHES + XTRA"
  81.             "14"="[INFO] - FAIL + MISMATCHES + XTRA"
  82.             "13"="[INFO] - OKCOPY + FAIL + MISMATCHES"
  83.             "12"="[INFO] - FAIL + MISMATCHES"
  84.             "11"="[INFO] - OKCOPY + FAIL + XTRA"
  85.             "10"="[INFO] - FAIL + XTRA"
  86.             "9" ="[INFO] - OKCOPY + FAIL"
  87.             "8" ="[ERRR] - Some files or directories could not be copied`r`nCopy errors occurred and the retry limit was exceeded."
  88.             "7" ="[INFO] - OKCOPY + MISMATCHES + XTRA"
  89.             "6" ="[INFO] - MISMATCHES + XTRA"
  90.             "5" ="[INFO] - OKCOPY + MISMATCHES"
  91.             "4" ="[WARN] - Some Mismatched files or directories were detected."
  92.             "3" ="[INFO] - OKCOPY + XTRA"
  93.             "2" ="[INFO] - Some Extra files or directories were detected."
  94.             "1" ="[INFO] - One or more files were copied successfully (that is, new files`r`n have arrived)."
  95.             "0" ="[INFO] - No errors occurred, and no copying was done.`r`nThe source and destination directory trees are completely synchronized."
  96.         }
  97.        
  98.         function Test-Administrator {  
  99.             $user = [Security.Principal.WindowsIdentity]::GetCurrent();
  100.             (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)  
  101.         }
  102.        
  103.         # If running with administrator privileges
  104.         If (Test-Administrator -eq $True) {
  105.             "Has administrator privileges" | out-null
  106.            
  107.             # Create EventLog Source if not already exists
  108.             if ([System.Diagnostics.EventLog]::SourceExists("$JOB") -eq $false) {
  109.             "Creating EventLog Source `"$JOB`""
  110.             [System.Diagnostics.EventLog]::CreateEventSource("$JOB", "Application")
  111.             }
  112.         }
  113.        
  114.         if ($MSG."$ExitCode" -gt $null) {
  115.             $StatusReport = $MSG."$exitCode"
  116.             Write-EventLog -LogName Application -Source $JOB -EventID $ExitCode -EntryType $MSGType."$ExitCode" -Message "Share Folder: $($SHARE)`r`n$($StatusReport)`r`nOutput Log: $($LOGFILE)"
  117.         }
  118.         else {
  119.             $StatusReport = "[Unknown] Can't interpret this exit code."
  120.             Write-EventLog -LogName Application -Source $JOB -EventID $ExitCode -EntryType Warning -Message "Unknown ExitCode. EventID equals ExitCode"
  121.         }
  122.        
  123.         $notifyCodes = @(16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
  124.         if ($notifyCodes -contains $exitCode) {
  125.             $parseLog = Get-Content $LOGFILE | out-string
  126.             $pattern = [regex]::match($parseLog,'(?<=\    Extras).+(?=)',"singleline").value
  127.  
  128. # Email Body       
  129. $BODY=@"
  130. ------------------------------------------------------------------------------
  131. <span style='color:blue;font-weight:bold;'>Share Folder: $($SHARE)</span>
  132. ------------------------------------------------------------------------------
  133. Started : $($DATESTAMPEXT)
  134.  
  135.  Source : $($SOURCE)
  136.    Dest : $($DESTINATION)
  137.  
  138. Options : $($WHAT) $($OPTIONS)
  139. ------------------------------------------------------------------------------
  140.  
  141.               Total    Copied   Skipped  Mismatch    FAILED    Extras
  142. $($pattern)
  143. <span style='font-style:italic;'>$($StatusReport)</span>
  144.  
  145. Output Log: <a href='$LOGFILE'>$($JOB)_$($SHARE)_$($DATESTAMP).log</a>
  146.  
  147.  
  148. "@
  149.         write-output $BODY
  150.         Add-content $LOGFILE "$($DATESTAMPEXT) $($StatusReport)" -PassThru | out-null
  151.         }
  152.         # Pass through the exit code.
  153.         Exit $exitCode     
  154.     }
  155. }
  156.  
  157. $JOBS | Wait-Job | out-null
  158. $RESULTS += $JOBS | Receive-Job
  159.  
  160. Out-File -InputObject $RESULTS -FilePath $EMAILLOG -Force | out-null
  161.  
  162. $EMAIL=@()
  163.  
  164. $EMAIL +="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>"
  165. $EMAIL +="<html><head><title>Posh Report</title>"
  166. $EMAIL +="<style type='text/css'>"
  167. $EMAIL +="<!--"
  168. $EMAIL +="pre {font-family:fixed,courier new, courier, monospace;font-size:8pt;margin:0em;background-color:#fff;}"
  169. $EMAIL +="-->"
  170. $EMAIL +="</style></head>"
  171. $EMAIL +="<body><pre>"
  172. $EMAIL +="$(foreach ($line in (Get-Content -Path "$($emailLog)" -delimiter "`n")){"$($line)"})"
  173. $EMAIL +="</pre></body></html>"
  174.  
  175. $smtpProps =@{
  176.     smtpserver = "someSMTP"
  177.     From = "someEmail"
  178.     To = "someEmail"
  179.     Subject = "[Robocopy Synch: $($JOB)] Execution report"
  180.     Body = "$($EMAIL)"
  181.     BodyAsHtml = $true
  182. }
  183.  
  184. Send-MailMessage @smtpProps
  185. # We are all done kids!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement