Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #requires -version 4.0
  2. #requires -runasadministrator
  3.  
  4. # variables
  5. $catalogPath = # choose location to store catalog
  6. $drive = @() # initialize array (necessary in the event of single drive)
  7. $diskLetter = @() # initialize array
  8.  
  9. # -notmatch to skip network drives and root
  10. $diskLetter += ((get-ciminstance win32_logicaldisk) | ? providername -notmatch '\\' | ? deviceid -notmatch 'c').deviceid
  11. # get-partition doesn't like ":"
  12. $diskLetter | % { $global:drive += (get-partition -driveletter "$_".trim(':') | get-disk) }
  13. $index = 0
  14.  
  15. # execute
  16. $drive | % {
  17.   $destination = $null # i don't cleanup well
  18.   $driveModel = ($_.model).replace(' ', '') # get drive model
  19.   if ($driveModel.length -gt 18) { $driveModel = $driveModel.substring(0, 18) } # trim if long
  20.   $driveSize = "{0:N0}GB" -f ($_.size / 1GB) # get size
  21.   $driveSerial = ($_.serialnumber).replace(' ', '') # get serial
  22.   if ($driveSerial.length -gt 18) { $driveSerial = $driveSerial.substring(0, 18) } # trim if too long
  23.   $source = gci "$($diskLetter[$index])\" -re -depth 2 | ? fullname -notlike "*catalog*" # get structure, -notlike so we don't catalog our catalog
  24.   $destination = $catalogPath + "\" + $driveSize + '_' + $driveSerial + '_' + $driveModel + '_' + $diskLetter[$index].trim(':') # put all the info together
  25.  
  26.   if (!(test-path $destination)) { ri -re -fo $catalogPath\*$driveSerial* } # in case drive letter changed delete the old one
  27.   ni $destination -type directory -ea 0 # make folder for drive
  28.  
  29.   # create folders
  30.   ($source | ? { $_.psiscontainer } | select @{n = 'withtext'; e = { "$destination" + '\' + ($_.fullname).substring(3) } }).withtext | % {
  31.     if ($_) { ni "\\?\$_" -type directory -ea 0 }
  32.   }
  33.  
  34.   # create fake files
  35.   ($source | ? { !($_.psiscontainer) } | select @{n = 'withtext'; e = { "$destination" + '\' + ($_.fullname).substring(3) } }).withtext | % {
  36.     if ($_) { ni "\\?\$_" -ea 0 }
  37.   }
  38.  
  39.   # mirror deletions
  40.   if ($source) {
  41.     diff @(gci $destination -re -name) @(gci "$($diskLetter[$index])\" -re -depth 2 -name | ? { $_ -notlike "*catalog*" }) | ? { $_.sideindicator -eq "<=" } | % { ri -re $destination\$($_.inputobject) -ea 0 }
  42.   }
  43.   $index++
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement