VMware Cloud Community
RThornburg
Contributor
Contributor

Powercli consuming Ram on Get-VMHostAccount lookup

I am running this script to try and lookup and validate all the local user accounts on ESXi hosts in the enterprise. 

This script works and reaches out to all the hosts and gets the local accounts list back to a csv. But the RAM on the host being used by PowerShell running the script balloons to several GB and does not release until the window is closed.

Does anyone have an idea why the RAM is being consumed so much or how to fix it?

$date=get-date -Format hhmm.yyyyMMdd
$log = "c:\host_audit-$date.csv"
$credentials = Import-Clixml "C:\host.xml"
$hostlist = Get-VMHost -Server * | Select Name,Powerstate 
    ForEach ($h in $hostlist)
        {
        $hnme = $h.Name
        $hpwr = $h.Powerstate
        $hnme
            If ($hpwr -eq "PoweredOn")
                {
                $esx = Connect-VIServer $hnme -Credential $credentials
                If ($esx -ne $null)
                    {
                    Get-VMHostAccount -Server $hnme | Select @{N="Host";E={$_.Name}},@{N="Uid";E={$_.ExtensionData.Id}},Id,@{N="Account";E={$_.ExtensionData.FullName}},@{N="Powerstate";E={$hostpwr}} | Export-Csv -Path $log -Append -Force -NoTypeInformation
                    Disconnect-VIServer -Server $hnme -Confirm:$false -Force
                    }
                Else
                    {
                    Remove-Variable OutputObj -ErrorAction SilentlyContinue
                    $OutputObj = New-Object -Type PSObject
                    $OutputObj | Add-Member -MemberType NoteProperty -Name Host -Value $hnme -Force
                    $OutputObj | Add-Member -MemberType NoteProperty -Name Uid -Value "" -Force
                    $OutputObj | Add-Member -MemberType NoteProperty -Name Id -Value "" -Force
                    $OutputObj | Add-Member -MemberType NoteProperty -Name Account -Value "" -Force
                    $OutputObj | Add-Member -MemberType NoteProperty -Name Powerstate -Value "LoginFail" -Force
                    $OutputObj | Export-Csv -Path $log -Append -Force -NoTypeInformation
                    }
                }
            else
                {
                Get-VMHost $hnme | Select @{N="Host";E={$hnme}},@{N="Uid";E={}},@{N="Id";E={}},@{N="Account";E={}},Powerstate | export-csv -Path $log -Append -force -NoTypeInformation
                }
            }

 

0 Kudos
5 Replies
LucD
Leadership
Leadership

Not sure against how many ESXi nodes you are running the script.
Nor how many local accounts each of these ESXi nodes has.

When I run it against 3 ESXi nodes in my lab, the total memory used does not go above 300MB.
The 1st increase in memory is the Connect-VIServer, which loads a number of PowerCLI modules.
The 2nd increase is by the script. 

mem.jpg

Btw, what you save as Host is in fact the account name, not the name of the ESXi node.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
RThornburg
Contributor
Contributor

I am running this against hundreds of nodes but have observed the RAM grow even limiting it using -first 25 on the $hostlist. And each host should not have any accounts outside of the 3 built in accounts. And your right I messed up pasting and generalizing my script here in the variable for Host. My actual run script pulls the hostname properly.  

I have added a pause after the disconnect-viserver during troubleshooting and see the ram increase on each successive host processed. If the connect-viserver causes an increase on every host loaded is there a way to release that before moving on to the next host?

0 Kudos
LucD
Leadership
Leadership

It is hard to guess where the memory is consumed.
You could insert the following line at strategic positions in your script in order to try and find the major contributor.
You can add additional text to the output to show where exactly the output was produced.

Write-Host "MemMB $((Get-Process | Where-Object { $_.Id -eq $pid }).WorkingSet64 / 1MB)"

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
RThornburg
Contributor
Contributor

Wow, thanks that's a cool look up. So here is the Ram after the first 25. So I went up 236mb in 25 hosts. so about 10mb each. 

1 of 25 MemMB 553.88
2 of 25 MemMB 568.8
3 of 25 MemMB 576.36
4 of 25 MemMB 584.61
5 of 25 MemMB 596.79
6 of 25 MemMB 610.97
7 of 25 MemMB 614.59
8 of 25 MemMB 614.62
9 of 25 MemMB 614.05
10 of 25 MemMB 610.63
11 of 25 MemMB 626.08
12 of 25 MemMB 641.79
13 of 25 MemMB 655.19
14 of 25 MemMB 660.77
15 of 25 MemMB 659.97
16 of 25 MemMB 667.82
17 of 25 MemMB 678.45
18 of 25 MemMB 696.99
19 of 25 MemMB 706.02
20 of 25 MemMB 721.75
21 of 25 MemMB 738.11
22 of 25 MemMB 753.44
23 of 25 MemMB 766.84
24 of 25 MemMB 782.51
25 of 25 MemMB 790.74

Shouldn't the disconnect command release the ram from the connection?

0 Kudos
LucD
Leadership
Leadership

I wouldn't know.
You can check by adding that line before and after the Disconnect-VIServer.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos