PowerCLI

 View Only
  • 1.  Connect-VIserver to ESXi host and disconnect

    Posted Dec 13, 2016 08:25 PM

    Good afternoon gods of VMware automation,

    I'm currently trying to do some managing of local ESXi user accounts accosts all hosts in vCenter.  I found an old thread where LucD had some great code to accomplish what I was looking for, but its not 100% for me.

    Thread in question: Need powercli script to create a new local admin account on all ESX and ESXi hosts in the vcenter

    I start my script doing a connect-viserver to the vCenter I'm going to be working on, and then in a loop connect-viserver to each host in the inventory (I didn't know you could use connect-viserver directly to a host before this).  I'm able to add a user, give it admin role, but then I go to disconnect from that host with disconnect-viserver $vmhost      (that $vmhost is a variable that has the current host I'm on in the loop and the same variable i use cor the connect-viserver), but this is where I'm hitting an error.  The error looks to say that it is unable to convert the type vmware.vim.automation.vi.core.impl.v1.inventory.vmhostimpl to type vmware.vim.automatoin.vicore.types.v1.viserver.

    So what this looks like to me is, while connect-viserver is designed to accept a host object as well as a vCenter object, disconnect-viserver only accepts vCenter objects.  If this is accurate you can only connect to a ESXi host and not disconnect.  Please tell me I'm just doing something wrong.  I think I can work around this by doing a disconnec-viserver with no paramaters to close all connections at the end of each loop iteration, but then'd I'd have to re-establish my vCenter connection after the loop.

    Oh and I've also tried something like this

    $srv = connect-viserver $vmhost

    disconnect-viserver $srv

    And that fails with the same error

    Ideas?



  • 2.  RE: Connect-VIserver to ESXi host and disconnect

    Posted Dec 13, 2016 08:44 PM

    Instead of using the $vmhost variable, try using the object (VIServer) that is returned by the Connect-VIServer cmdlet.

    Update: just noticed you tried that already.

    What error does that return?



  • 3.  RE: Connect-VIserver to ESXi host and disconnect
    Best Answer

    Posted Dec 13, 2016 08:48 PM

    This seems to work for me

    $secEsxPswd = ConvertTo-SecureString -String $esxPswd -AsPlainText -Force

    $cred = New-Object System.Management.Automation.PSCredential ($esxUser,$secEsxPswd)

    Get-VMHost | %{

        $srv = Connect-VIServer -Server $_.Name -Credential $cred

        Disconnect-VIServer -Server $srv -Confirm:$false

    }



  • 4.  RE: Connect-VIserver to ESXi host and disconnect

    Posted Dec 14, 2016 07:06 PM

    The god of powerCli has replied to my question!  lol thanks for the help Luc.

    Your approach is almost the same as what I'm doing except where I used a foreach loop you pipped get-vmhost directly into a script block.  Your way seems to work better, but I'm still dealing with some oddities.  I think those might be environment specific (I'm working in a highly secure non internet connected system).  Its odd that you can't simply send the same parameter into disconnect that you can into connect.  I'm going to keep playing with this, but I think your pipe to script block method is the way to go.