]> git.p6c8.net - dsmonrot.git/commitdiff
- Output start and end time of script
authorPatrick Canterino <patrick@patrick-canterino.de>
Wed, 17 Jan 2018 19:03:20 +0000 (20:03 +0100)
committerPatrick Canterino <patrick@patrick-canterino.de>
Wed, 17 Jan 2018 23:47:24 +0000 (00:47 +0100)
- Added computer name to default email subject
- Removed some useless output
- Check if network drive was disconnected successfully
- Send error messages are now sent via email

dsmonrot.ps1

index 353e0dbaae7cd1321f12961481a4d3bda0d0253d..82b501487a238bfe134293574f1c3d022ffc546d 100644 (file)
@@ -46,7 +46,7 @@
 # To address of email notification\r
 [String]$emailToAddress = "patrick@test.local"\r
 # Subject of email notification\r
-[String]$emailSubject = "DSMonRot"\r
+[String]$emailSubject = "DSMonRot on $env:computername"\r
 # Mail server\r
 [String]$emailMailserver = "localhost"\r
 # SMTP Port\r
@@ -70,21 +70,23 @@ function Send-Email([String]$body) {
        Write-Host "Sending email: $emailToAddress, $body"\r
        \r
        try {\r
-               $smtp = New-Object System.Net.Mail.SmtpClient($emailMailServer, $emailPort);\r
+               $smtp = New-Object System.Net.Mail.SmtpClient($emailMailServer, $emailPort)\r
 \r
                $smtp.EnableSSL = $emailSSL\r
 \r
                if($emailAuth) {\r
-                       $smtp.Credentials = New-Object System.Net.NetworkCredential($emailUser, $emailPassword);\r
+                       $smtp.Credentials = New-Object System.Net.NetworkCredential($emailUser, $emailPassword)\r
                }\r
 \r
-               $smtp.Send($emailFromAddress, $emailToAddress, $emailSubject, $body);\r
+               $smtp.Send($emailFromAddress, $emailToAddress, $emailSubject, $body)\r
        }\r
        catch {\r
                Write-Host "Could not send email: $_.Exception.Message"\r
        }\r
 }\r
 \r
+Write-Host "Started at" (Get-Date -format "yyyy-MM-dd HH:mm:ss")\r
+\r
 $errorMessages = @()\r
 \r
 $smbConnected = $False\r
@@ -111,7 +113,7 @@ if($smbDrive) {
                $smbConnected = $True\r
        }\r
        Catch {\r
-               Write-Host "Could not connect to network drive: $_.Exception.Message"\r
+               Write-Host "Could not connect to network drive $smbDrive`: $_.Exception.Message"\r
                exit\r
        }\r
 }\r
@@ -161,12 +163,15 @@ if((Test-Path $backupTarget) -and (Test-Path $backupTargetFull) -and (Test-Path
                & $dsPath $dsArgs\r
                \r
                if($LastExitCode -ne 0) {\r
-                       Write-Host "Error code: $LastExitCode"\r
+                       Write-Host "Drive Snapshot failed to backup! Exit code: $LastExitCode"\r
+                       $errorMessages += "Drive Snapshot failed to backup! Exit code: $LastExitCode"\r
+               }\r
+               else {\r
+                       $success = $True\r
                }\r
        }\r
        else {\r
                Write-Host "Directory $backupTargetDiff already exists!"\r
-               \r
                $errorMessages += "Directory $backupTargetDiff already exists!"\r
        }\r
 }\r
@@ -191,13 +196,15 @@ else {
        & $dsPath $dsArgs\r
        \r
        if($LastExitCode -ne 0) {\r
-               Write-Host "Error code: $LastExitCode"\r
+               Write-Host "Drive Snapshot failed to backup! Exit code: $LastExitCode"\r
+               $errorMessages += "Drive Snapshot failed to backup! Exit code: $LastExitCode"\r
+       }\r
+       else {\r
+               $success = $True\r
        }\r
-       \r
-       $success = $False\r
 }\r
 \r
-if($isDiff -eq $False -and $success -eq $True -and $keepMonths -ge 0) {\r
+if(!$isDiff -and $success -eq $True -and $keepMonths -ge 0) {\r
        Write-Host "Rotating"\r
        \r
        $keepMonthsCount = $keepMonths\r
@@ -210,8 +217,6 @@ if($isDiff -eq $False -and $success -eq $True -and $keepMonths -ge 0) {
                        $keepMonthsCount--\r
                }\r
                \r
-               Write-Host $keepMonthsCount\r
-               \r
                if($keepMonthsCount -eq -1) {\r
                        Write-Host "Deleting $_"\r
                        Remove-Item -Recurse -Force $_.FullName\r
@@ -221,9 +226,18 @@ if($isDiff -eq $False -and $success -eq $True -and $keepMonths -ge 0) {
 \r
 if($smbConnected) {\r
        Write-Host "Disconnecting network drive"\r
-       Remove-PSDrive $smbDrive\r
+       \r
+       try {\r
+               Remove-PSDrive $smbDrive -ErrorAction Stop\r
+       }\r
+       catch {\r
+               Write-Host "Could not disconnect network drive $smbDrive`: $_.Exception.Message"\r
+               $errorMessages +=  "Could not disconnect network drive $smbDrive`: $_.Exception.Message"\r
+       }\r
 }\r
 \r
 if($emailOnError -and $errorMessages.Count -gt 0) {\r
        Send-Email ("Error:`n"+($errorMessages -join "`n"))\r
-}
\ No newline at end of file
+}\r
+\r
+Write-Host "Ended at" (Get-Date -format "yyyy-MM-dd HH:mm:ss")
\ No newline at end of file

patrick-canterino.de