david62277

PVS vDisk Cleanup

Feb 6th, 2019
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requires -RunAsAdministrator
  2. <#
  3. Provisioning Services vDisk Cleanup
  4. Written by David Ott
  5. Have you ever noticed that when you delete an old vDisk version or version chain that the files in the store aren't always
  6. deleted?
  7. I got tired of checking behind manually, and wrote this quick script.  As long as you run this from one of your Provisioning
  8. Servers as an administrator it should work without modification.  Explanation of certain sections marked with "#####".
  9. #>
  10.  
  11. <#####
  12. This function looks in each store root for files that match the base file name of the vDisk, gets the version
  13. number from the file name, and compares that version to the versions PVS knows of.  If the file version is not contained
  14. in the list of versions for that vdisk it will flag it for deletion.
  15. #####>
  16. function find-oldvdisks($rs,$fn,$lv) {
  17. foreach ($r in $rs) {
  18. (gci $r "*$fn*").fullname | %{
  19. $v = ($_ -split "\.")[($_ -split "\.").count - 2] | ?{$_ -match "^\d+"}
  20. if ($v -eq $null) {$v = 0}
  21. if (($lv -notcontains $v)) {
  22. $_
  23. }
  24. }
  25. }
  26. }
  27. ##### imports PVS module
  28. ipmo 'C:\Program Files\Citrix\Provisioning Services Console\Citrix.PVS.SnapIn.dll'
  29. ##### gets info on all vDisks
  30. $vdisks = Get-PvsDiskInfo
  31. ##### sets a blank array
  32. $ovdisks = @()
  33. <#####
  34. Gets the unc path for each vDisk (ie:\\server\d$\Store\2016image.20.vhdx).  Using that information it breaks down
  35. the file name into the base and the version number.  Finally it sends the store root path (ie: \\server\d$\Store),
  36. the root file name (ie: 2016image) and the version numbers that PVS knows about (ie: 20).
  37. The reply from the function is stored in the $ovdisks array
  38. #####>
  39. foreach ($vdisk in $vdisks) {
  40. $vdiskinventory = Get-PvsDiskInventory -DiskLocatorId $vdisk.DiskLocatorId | select version,@{n='filepath';e={if ($_.filepath -like "\\*"){
  41. $_.filepath
  42. } else {
  43. $drive = ($_.filepath -split ":")[0];`
  44. $servername = $_.servername;`
  45. $fpath = ($_.filepath -split ":")[1];`
  46. "\\$servername\$drive`$$fpath"}}} | %{
  47. $version = $_.version
  48. $rootpath = $_.filepath
  49. Join-Path $rootpath (Get-PvsDiskVersion -DiskLocatorId $vdisk.DiskLocatorId | ?{$_.version -eq $version}).DiskFileName
  50. }
  51. $vdiskversions = (Get-PvsDiskVersion -DiskLocatorId $vdisk.DiskLocatorId).version
  52. $rootstores = $vdiskinventory | %{Split-Path $_ -Parent} | sort -Unique
  53. $filename = $vdiskinventory | %{Split-Path $_ -leaf} | %{($_ -split "\.")[0]}| sort -Unique
  54. $ovdisks += find-oldvdisks -rs $rootstores -fn $filename -lv $vdiskversions
  55. }
  56. if (($ovdisks | measure).count -eq 0) {
  57. Write-Host "No old vDisks to clean up"
  58. }
  59. <#####
  60. Shows the vDisk files (vhd(x), pvp, lok) in a grid view gui, and allows you to select the files to delete
  61. #####>
  62. $deletes = $ovdisks | Out-GridView -Title "Select vDisks to delete" -OutputMode Multiple
  63. ##### Deletes the files
  64. $deletes | %{
  65. ri $_ -Force -Verbose
  66. }
Add Comment
Please, Sign In to add comment