Advertisement
naugrim

Orphaned VMDK report

Sep 27th, 2016
1,652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. If ((-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) -or (-not (Get-PSSnapin VMware.VimAutomation.Vds -ErrorAction SilentlyContinue)))
  2. {   If (-not (Get-PSSnapin VMware.VimAutomation.Core -Registered -ErrorAction SilentlyContinue))
  3.     {   Throw "VMware PowerShell Tools (PowerCLI) is not installed, these are required.  Aborting script."
  4.     }
  5.     Else
  6.     {   Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
  7.         Add-PSSnapin VMware.VimAutomation.Vds -ErrorAction SilentlyContinue
  8.     }
  9. }
  10.  
  11. Connect-VIServer MyVcenter 3> $null
  12.  
  13. $report = @()
  14. [string[]]$arrUsedDisks = (get-vm).extensiondata.layout.disk.diskfile
  15. $arrUsedDisks += (get-template).extensiondata.layout.disk.diskfile
  16. $arrDS = Get-VMHost | Get-Datastore | where {$_.Name -notlike "*local*"} | Sort-Object -property Name -unique
  17. foreach ($strDatastore in $arrDS) {
  18.     Write-Host $strDatastore.Name
  19.     $ds = Get-Datastore -Name $strDatastore.Name | % {Get-View $_.Id}
  20.     $fileQueryFlags = New-Object VMware.Vim.FileQueryFlags
  21.     $fileQueryFlags.FileSize = $true
  22.     $fileQueryFlags.FileType = $true
  23.     $fileQueryFlags.Modification = $true
  24.     $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
  25.     $searchSpec.details = $fileQueryFlags
  26.     $searchSpec.matchPattern = "*.vmdk"
  27.     $searchSpec.sortFoldersFirst = $true
  28.     $dsBrowser = Get-View $ds.browser
  29.     $rootPath = "[" + $ds.Name + "]"
  30.     $searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
  31.  
  32.     foreach ($folder in $searchResult)
  33.     {
  34.         foreach ($fileResult in $folder.File)
  35.         {
  36.             if ($fileResult.Path -and ($fileResult.Modification -lt (get-date).adddays(-15) -and ($fileResult.Path -notlike "*-ctk.vmdk")))
  37.             {
  38.                 if (-not ($arrUsedDisks -contains ($folder.FolderPath + $fileResult.Path))){
  39.                     $row = "" | Select File, SizeGB, ModDate
  40.                     $row.File = $folder.FolderPath + $fileResult.Path
  41.                     $row.SizeGB = "{0:n2}" -f ($fileResult.FileSize / 1000000000)
  42.                     $row.ModDate = $fileResult.Modification
  43.                     $report += $row
  44.                 }
  45.             }
  46.         }
  47.     }
  48. }
  49.  
  50. $Head = @"
  51. <style>
  52. H2 { color: #592c81; font-weight: bold; }
  53. TR:Nth-Child(Even) {Background-Color: #dddddd;}
  54. TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;font-family:Helvetica;}
  55. TR:Hover TD {Background-Color: #C1D5F8;}
  56. TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #514a79;color: white;}
  57. TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;width: 5%;}
  58. DIV { background-color: #c2b5c7; color: #514a79; font-weight: bold; border-style:solid; border-width:1px; border-color:black; font-family:Helvetica; font-size: 1.1em; }
  59. .heading { background-color: white; font-weight: bold; font-size:1.2em; border-style:none; }
  60. </style>
  61. "@
  62.  
  63. $table = $Report | Sort -Property Comment | ConvertTo-Html -Head $Head
  64.  
  65. $body = @"
  66. <H2>Orphaned VMDK files on My Datastores</H2>
  67.  
  68. $table
  69.  
  70. "@
  71.  
  72. Send-MailMessage -From foo@bar.org -To me@bar.org -Subject "Orphaned VMDKs" -Body $body -BodyAsHtml -SmtpServer relay.bar.org
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement