VMware Cloud Community
Troy_Clavell
Immortal
Immortal
Jump to solution

Report Free Space

We have the below code that needs a bit of tweaking.  The object is to get guests out of a certain number of 'blue' folders.

Get-VM  | where { $_.PowerState -eq "PoweredOn" } |Get-VMGuest | where {$_.Disks} | Select VmName -ExpandProperty Disks | Select VmName, Path, @{ N="PercFree"; E={ [math]::Round( ( 100 * ( $_.FreeSpace / $_.Capacity ) ),0 ) } } | Sort PercFree | Export-Csv C:\scripts\freespace.csv -NoTypeInformation 

any help would be greatly appreciated.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, missed that part about the blue folders.

Will this do the trick ?

In the variable $blueFolders you store the names of the blue folders for which you want a report.

And with a bit a bit of Get-View filtering on the Parent of the VM we find the VMs in the folders.

Note, this won't work recursively !

$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 |   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

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

I don't know what tweaking you had in mind, but this should make the script a bit faster

&{foreach($vm in Get-View -ViewType VirtualMachine -Property Name,Guest.Disk -Filter @{"Runtime.PowerState"="poweredOn"}){  
  $vm.Guest.Disk | 
  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

Troy_Clavell
Immortal
Immortal
Jump to solution

I'd like to only pull the information from guests in the "blue" folders under VMs and Template.  Like the four below.

2116677.png

Thank your for making the code a bit faster!

0 Kudos
schepp
Leadership
Leadership
Jump to solution

I don't even dare to suggest to use something like "Get-Folder -Name Banner*"  as it is too easy Smiley Wink

Or if you would like to filter them by the fact that they are blue, Luc can help, as always Smiley Wink : http://www.lucd.info/2010/10/21/get-the-folderpath/

Tim

Troy_Clavell
Immortal
Immortal
Jump to solution

I inherited this environment (VMware View).  Yeah, filtering by "Banner" would return a lot of stuff.  Not everything, but more than needed.

Thanks, Tim!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, missed that part about the blue folders.

Will this do the trick ?

In the variable $blueFolders you store the names of the blue folders for which you want a report.

And with a bit a bit of Get-View filtering on the Parent of the VM we find the VMs in the folders.

Note, this won't work recursively !

$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 |   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

0 Kudos
Troy_Clavell
Immortal
Immortal
Jump to solution

Brilliant! Since there are no sub-folders under the parent, this is exactly what I was looking for.

You make my job very easy sir, thank you!

0 Kudos