Created
October 27, 2016 08:53
-
-
Save stemid/088eb5a3bd4b2b09d4e6b32464df79ad to your computer and use it in GitHub Desktop.
VMDK-orphaned
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add-pssnapin VMware.VimAutomation.Core | |
Set-PowerCLIConfiguration -invalidCertificateAction 'ignore' -confirm:$false -Scope Session | |
Connect-VIServer -Server 10.220.100.220 -Protocol https | |
$report = @() | |
$daysAgo = (Get-Date).AddDays(-14) | |
$arrUsedDisks = Get-View -ViewType VirtualMachine | % {$_.Layout} | % {$_.Disk} | % {$_.DiskFile} | |
$arrDS = Get-Datastore | Sort-Object -property Name | |
foreach ($strDatastore in $arrDS) { | |
$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) | |
{ | |
if (-not ($arrUsedDisks -contains ($folder.FolderPath + $fileResult.Path)) ` | |
-and ($fileResult.Modification -t $daysAgo)) { | |
$row = "" | Select DS, Path, File, Size, ModDate | |
$row.DS = $strDatastore.Name | |
$row.Path = $folder.FolderPath | |
$row.File = $fileResult.Path | |
$row.Size = $fileResult.FileSize | |
$row.ModDate = $fileResult.Modification | |
# Commented code deletes files and marks them as Deleted in the data set | |
#$dsBrowser.DeleteFile($folder.FolderPath + $fileResult.Path) | |
#$row.Deleted = $TRUE | |
$report += $row | |
} | |
} | |
} | |
} | |
} | |
$report | sort-object Size -descending | export-csv -path "C:\vSphere scripts\orphans.csv" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment