Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- If ((-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) -or (-not (Get-PSSnapin VMware.VimAutomation.Vds -ErrorAction SilentlyContinue)))
- { If (-not (Get-PSSnapin VMware.VimAutomation.Core -Registered -ErrorAction SilentlyContinue))
- { Throw "VMware PowerShell Tools (PowerCLI) is not installed, these are required. Aborting script."
- }
- Else
- { Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
- Add-PSSnapin VMware.VimAutomation.Vds -ErrorAction SilentlyContinue
- }
- }
- Connect-VIServer MyVcenter 3> $null
- $report = @()
- [string[]]$arrUsedDisks = (get-vm).extensiondata.layout.disk.diskfile
- $arrUsedDisks += (get-template).extensiondata.layout.disk.diskfile
- $arrDS = Get-VMHost | Get-Datastore | where {$_.Name -notlike "*local*"} | Sort-Object -property Name -unique
- foreach ($strDatastore in $arrDS) {
- Write-Host $strDatastore.Name
- $ds = Get-Datastore -Name $strDatastore.Name | % {Get-View $_.Id}
- $fileQueryFlags = New-Object VMware.Vim.FileQueryFlags
- $fileQueryFlags.FileSize = $true
- $fileQueryFlags.FileType = $true
- $fileQueryFlags.Modification = $true
- $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
- $searchSpec.details = $fileQueryFlags
- $searchSpec.matchPattern = "*.vmdk"
- $searchSpec.sortFoldersFirst = $true
- $dsBrowser = Get-View $ds.browser
- $rootPath = "[" + $ds.Name + "]"
- $searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
- foreach ($folder in $searchResult)
- {
- foreach ($fileResult in $folder.File)
- {
- if ($fileResult.Path -and ($fileResult.Modification -lt (get-date).adddays(-15) -and ($fileResult.Path -notlike "*-ctk.vmdk")))
- {
- if (-not ($arrUsedDisks -contains ($folder.FolderPath + $fileResult.Path))){
- $row = "" | Select File, SizeGB, ModDate
- $row.File = $folder.FolderPath + $fileResult.Path
- $row.SizeGB = "{0:n2}" -f ($fileResult.FileSize / 1000000000)
- $row.ModDate = $fileResult.Modification
- $report += $row
- }
- }
- }
- }
- }
- $Head = @"
- <style>
- H2 { color: #592c81; font-weight: bold; }
- TR:Nth-Child(Even) {Background-Color: #dddddd;}
- TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;font-family:Helvetica;}
- TR:Hover TD {Background-Color: #C1D5F8;}
- TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #514a79;color: white;}
- TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;width: 5%;}
- 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; }
- .heading { background-color: white; font-weight: bold; font-size:1.2em; border-style:none; }
- </style>
- "@
- $table = $Report | Sort -Property Comment | ConvertTo-Html -Head $Head
- $body = @"
- <H2>Orphaned VMDK files on My Datastores</H2>
- $table
- "@
- 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