PowerCLI

 View Only
  • 1.  add local user to esxi host

    Posted Mar 21, 2025 05:19 AM

    Hi together,
    I try to add a local admin user to ESX hosts in a VCenter.
    Adding the user works well. But, adding it to the admin group failed.

    Adding it in the cli with IP of the host as server it works:
    $rootFolder = Get-Folder -NoRecursion
    New-VIPermission -server 192.168.2.122 -Entity $rootFolder -Principal admin2 -Role Admin -Propagate $true -Confirm:$false

    With the script not.


    Script snippet:


    ###############################################
    # Update password on each host
    ###############################################
                        
    foreach ($vmhost in $vmhosts) {
                                

            Connect-VIServer -Server $vmhost.name -Credential $vccreds
            write-host $vmhost.name
           # new user
                               
           New-VMHostAccount -Id admin2 -Password $NewPassword  -Description "2. admin account"  
           $rootFolder = Get-Folder -NoRecursion

           New-VIPermission -Entity $rootFolder -Principal admin2 -Role Admin -Propagate $true -Confirm:$false
                                
           Disconnect-VIServer -Server $vmhost -Confirm:$false

           $vmhostcount++

           }
                        
       
    Thanks in advance for any ideas.

    Kind regards,

    Christoph         



  • 2.  RE: add local user to esxi host

    Posted Mar 21, 2025 06:41 AM

    When you run the cmdlet via the prompt, are you connected to the ESXi node or a vCenter?
    Are there multiple connections open? Check what is in $global:defaultVIServers.



    ------------------------------


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


    ------------------------------



  • 3.  RE: add local user to esxi host

    Posted Mar 21, 2025 07:26 AM

    $global:defaultVIServers:

    It shows me my 4 server, where I tried to add the user.

    PS C:\Powercli> $global:defaultVIServers

    Name                           Port  User                          
    ----                           ----  ----                          
    server01.test.local... 443   LAB\admin01                
    server02.test.local... 443   LAB\admin01   
    server03.test.local... 443   LAB\admin01   
    server04.test.local... 443   LAB\admin01   


    I first connect to the VCenter to get the hosts.

    ###############################################
                  # connect to each vCenter/disconnect when done
                  ###############################################
                  foreach ($vc in $vcenter_list) {
                         connect-viserver -Server $vc -Credential $vccreds -WarningAction SilentlyContinue -ErrorAction Stop | out-null
                         write-host -ForegroundColor Green "Adding AD group read-only to ($vc) `($vccount of $($vcenter_list.count)`)"
                         $vmhosts = get-vmhost | Where-Object { $_.ConnectionState -eq "Connected" -or $_.ConnectionState -eq "Maintenance" }
                         $dchosts = get-vmhost | Where-Object { $_.ConnectionState -eq "Disconnected" -or $_.ConnectionState -eq "NotResponding " }
                         $dclist += $dchosts
                         disconnect-viserver -confirm:$false *


    Afterwards I connect inside the loop to the hosts.




  • 4.  RE: add local user to esxi host

    Posted Mar 21, 2025 10:30 AM

    If you have multiple connections open it would be best to use the Server parameter on the cmdlets.
    That way you can tell explicitly where the cmdlet should execute

    Like

    New-VMHostAccount -Id admin2 -Password $NewPassword  -Description "2. admin account"  -Server $vmhost.name


    ------------------------------


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


    ------------------------------



  • 5.  RE: add local user to esxi host

    Posted Mar 21, 2025 10:52 AM

    Hi Luc,

    thanks, will add it

    Best

    Christoph




  • 6.  RE: add local user to esxi host

    Posted Mar 25, 2025 01:09 PM

    I never had good results with the 'New-VMHostAccount'. My script still uses the old school ESXICLI commands. One thing to note is that you need to have Lockdown mode disabled for a lot of local user account manipulation. 

    My code:

    Function Lockdownchange ( $value ) {
    # Options are 'lockdowndisabled' and 'lockdownNormal'
    # Change lockdown
    Write-host -ForegroundColor Green "Lockdown mode set to: $value"
        $level = $value
        $vmView = Get-VMHost $esx | Get-View
        $lockdown = Get-View $vmView.ConfigManager.HostAccessManager
        $lockdown.ChangeLockdownMode($level)
    }

    $localuser = "local_admin"
    $vmhosts = Get-VMHost | where {$_.ConnectionState -eq "Connected"}
    ForEach ($esx in $VMHosts) {
        Write-Host "`n$esx"
        Lockdownchange 'lockdowndisabled'
        # Set Permission
        $esxcli = Get-EsxCli -VMHost $esx -V2 
        $esxcliArgs = $esxcli.system.permission.set.CreateArgs()
        $esxcliArgs.id = $localuser
        $esxcliArgs.role = 'Admin'
        $esxcli.system.permission.set.Invoke($esxcliArgs)
        Lockdownchange 'lockdownNormal'
    }

    YMMV




  • 7.  RE: add local user to esxi host

    Posted Apr 17, 2025 10:20 AM

    Hi Todd,

    works perfect for me now

    Thanks for the help.

    Kind regards,

    Chris