Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #requires -version 4.0
- #requires -runasadministrator
- # variables
- $catalogPath = # choose location to store catalog
- $drive = @() # initialize array (necessary in the event of single drive)
- $diskLetter = @() # initialize array
- # -notmatch to skip network drives and root
- $diskLetter += ((get-ciminstance win32_logicaldisk) | ? providername -notmatch '\\' | ? deviceid -notmatch 'c').deviceid
- # get-partition doesn't like ":"
- $diskLetter | % { $global:drive += (get-partition -driveletter "$_".trim(':') | get-disk) }
- $index = 0
- # execute
- $drive | % {
- $destination = $null # i don't cleanup well
- $driveModel = ($_.model).replace(' ', '') # get drive model
- if ($driveModel.length -gt 18) { $driveModel = $driveModel.substring(0, 18) } # trim if long
- $driveSize = "{0:N0}GB" -f ($_.size / 1GB) # get size
- $driveSerial = ($_.serialnumber).replace(' ', '') # get serial
- if ($driveSerial.length -gt 18) { $driveSerial = $driveSerial.substring(0, 18) } # trim if too long
- $source = gci "$($diskLetter[$index])\" -re -depth 2 | ? fullname -notlike "*catalog*" # get structure, -notlike so we don't catalog our catalog
- $destination = $catalogPath + "\" + $driveSize + '_' + $driveSerial + '_' + $driveModel + '_' + $diskLetter[$index].trim(':') # put all the info together
- if (!(test-path $destination)) { ri -re -fo $catalogPath\*$driveSerial* } # in case drive letter changed delete the old one
- ni $destination -type directory -ea 0 # make folder for drive
- # create folders
- ($source | ? { $_.psiscontainer } | select @{n = 'withtext'; e = { "$destination" + '\' + ($_.fullname).substring(3) } }).withtext | % {
- if ($_) { ni "\\?\$_" -type directory -ea 0 }
- }
- # create fake files
- ($source | ? { !($_.psiscontainer) } | select @{n = 'withtext'; e = { "$destination" + '\' + ($_.fullname).substring(3) } }).withtext | % {
- if ($_) { ni "\\?\$_" -ea 0 }
- }
- # mirror deletions
- if ($source) {
- diff @(gci $destination -re -name) @(gci "$($diskLetter[$index])\" -re -depth 2 -name | ? { $_ -notlike "*catalog*" }) | ? { $_.sideindicator -eq "<=" } | % { ri -re $destination\$($_.inputobject) -ea 0 }
- }
- $index++
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement