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
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
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! 🙂
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é
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
Thank you very much. I'll give that a shot and report back how it works! Cheers!
That worked great! Thank you very much for your corrections to the script! Cheers.