We have requirements to report on a group of VMs in certain blue folders. We would like to generate a report for VMs with less the 100MB of free space.
Taking the script developed in Report Free Space, is this something that can be done?
Try this
$blueFolders = "Blue1","Blue2" $expression1 = [string]::Join('|',$blueFolders) $folderMoRef = Get-View -ViewType Folder -Property Name -Filter @{"Name"=$expression1} | %{ $_.MoRef.Value
} $expression2 = [string]::Join('|',$folderMoRef ) &{foreach($vm in Get-View -ViewType VirtualMachine -Property Name,Guest.Disk `
-Filter @{"Runtime.PowerState"="poweredOn";"Parent"=$expression2}){ $vm.Guest.Disk | where {$_.FreeSpace -le 100MB} |
Select @{N="VMname";E={$vm.Name}},
@{N="Path";E={$_.DiskPath}},
@{N="MBFree";E={[math]::Round(($_.FreeSpace/1MB),0)}},
@{ N="PercFree"; E={[math]::Round((100*($_.FreeSpace/$_.Capacity)),0)}} }} | Sort PercFree | Export-Csv C:\freespace.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Does the addition of this Where-clause produce the results you're after ?
$blueFolders = "Blue1","Blue2" $expression1 = [string]::Join('|',$blueFolders) $folderMoRef = Get-View -ViewType Folder -Property Name -Filter @{"Name"=$expression1} | %{ $_.MoRef.Value
} $expression2 = [string]::Join('|',$folderMoRef ) &{foreach($vm in Get-View -ViewType VirtualMachine -Property Name,Guest.Disk `
-Filter @{"Runtime.PowerState"="poweredOn";"Parent"=$expression2}){ $vm.Guest.Disk | where {$_.FreeSpace -le 100MB} |
Select @{N="VMname";E={$vm.Name}},
@{N="Path";E={$_.DiskPath}},
@{ N="PercFree"; E={[math]::Round((100*($_.FreeSpace/$_.Capacity)),0)}} }} | Sort PercFree | Export-Csv C:\freespace.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
very very close!! Is there a way to add into the csv file a field called MBFree and show how much MB is actually free along with the PercFree
here are the results of the output now
VMname | Path | PercFree | |
PHX1V2240070 | C:\ | 0 | |
PHX1V2240101 | C:\ | 0 | |
PHX1V2240099 | C:\ | 0 |
an example of what would be ideal:
VMname | Path | PercFree | MBFree |
PHX1V2240070 | C:\ | 0 | 30 |
PHX1V2240101 | C:\ | 0 | 10 |
PHX1V2240099 | C:\ | 0 | 95 |
Try this
$blueFolders = "Blue1","Blue2" $expression1 = [string]::Join('|',$blueFolders) $folderMoRef = Get-View -ViewType Folder -Property Name -Filter @{"Name"=$expression1} | %{ $_.MoRef.Value
} $expression2 = [string]::Join('|',$folderMoRef ) &{foreach($vm in Get-View -ViewType VirtualMachine -Property Name,Guest.Disk `
-Filter @{"Runtime.PowerState"="poweredOn";"Parent"=$expression2}){ $vm.Guest.Disk | where {$_.FreeSpace -le 100MB} |
Select @{N="VMname";E={$vm.Name}},
@{N="Path";E={$_.DiskPath}},
@{N="MBFree";E={[math]::Round(($_.FreeSpace/1MB),0)}},
@{ N="PercFree"; E={[math]::Round((100*($_.FreeSpace/$_.Capacity)),0)}} }} | Sort PercFree | Export-Csv C:\freespace.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
my friend, as always, thank you is never enough.
Ths is perfect!!!
Hello again
Another slight modification:
Within the report, can we have the "blue folder" in the csv output? Since the folders match the pool names in View, can it look something like?
Vmname | Path | MBFree | PercFree | Pool |
PHX1V2240101 | C:\ | 3 | 0 | Pool 1 |
PHX1V2240070 | C:\ | 1 | 0 | Pool 2 |
PHX1V2240099 | C:\ | 11 | 0 | Pool 3 |
PHX1V2930701 | C:\ | 5 | 0 | Pool 4 |
PHX1V2930707 | C:\ | 20 | 0 | Pool 5 |
Sure, try this
$blueFolders = "Blue1","Blue2" $expression1 = [string]::Join('|',$blueFolders) $folderMoRef = Get-View -ViewType Folder -Property Name -Filter @{"Name"=$expression1} | %{ $_.MoRef.Value
} $expression2 = [string]::Join('|',$folderMoRef ) &{foreach($vm in Get-View -ViewType VirtualMachine -Property Name,Parent,Guest.Disk `
-Filter @{"Runtime.PowerState"="poweredOn";"Parent"=$expression2}){ $vm.Guest.Disk | where {$_.FreeSpace -le 100MB} |
Select @{N="VMname";E={$vm.Name}},
@{N="Path";E={$_.DiskPath}},
@{N="MBFree";E={[math]::Round(($_.FreeSpace/1MB),0)}},
@{N="PercFree"; E={[math]::Round((100*($_.FreeSpace/$_.Capacity)),0)}},
@{N="Pool";E={Get-View $vm.Parent | Select -ExpandProperty Name}} }} | Sort PercFree | Export-Csv C:\freespace.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You are a machine!!
Thank you, again!!!!
bumping and old thread due to new requirements
We only need to report on C: drive space, not all drives. Easy edit?
Expand the Where-clause.
Something like this
where {$_.DiskPath -match "^C:\\" -and $_.FreeSpace -le 100MB}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
didn't really work too well. I filtered for anything less the 500MB to get some results. the results should look like my reply above,
However they appear different now
DiskPath | Capacity | FreeSpace | DynamicType | DynamicProperty |
C:\ | 12888977408 | 513196032 | ||
C:\ | 12888977408 | 344416256 | ||
C:\ | 12888977408 | 491610112 | ||
C:\ | 12888977408 | 451809280 | ||
C:\ | 12888977408 | 475729920 |
Here is the script:
$blueFolders = "BannerAZCoders", "HCSCoders", "BannerWRCoders", "BannerWRFltCoder", "BannerAZFltCdr", "HIMsOpsStaff", "HIMSCROI", "HIMSPericalm", "HIMSRAC", "HIMSRAC2"
$expression1 = [string]::Join('|',$blueFolders)
$folderMoRef = Get-View -ViewType Folder -Property Name -Filter @{"Name"=$expression1} | %{
$_.MoRef.Value
}
$expression2 = [string]::Join('|',$folderMoRef )
&{foreach($vm in Get-View -ViewType VirtualMachine -Property Name,Parent,Guest.Disk `
-Filter @{"Runtime.PowerState"="poweredOn";"Parent"=$expression2}){
# $vm.Guest.Disk | where {$_.FreeSpace -le 200MB} |
$vm.Guest.Disk |where {$_.DiskPath -match "^C:\\" -and $_.FreeSpace -le 500MB}
Select @{N="VMname";E={$vm.Name}},
@{N="Path";E={$_.DiskPath}},
@{N="MBFree";E={[math]::Round(($_.FreeSpace/1MB),0)}},
@{N="PercFree"; E={[math]::Round((100*($_.FreeSpace/$_.Capacity)),0)}},
@{N="Pool";E={Get-View $vm.Parent | Select -ExpandProperty Name}}
}} | Sort PercFree |
Export-Csv C:\scripts\vpcspace.csv -NoTypeInformation
I think you forgot the pipeline symbol (|) at the end of the line with the Where-clause
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
that was it!!!
a pint of your favorite beverage, on me, at VMworld
Thanks, Luc!