VMware Cloud Community
RajuVCP
Hot Shot
Hot Shot

Script to change ESXi host password via vCenter

Hi All,

Is there a way to change all of my ESXi passwords from vCenter, the conditions ares

1.i have to run powercli connect vCenter

2. Show the new password from source file

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
10 Replies
LucD
Leadership
Leadership

You will need to have the password for the root account on the ESXi nodes.

Then you could do something like this (I assume the password is, and will be the same, on all ESXi nodes).

$user = 'root'

$pswd = 'VMware1!'

$newPswd = 'VMware2!'

$esxServers = Get-VMHost

foreach($esx in $esxServers){

    $srv = Connect-VIServer -Server $esx.Name -User $user -Password $pswd 4> $null

    if($srv){

        Set-VMHostAccount -UserAccount $user -Password $newPswd -Server $srv -Confirm:$false

        Disconnect-VIServer -Server $esx.Name -Confirm:$false

    }

    else{

        Write-Host "Logon failed $($esx.Name)"

    }

}


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

RajuVCP
Hot Shot
Hot Shot

Thank you LucD for help always.

But the issue is , i dont have existing root password (old Infra)

new password for the host should not be unique, i will have to show them via txt or excel format,

script should pick new password from that file.

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
LucD
Leadership
Leadership

Picking the new password from a file is not really a problem.

But if you don't know the current password, I'm afraid you're stuck.

You can either reinstall ESXi or use an unsupported method to reset the root password (like for example the one in How to Reset the ESXi Root Password)


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

RajuVCP
Hot Shot
Hot Shot

LucD,

I came across this link , which says we can change root password via vCenter. But it is bit confusing at $VMHost.

https://www.linkedin.com/pulse/reset-esxi-root-password-through-vcenter-esxcli-method-buschhaus/

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
0 Kudos
LucD
Leadership
Leadership

And are your ESXi nodes 6.* ?
Btw, the content of $VMHost is just the output of Get-VMHost (while connected to a vCenter).

When the ESXi nodes are pre-6.* and you have HostProfiles, you could also try my function from Change The Root Password In Hosts And Host Profiles


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

0 Kudos
RajuVCP
Hot Shot
Hot Shot

They are 5.5 versoin...

So i believe this above script wont work for them. Smiley Sad

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
0 Kudos
LucD
Leadership
Leadership

Do you have HostProfile?


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

ShoHRock
Enthusiast
Enthusiast

Hi Experts, on similar lines, is there any way the script will pic data from a csv & then modify the root password which is unique for each host?

VMHost   OldPass   NewPass

ESX1     abc     abc1

ESX2     xyz     xyz1

ESX3     pqr     pqr1

=====================================

$esxServers = get-content "e:/sho/esxpass.csv"

foreach($esx in $esxServers){

$srv = Connect-VIServer -Server $esx.VMHost -User root -Password $esx.OldPass

if($srv)

{Set-VMHostAccount -UserAccount $user -Password $esx.OldPass -Server $srv -Confirm:$false

Disconnect-VIServer -Server $esx.VMHost -Confirm:$false}

Else {Write-Host "Logon failed $($esx.VMHost)"}}

=====================================

Something like this???

0 Kudos
LucD
Leadership
Leadership

Or just use the Import-Csv cmdlet

Import-Csv -Path 'e:/sho/esxpass.csv' -UseCulture |

ForEach-Object -Process {

    $srv = Connect-VIServer -Server $_.VMHost -User root -Password $_.OldPass

    if($srv){

        Set-VMHostAccount -UserAccount $user -Password $_.NewPass -Server $srv -Confirm:$false

        Disconnect-VIServer -Server $_.VMHost -Confirm:$false

    }

    else{

        Write-Host "Logon failed $($esx.VMHost)"

    }

}


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

0 Kudos
CIASM
Contributor
Contributor

# vCenter Server configuration
$vCenter = "192.168.19.254"
$user = "administrator@vsphere.local"
$password = "vCenter_password"
 
Write-Host "connect esxi & vCenter" -ForegroundColor Green
Connect-VIServer -Protocol https -User $user -Password $password -Server $vCenter
 
$newPassword = "YOU_esxi_password"
 
$VMhosts = Get-VMHost
Foreach ($VMhost in $VMhosts) {
# Specify the name or IP address of the ESXi host to obtain the EsxCli object, and the EsxCli version to use
    $esxcli = Get-EsxCli -VMhost $VMhost -v2 
 
    Write-Host "Changing esxi root password for host $VMhost" -ForegroundColor Green
    $esxcli.system.account.set.Invoke(@{id="root";password=$newPassword;passwordconfirmation=$newPassword})
}
0 Kudos