Powershell script to detect a certain drive that is mounted then perfrom backup

Junior Member
Junior Member (55 reputation)Junior Member (55 reputation)Junior Member (55 reputation)Junior Member (55 reputation)Junior Member (55 reputation)Junior Member (55 reputation)Junior Member (55 reputation)Junior Member (55 reputation)Junior Member (55 reputation)Junior Member (55 reputation)
Group: Forum Members
Posts: 12, Visits: 101
First of all I have a few different backup scripts that perfrom backups to certain external drives depending on their volume name / label / drive letter. I use more or less the default Macrium backup definition file so no need to change anything there on my side.The batch file below mounts the external USB drive so that a backup can be performed. This is called in the backup script of Macrium, I exported to VBS script and added a call to mount the drive.

@echo off
REM change this to the drive letter you want to mount the drive to!
set drive=G
REM Be sure to change this to the Volume Name of the drive you want to mount!
REM You can get the volume name by typing mountvol on the command prompt
set volume=\\?\Volume{e538db8f-6056-11e4-9855-6c626d3a92b8}\
echo Mounting Drive...
mountvol %drive%: %volume%
echo Drive Mounted!

Batch file to call the powershell script. I found this easier than just using the task scheduler to start powershell command directly becasue I wanted logging i.e. output of console to log file and this would not work for some reason when called witht he Windows Task scheduler. So anyway I just created this batch file and call this from the Task scheduler. I can of course unplug and plug back in the drive whenever I want but if you do this remember that the powershell script will be kicked off each time! If you want to do something else on the drive other than backups then just increase the sleep time and before powershell starts you will have the oppurtunity to kill the powershell process. I use a simple taskkill  command for that if necessarry.

set dt=%date:~7,2%-%date:~4,2%-%date:~10,4%
cd "c:\Users\<User>\Documents\Reflect"
powershell.exe -WindowStyle Hidden -ExecutionPolicy Unrestricted -File "C:\Users\<User>\Documents\Reflect\backup_script.ps1" >> "c:\temp\powershell_%dt%.log"
cd "c:\Users\<User>\Documents\Reflect"

Your powershell log would look something like this

2015-03-02T17:08:02  Starting script...
2015-03-02T22:00:00  Event detected =  Device arrival
2015-03-02T22:00:00  Drive name =  G:
2015-03-02T22:00:00  Drive label =  USB_Backup
2015-03-02T22:00:00  Starting task in 60 seconds...
2015-03-02T22:02:36  Event detected =  Device removal

I use the powershell script below to detect when drive G with the label USB_Backup has been mounted. This script is started at user logon and runs the whole time the machine is left on. This powershell script calls a VB script that performs the backup and then copies certain backups to the extrernal USB drive and NAS depending on which backup files I want on the NAS and the USB drive. More important backups are copied to 3 different locations.

#Requires -version 2.0
Register-WmiEvent -Class win32_VolumeChangeEvent -SourceIdentifier volumeChange
write-host (get-date -format s) " Starting script..."
$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)
$driveLetter = $newEvent.SourceEventArgs.NewEvent.DriveName
$driveLabel = ([wmi]"Win32_LogicalDisk='$driveLetter'").VolumeName
write-host (get-date -format s) " Drive name = " $driveLetter
write-host (get-date -format s) " Drive label = " $driveLabel
# Execute process if drive matches specified condition(s)
if ($driveLetter -eq 'G:' -and $driveLabel -eq 'USB_Backup')
write-host (get-date -format s) " Starting task in 15 seconds..."
start-sleep -seconds 15
start-process "C:\Users\<User>\Documents\Reflect\Copy Backup Files to USB and NAS Drives.vbs"
Remove-Event -SourceIdentifier volumeChange
} while (1-eq1) #Loop until next event
Unregister-Event -SourceIdentifier volumeChange

After the backup is finished I unmount the drive:

@echo off
REM change this to the drive you want to unmount!
set drive=G:
echo Unmounting Drive...
mountvol %drive% /d
echo Drive Unmounted!

Edited 3 March 2015 6:59 PM by GlennChambers

Merge Selected

Merge into selected topic...

Merge into merge target...

Merge into a specific topic ID...

Reading This Topic