Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- param (
- [string]$Path = "C:\MSNICT", #file path
- [string]$LogFile = "drivechange.log", #log file name
- [int]$MaxLogLines = 500, #maximum number of lines for the log file
- [bool]$EventLog = $False, #log errors to the event log
- [Parameter(Mandatory=$true)][string]$DriveLetter, #drive letter to check
- [int]$Frequency = 7 #number of days before drive swap warnings
- )
- [int]$DaysSinceSwitch # global variable to hold days since the drive was switched out
- [string]$DriveUUID # global variable to hold persistent drive hash
- [string]$CurrentDriveUUID # global variable to hold current drive hash
- $Drive = $DriveLetter+":" # drive letter with : added
- $UUIDRegex = [regex]"(?<=\{).*?(?=\})" # regular expression to pull the uuid out of the volume device id
- $LogCanonical = $Path+'\'+$LogFile # create full log file with path string
- $LogTime = Get-Date -Format "[dd-MM-yyyy hh:mm:ss]" # get timestamp
- # log writing convenience function
- function Write-Log {
- param( [string]$Value, [bool]$Force = $False, [bool]$DoEvent = $False, [int]$EventID = 1 )
- if($Force) {
- Add-Content -Path $LogCanonical -Value $LogTime" $Value" -Force
- } else {
- Add-Content -Path $LogCanonical -Value $LogTime" $Value"
- }
- if($EventLog -And $DoEvent) {
- New-EventLog -LogName Application -Source "Backup Drive Monitor"
- Write-EventLog -LogName Application -Source "Backup Drive Monitor" -EntryType Error -EventId $EventID -Message $Value
- }
- }
- # log file size limiting function - call before any exit
- function Log-Trunicate {
- # Limit log file length
- (get-content $LogCanonical -tail $MaxLogLines -readcount 0) | set-content $LogCanonical
- }
- # check registry keys and values for drive and create if not exist
- function Initialize-Registry {
- # check if registry key exists for drive and create if not exist
- $RegistryKey = 'HKLM:\SOFTWARE\MSNICT\DriveChange\'+$DriveLetter
- if( -not (Test-Path $RegistryKey)) {
- New-Item -Path $RegistryKey -Force
- Write-Log -Value $DriveLetter" drive registry key created"
- Write-Host "Created the registry key for drive"$DriveLetter" and logged creation"
- }
- # check if registry values exist for drive and create if not exist
- $TestKey = Get-Item -Path $RegistryKey
- if( $TestKey.GetValue('DaysSinceSwitch') -eq $null) {
- #create value and initialize variable if it doesn't exist
- $DaysSinceSwitch = 0
- New-ItemProperty -Path $RegistryKey -Name 'DaysSinceSwitch' -PropertyType DWord -Value 0
- Write-Host "Created registry value DaysSinceSwitch: "$DaysSinceSwitch
- } else {
- #load value from the registry to variable
- try {
- $Key = Get-ItemProperty -Path $RegistryKey -Name 'DaysSinceSwitch' -ErrorAction Stop
- $DaysSinceSwitch = $Key.DaysSinceSwitch
- Write-Host "Loaded DaysSinceSwitch registry key: "$DaysSinceSwitch
- } catch {
- $DaysSinceSwitch = 0
- }
- }
- if( $TestKey.GetValue('DriveUUID') -eq $null) {
- #create value and initialize variable if it doesn't exist
- $QueryObject = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = '$Drive'" | Select-Object -ExpandProperty DeviceID | Select -First 1
- $DriveUUID = $UUIDRegex.match($QueryObject)
- New-ItemProperty -Path $RegistryKey -Name 'DriveUUID' -PropertyType String -Value $DriveUUID
- Write-Host "Created registry value DriveUUID: "$DriveUUID
- } else {
- #load value from the registry to variable
- try {
- $Key = Get-ItemProperty -Path $RegistryKey -Name 'DriveUUID' -ErrorAction Stop
- $DriveUUID = $Key.DriveUUID
- Write-Host "Loaded DriveUUID registry key: "$DriveUUID
- } catch {
- $QueryObject = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = '$Drive'" | Select-Object -ExpandProperty DeviceID | Select -First 1
- $DriveUUID = $UUIDRegex.match($QueryObject)
- }
- }
- }
- # check file path and create if not exist
- if((Test-Path $Path) -eq $False) {
- New-Item -ItemType Directory -Force -Path $Path
- }
- # check log file and create if not exist
- if ((Test-Path $LogCanonical) -eq $False)
- {
- Write-Log -Value "Log file created" -Force
- Write-Host "Created new log file and logged creation"
- }
- # drive letter checking
- if(Test-Path $DriveLetter":") {
- Write-Host "Backup Drive Found: $DriveLetter"
- #Load saved UUID and drive change number of days from registry
- Initialize-Registry
- #Load current drive's UUID
- $QueryObject = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = '$Drive'" | Select-Object -ExpandProperty DeviceID | Select -First 1
- $CurrentDriveUUID = $UUIDRegex.match($QueryObject)
- Write-Host "Current DriveUUID: "$CurrentDriveUUID
- } else {
- Write-Log -Value "Backup Drive Not Found: $DriveLetter" -DoEvent $True
- Write-Host "Backup Drive Not Found: $DriveLetter"
- Log-Trunicate
- exit
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement