Created
April 11, 2017 14:37
-
-
Save pecigonzalo/2a29cf83e530ac9fc8651eda5b525e6b to your computer and use it in GitHub Desktop.
Trigger action on external disk connected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The targetpartition to backup to, obtain it with: | |
# Get-WmiObject Win32_LogicalDisk | Select-Object DeviceID, VolumeSerialNumber | |
$TargetPartition = '' | |
# Notification settings | |
$NotifyFrom = '' | |
$NotifyTo = '' | |
$SmtpServer = '' | |
$SmtpPort = '587' | |
$SmtpPass = '' | |
$SecurePassword=Convertto-SecureString –String $SmtpPass –AsPlainText –force | |
$SmtpCred = new-object -typename System.Management.Automation.PSCredential ` | |
-argumentlist $NotifyFrom, $SecurePassword | |
# Write-Debug $TargetPartition | |
# Write-Debug $NotifyFrom | |
# Write-Debug $NotifyTo | |
# Write-Debug $SmtpServer | |
function CheckDiskPresent { | |
Start-Sleep 10 | |
Write-Debug "DEBUG: Checking device is present" | |
$LogicalDisks = Get-WmiObject Win32_LogicalDisk | | |
Select-Object * | |
$TargetVolume = $nil | |
$LogicalDisks | ForEach-Object { | |
Write-Debug "DEBUG: $($_.VolumeSerialNumber)" | |
if ($_.VolumeSerialNumber -eq $TargetPartition) { | |
$TargetVolume = $_.DeviceId | |
Write-Debug "DEBUG: device found on volume $TargetVolume" | |
return $TargetVolume | |
} | |
} | |
} | |
function NofifyFailed ($body) { | |
Write-Debug "DEBUG: Sending Failed Notification" | |
if (!$body) { | |
$body = " " | |
} | |
Send-MailMessage -From $NotifyFrom -To $NotifyTo -SmtpServer $SmtpServer -Port $SmtpPort -Credential $SmtpCred ` | |
-Subject 'Notificaciones de Backup: Error en el backup' ` | |
-Body $body | |
} | |
function NotifySuccess ($body) { | |
Write-Debug "DEBUG: Sending Success Notification" | |
if (!$body) { | |
$body = " " | |
} | |
Send-MailMessage -From $NotifyFrom -To $NotifyTo -SmtpServer $SmtpServer -Port $SmtpPort -Credential $SmtpCred ` | |
-Subject 'Notificaciones de Backup: Completado con exito' ` | |
-Body $body | |
} | |
function DeleteOldFiles($path) { | |
$limit = (Get-Date).AddDays(-180) | |
$path = $path + "\Monthly" | |
# Delete files older than the $limit. | |
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit } | Remove-Item -Force | |
# Delete any empty directories left behind after deleting the old files. | |
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse | |
} | |
function Start-Backup { | |
$TargetVolume = CheckDiskPresent | |
if ($TargetVolume) { | |
Write-Output "INFO: Disk $TargetPartition present on $TargetVolume" | |
try { | |
Stop-Service CobianBackup11 | |
Write-Output "INFO: Service stopped" | |
&'C:\Program Files (x86)\Cobian Backup 11\Cobian.exe' '-tasks:{535A4CED-EA1F-4E06-BB4B-469152165FDC}' 'nogui' '-autoclose' | Out-Null | |
Write-Output "INFO: Deleting old files" | |
DeleteOldFiles($TargetVolume) | |
} | |
catch { | |
Write-Error "Unable to complete backup job " | |
NofifyFailed($_.Exeption.Message) | |
} | |
Start-Service CobianBackup11 | |
NotifySuccess('Backup task {535A4CED-EA1F-4E06-BB4B-469152165FDC} completed OK') | |
} else { | |
Write-Output "INFO: Disk $TargetPartition NOT present" | |
} | |
} | |
Register-WmiEvent -Class win32_VolumeChangeEvent -SourceIdentifier volumeChange | |
write-host "INFO: Beginning script..." | |
do { | |
$newEvent = Wait-Event -SourceIdentifier volumeChange | |
$eventType = $newEvent.SourceEventArgs.NewEvent.EventType | |
$eventTypeName = switch($eventType) { | |
1 {"Configuration changed"} | |
2 {"Device arrival"} | |
3 {"Device removal"} | |
4 {"docking"} | |
} | |
write-host (get-date -format s) " Event detected = " $eventTypeName | |
if ($eventType -eq 2) { | |
Start-Backup | |
} | |
Remove-Event -SourceIdentifier volumeChange | |
} while (1-eq1) #Loop until next event | |
Unregister-Event -SourceIdentifier volumeChange |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment