Advertisement
smokex

Drive Check

Jun 1st, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. param (
  2.     [string]$Path = "C:\MSNICT",                        #file path
  3.     [string]$LogFile = "drivechange.log",               #log file name
  4.     [int]$MaxLogLines = 500,                            #maximum number of lines for the log file
  5.     [bool]$EventLog = $False,                           #log errors to the event log
  6.     [Parameter(Mandatory=$true)][string]$DriveLetter,   #drive letter to check
  7.     [int]$Frequency = 7                                 #number of days before drive swap warnings
  8. )
  9.  
  10. [int]$DaysSinceSwitch                                   # global variable to hold days since the drive was switched out
  11. [string]$DriveUUID                                      # global variable to hold persistent drive hash
  12. [string]$CurrentDriveUUID                               # global variable to hold current drive hash
  13. $Drive = $DriveLetter+":"                               # drive letter with : added
  14. $UUIDRegex = [regex]"(?<=\{).*?(?=\})"                  # regular expression to pull the uuid out of the volume device id
  15. $LogCanonical = $Path+'\'+$LogFile                      # create full log file with path string
  16. $LogTime = Get-Date -Format "[dd-MM-yyyy hh:mm:ss]"     # get timestamp
  17.  
  18.  
  19.  
  20. # log writing convenience function
  21. function Write-Log {
  22.     param( [string]$Value, [bool]$Force = $False, [bool]$DoEvent = $False, [int]$EventID = 1 )
  23.  
  24.     if($Force) {
  25.         Add-Content -Path $LogCanonical -Value $LogTime" $Value" -Force
  26.     } else {
  27.         Add-Content -Path $LogCanonical -Value $LogTime" $Value"
  28.     }
  29.    
  30.     if($EventLog -And $DoEvent) {
  31.         New-EventLog -LogName Application -Source "Backup Drive Monitor"
  32.         Write-EventLog -LogName Application -Source "Backup Drive Monitor" -EntryType Error -EventId $EventID -Message $Value
  33.     }
  34. }
  35.  
  36. # log file size limiting function - call before any exit
  37. function Log-Trunicate {
  38.     # Limit log file length
  39.     (get-content $LogCanonical -tail $MaxLogLines -readcount 0) | set-content $LogCanonical
  40. }
  41.  
  42. # check registry keys and values for drive and create if not exist
  43. function Initialize-Registry {
  44.     # check if registry key exists for drive and create if not exist
  45.     $RegistryKey = 'HKLM:\SOFTWARE\MSNICT\DriveChange\'+$DriveLetter
  46.     if( -not (Test-Path $RegistryKey)) {
  47.         New-Item -Path $RegistryKey -Force
  48.         Write-Log -Value $DriveLetter" drive registry key created"
  49.         Write-Host "Created the registry key for drive"$DriveLetter" and logged creation"
  50.     }
  51.  
  52.     # check if registry values exist for drive and create if not exist
  53.     $TestKey = Get-Item -Path $RegistryKey
  54.     if( $TestKey.GetValue('DaysSinceSwitch') -eq $null) {
  55.         #create value and initialize variable if it doesn't exist
  56.         $DaysSinceSwitch = 0
  57.         New-ItemProperty -Path $RegistryKey -Name 'DaysSinceSwitch' -PropertyType DWord -Value 0
  58.         Write-Host "Created registry value DaysSinceSwitch: "$DaysSinceSwitch
  59.     } else {
  60.         #load value from the registry to variable
  61.         try {
  62.             $Key = Get-ItemProperty -Path $RegistryKey -Name 'DaysSinceSwitch' -ErrorAction Stop
  63.             $DaysSinceSwitch = $Key.DaysSinceSwitch
  64.             Write-Host "Loaded DaysSinceSwitch registry key: "$DaysSinceSwitch
  65.         } catch {
  66.             $DaysSinceSwitch = 0
  67.         }
  68.     }
  69.     if( $TestKey.GetValue('DriveUUID') -eq $null) {
  70.         #create value and initialize variable if it doesn't exist
  71.         $QueryObject = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = '$Drive'" | Select-Object -ExpandProperty DeviceID | Select -First 1
  72.         $DriveUUID = $UUIDRegex.match($QueryObject)
  73.         New-ItemProperty -Path $RegistryKey -Name 'DriveUUID' -PropertyType String -Value $DriveUUID
  74.         Write-Host "Created registry value DriveUUID: "$DriveUUID
  75.     } else {
  76.         #load value from the registry to variable
  77.         try {
  78.             $Key = Get-ItemProperty -Path $RegistryKey -Name 'DriveUUID' -ErrorAction Stop
  79.             $DriveUUID = $Key.DriveUUID
  80.             Write-Host "Loaded DriveUUID registry key: "$DriveUUID
  81.         } catch {
  82.             $QueryObject = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = '$Drive'" | Select-Object -ExpandProperty DeviceID | Select -First 1
  83.             $DriveUUID = $UUIDRegex.match($QueryObject)
  84.         }
  85.     }
  86. }
  87.  
  88.  
  89.  
  90. # check file path and create if not exist
  91. if((Test-Path $Path) -eq $False) {
  92.     New-Item -ItemType Directory -Force -Path $Path
  93. }
  94.  
  95. # check log file and create if not exist
  96. if ((Test-Path $LogCanonical) -eq $False)
  97. {
  98.     Write-Log -Value "Log file created" -Force
  99.     Write-Host "Created new log file and logged creation"
  100. }
  101.  
  102.  
  103.  
  104. # drive letter checking
  105. if(Test-Path $DriveLetter":") {
  106.     Write-Host "Backup Drive Found: $DriveLetter"
  107.     #Load saved UUID and drive change number of days from registry
  108.     Initialize-Registry
  109.     #Load current drive's UUID
  110.     $QueryObject = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = '$Drive'" | Select-Object -ExpandProperty DeviceID | Select -First 1
  111.     $CurrentDriveUUID = $UUIDRegex.match($QueryObject)
  112.     Write-Host "Current DriveUUID: "$CurrentDriveUUID
  113. } else {
  114.     Write-Log -Value "Backup Drive Not Found: $DriveLetter" -DoEvent $True
  115.     Write-Host "Backup Drive Not Found: $DriveLetter"
  116.     Log-Trunicate
  117.     exit
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement