VMware Cloud Community
zatara
Enthusiast
Enthusiast
Jump to solution

Trying to update all hosts root password from an external txt file

Hello,

I tried to get assistance before, but my post disappeared. I was wondering if someone can help me revise my script to work correctly? Please don't delete my post. 

Here is what I've been trying to get work but I'm not doing something right:

(By the way, I gave the hosts.txt file full permissions for anything trying to access it)

If someone could help me get this working, I'd appreciate it greatly. Thank you. I've spent hours trying to get it right, but I'm just not great at PowerShell or scripting in general. Thank you.

# Import the VMware PowerCLI module
Import-Module VMware.PowerCLI

# Set the old and new passwords
$oldPassword = "oldpassword"
$newPassword = "newpassword"

# Set the path to the file containing the list of ESXi hosts
$hostListFile = "C:\hosts.txt"

# Authenticate to the vCenter
Connect-VIServer -Server vcenter.example.com -User administrator -Password password

# Read the list of ESXi hosts from the file
$hosts = Get-Content $hostListFile

# Loop through each host and update the root password
foreach ($esxiHost in $hosts) {
    Write-Host "Updating password for host $esxiHost"
    $esxcli = Get-EsxCli -VMHost $esxiHost -V2
    $esxcli.system.account.set($null, $newPassword, $oldPassword)
}

# Disconnect from the vCenter
Disconnect-VIServer -Server vcenter.example.com -Confirm:$false

 

Labels (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

When you use Get-EsxCli with the V2 switch, you will have to use the Invoke method.
And the parameters should be passed in a hash table.

Something like this

$esxcli = Get-EsxCli -VMHost $esxiHost -V2
$esxcli.system.account.set.invoke(@{id='root';password=$newPassword;passwordconfirmation=$newPassword})


 


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

View solution in original post

5 Replies
zatara
Enthusiast
Enthusiast
Jump to solution

Hi,

I'm trying to put together a PowerShell Script that will update all the root passwords on all ESXi. The script should have a variable for the old password and the new password. All the host names should come from a list oh ESXi hosts in an external file.  I tried to write this particular script numerous times but just I kept running into error after error. *note the hosts are combined in different regional datacenters, but are all connected in enhanced linked mode via vCenter, so I'm thinking if the script authenticates to vcenter first, it should have no issue contacting any ESXi hosts. I'm looking to have my script update around 300 ESXi hosts as fast as possible. My last script took me a week or so to finish everything, but I realize it was a very poor way to go about it. 

I hate to ask someone to write me a script, but can anyone provide me a script that will take care of what I'm describing? I would appreciate it greatly! Thank you so much. I look forward to testing whatever is sent to me. Cheers! 🙂

0 Kudos
a_p_
Leadership
Leadership
Jump to solution

Your first post has - for whatever reason - been marked as spam.
I've now restored it, and attached it to this thread, as it contains information that may be helpful.

André

LucD
Leadership
Leadership
Jump to solution

When you use Get-EsxCli with the V2 switch, you will have to use the Invoke method.
And the parameters should be passed in a hash table.

Something like this

$esxcli = Get-EsxCli -VMHost $esxiHost -V2
$esxcli.system.account.set.invoke(@{id='root';password=$newPassword;passwordconfirmation=$newPassword})


 


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

zatara
Enthusiast
Enthusiast
Jump to solution

Thank you very much. I'll give that a shot and report back how it works! Cheers!

0 Kudos
zatara
Enthusiast
Enthusiast
Jump to solution

That worked great! Thank you very much for your corrections to the script! Cheers.

0 Kudos