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.
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
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
I'd like to only pull the information from guests in the "blue" folders under VMs and Template. Like the four below.
Thank your for making the code a bit faster!
I don't even dare to suggest to use something like "Get-Folder -Name Banner*" as it is too easy
Or if you would like to filter them by the fact that they are blue, Luc can help, as always : http://www.lucd.info/2010/10/21/get-the-folderpath/
Tim
I inherited this environment (VMware View). Yeah, filtering by "Banner" would return a lot of stuff. Not everything, but more than needed.
Thanks, Tim!
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
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!