PowerCLI

 View Only
  • 1.  PowerCLI Get-VIAccount output

    Posted Jul 20, 2016 10:07 PM

    Hello,

    I wrote a script to generate list of local accounts on ESXi.  Script provide the information I need but for some reason it output the list of ESXi account multiple times for the same server.  Can't figure out how to make the script so it only return the list once per server.

    $esxiServers = Get-Content c:\temp\esxiServers.txt

    ForEach ($server in $esxiServers)

    {

    Connect-VIServer $server

    Write-Host "Processing $server ..." -BackgroundColor Yellow -ForegroundColor Red

    $userNames = Get-VIAccount

    ForEach ($user in $userNames)

     

    $server + "," + $user.ID + "," + $user.Name | Out-File c:\temp\esxiUserList.txt -Append

    Disconnect-VIServer $server -Confirm:$false

    }

    Thank you,

    Asim



  • 2.  RE: PowerCLI Get-VIAccount output
    Best Answer

    Posted Jul 21, 2016 04:37 AM

    You might have multiple connections open, have a look at the content of the $global:defaultviservers variable.



  • 3.  RE: PowerCLI Get-VIAccount output

    Posted Jul 21, 2016 02:47 PM

    Hi LuCD,

    Thank you, that fixed my issue.

    I added $global:defaultviservers = " " after the Disconnect-VIServer $server -Confirm:$false and it did the trick.  It this the correct way to do this or do you recommend another way.

    $esxiServers = Get-Content c:\temp\esxiServers.txt

    ForEach ($server in $esxiServers)

    {

    Connect-VIServer $server

    Write-Host "Processing $server ..." -BackgroundColor Yellow -ForegroundColor Red

    $userNames = Get-VIAccount

    ForEach ($user in $userNames)

     

    $server + "," + $user.ID + "," + $user.Name | Out-File c:\temp\esxiUserList.txt -Append

    Disconnect-VIServer $server -Confirm:$false

    $global:defaultviservers = " "

    }



  • 4.  RE: PowerCLI Get-VIAccount output

    Posted Jul 21, 2016 03:31 PM

    No, you shouldn't change the $global:defaultviservers yourself.

    That is done by the Connect-ViServer and Disconnect-ViServer cmdlets.

    When you add the Force switch on the Disconnect-ViServer cmdlet it will close all existing connections.

    Always good to do that at the beginning of a script, that way you are sure there no relics left behind that can cause unforeseen behavior of a script