PowerCLI

 View Only
  • 1.  User account creation error

    Posted Nov 05, 2025 06:49 PM

    I am getting error while running the below commands. Can someone please help?

    This is for connecting the vCenter and then create a new user account for each esxi hosts and then add them as part of admin

     
    # Connect to vCenter
    $vCenterServer = "your-vcenter.domain.com"
    Connect-VIServer -Server $vCenterServer

    # Define credentials for the new user
    $newUsername = "root1"
    $newPassword = "Password123"  

    # Get all ESXi hosts
    $esxiHosts = Get-VMHost

    # Create user and assign Admin role
    foreach ($host2 in $esxiHosts) {
        Write-Host "Processing host: $($host2.Name)"

        # Create user
        New-VMHostAccount -VMHost $host2 -Id $newUsername -Password $newPassword -Confirm:$false

        # Assign Admin role
        New-VIPermission -Entity $host2 -Principal $newUsername -Role "Admin" -Propagate:$true
    }

    Error:



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


  • 2.  RE: User account creation error

    Posted Nov 07, 2025 02:23 PM

    Can someone please help me on this?

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



  • 3.  RE: User account creation error

    Posted Nov 07, 2025 02:32 PM
    Edited by p0werShelldude Nov 07, 2025 02:38 PM

    LucD has answered this cleanly here

    https://community.broadcom.com/vmware-cloud-foundation/discussion/creating-local-accounts-on-esx-hosts-with-new-vmhostaccount#bm6216a812-662a-408d-905d-fe450c325498

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



  • 4.  RE: User account creation error

    Posted Nov 08, 2025 05:15 AM

    @LucD / @p0werShelldude

    That scripts talk about, connecting the vCenter and then get the root password of all the ESXi hosts and then create a new account. This logic wont work in our environment since all the esxi hosts has its own separate root password. So I am looking for a help to sort the current situation. So I am looking for a script that can connect the vCenter (That has admin privileges) and then create new user account and then add that as part of Admin account. Please let me know if we have a solution for this situation.

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



  • 5.  RE: User account creation error

    Posted Nov 08, 2025 08:04 AM
    Edited by a_p_ Nov 08, 2025 08:05 AM

    Please see whether the below meets your requirements.

    André

    $cred = Get-Credential -UserName "administrator@vsphere.local" -Message "Enter the vCenter Server admin's credentials."
    Connect-VIServer -Server <your-vcenter-server> -Credential $cred

    $NewUserCredentials = Get-Credential -UserName "root1" -Message "Enter the new user's credentials."


    $vmhosts = Get-VMHost
    foreach ($vmhost in $vmhosts) {
        Write-Host "Creating $($NewUserCredentials.UserName) on $($vmhost.name) ..."
        $esxcli = get-esxcli -vmhost $vmhost -v2
        
        $esxcliArgs = $esxcli.system.account.add.CreateArgs()
        $esxcliArgs.id = $NewUserCredentials.UserName
        $esxcliArgs.description = "Yet Another user Account"
        $esxcliArgs.shellaccess = $false
        $esxcliArgs.password = $NewUserCredentials.GetNetworkCredential().Password
        $esxcliArgs.passwordconfirmation = $NewUserCredentials.GetNetworkCredential().Password
        $esxcliResult = $esxcli.system.account.add.Invoke($esxcliArgs)

        $esxcliArgs = $esxcli.system.permission.set.CreateArgs()
        $esxcliArgs.id = $NewUserCredentials.UserName
        $esxcliArgs.role = "Admin"
        $esxcliResult = $esxcli.system.permission.set.Invoke($esxcliArgs)
    }

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



  • 6.  RE: User account creation error

    Posted Nov 09, 2025 02:05 PM

    Thank you so much @a_p_

    It works perfectly good. 

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