I have a script that gets vmname, cpu, memory etc & works quite well. I've been asked to add user assigned to VM & pool entitlements. Because of the large number of vms we have when I run code ($userdisplayname = $(Get-DesktopVM -Name $_.Name).user_displayname) takes over 30 sec. If there is a better way to do this please let me know. Below is the main code that works. Thanks in advance.
#########################
# get list of all VMs #
#########################
$vms = Get-View -ViewType VirtualMachine | sort-object -property Name
#########################################################
# go through each VM writing out pertinent information #
#########################################################
$vms | foreach-object -process {
# make sure it's not a clone. If so, exclude. Error check to make sure no null strings.
if ($_.Name -notmatch "clone" -and $_.Name -ne "" -and ($_.Name.indexof('replica') -ne 0))
{
# Calculate memory
if ($_.Config.Hardware.MemoryMB -ne "")
{
if ($_.Config.Hardware.MemoryMB -le 1024)
{$memory = 1}
else
{$memory = [math]::round($_.Config.Hardware.MemoryMB/1024, 0)}
}
# Calculate disk totals
$total_diskKB = $_.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualDisk"} | measure-object -property CapacityinKB -sum
if ($total_diskKB -ne "")
{
$total_diskGB = [math]::round($total_diskKB.Sum/1048576,2)
}
else
{
$total_diskGB = 0
}
#get billing code
$vmcode = (($_.summary.customvalue) | where {$_.key -eq "202"}).value
################################################
#View Code to get users #
################################################
$Poolid = $(Get-DesktopVM -Name $_.Name).pool_id
$userdisplayname = $(Get-DesktopVM -Name $_.Name).user_displayname
Need help here to get user assigned & users entitled. Thanks!!
You could try with a hash table, instead of doing the Get-DesktopVM multiple times.
Something like this
# Populate the table (before the Get-View loop) $lookupTab = @{} Get-DesktopVM | %{ $lookupTab.Add($_.Name,$_) } ....
# Inside the Get-View loop $Poolid = $lookupTab[$_.Name].pool_id
$userdisplayname = $lookupTab[$_.Name].user_displayname
First you fill up the table, and later you can index into the table with the name of the VM
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I’m a newbie so thanks for the great idea. Is there an easy way to see the contents of the hash table? I’m not getting any results for $poolid or $userdisplayname.
Thanks
I’m a newbie so thanks for the great idea. Is there an easy way to see the contents of the hash table? I’m not getting any results for $poolid or $userdisplayname.
Thanks
You can use the GetEnumerator method on a hash table.
Like this
$lookupTab.GetEnumerator()
But it's probably easier if you use an editor with debugging options.
This is just one of the free ones My favorite PowerShell editor is free now!
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD
Thank you for the help recommending a hash table & free editor. I’m still learning the editor but what a great tool to have. I’ve been able to find 2 issues using debug mode. ☺