Hi All,
Does any one have script to fetch Datastore Folders along with their Folder Size like below.
Datastore Folder Name Folder Size
------------- ----------------- ---------------
Datastore1 VM1 50 GB
Datastore2 VM2 100 GB
Some VMs are registered and Some are not registered.
Need to find those Unregistered VM Folders and need to delete after cross checking it once.
Thanks in Advance.
-Siva
Try something like this
$datastores = Get-Datastore
foreach($ds in $datastores){
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" > $null
Get-ChildItem -Path DS: | where{$_.ItemType -eq 'Folder' -and $_.Name -notmatch '^\.|^vmk|^esxconsole|tmp'} | %{
New-Object PSObject -Property @{
Datastore = $ds.Name
Folder = $_.Name
SizeGB = [math]::Round((Get-ChildItem -Path "DS:\$($_.Name)" | Measure-Object -Property Length -Sum | select -ExpandProperty Sum)/1GB,1)
}
}
Remove-PSDrive -Name DS -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
$datastores = Get-Datastore
foreach($ds in $datastores){
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" > $null
Get-ChildItem -Path DS: | where{$_.ItemType -eq 'Folder' -and $_.Name -notmatch '^\.|^vmk|^esxconsole|tmp'} | %{
New-Object PSObject -Property @{
Datastore = $ds.Name
Folder = $_.Name
SizeGB = [math]::Round((Get-ChildItem -Path "DS:\$($_.Name)" | Measure-Object -Property Length -Sum | select -ExpandProperty Sum)/1GB,1)
}
}
Remove-PSDrive -Name DS -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd,
Thank you so much, your script works like charm.
Could you please let me know the usage of '^' in your script. or any tutorial or example link so that I can go through it.
Thanks & Regards,
Siva
Sure, in the Where clause I wanted to filter out some system folders.
With the -match operator you have a RegEx as the right-hand side operator.
In the RegEx expression you specify one or more 'masks' for which you want a match.
In the RegEx different masks can be separated by an OR (the | symbol).
In each individual mask you have some 'location specifiers', in this case the ^ indicates I want a macth at the begiining of the text.
I.e. '^abc' means match 'abc' at he beginning of the string. So 'abcd' will match, but 'xyzabc' will not.
There are some free learning resources available.
A good one is the Master PowerShell ebook. In Chapter 13 you find more info on Regular Expressions
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you so much Lucd.
Yes used and worked in 2020 :smileygrin:, Thank you LucD.