Skip navigation
VMware

This Question is Possibly Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
3,473 Views 29 Replies Last post: Sep 9, 2010 7:46 AM by gmjulian RSS
1 2 Previous Next
jasonrobinson Enthusiast 64 posts since
Oct 18, 2007
Currently Being Moderated

Oct 3, 2008 11:08 AM

Root password change

 

I have about 50 esx servers I need to change the root and one other additional local user password (lets say vmuser) pwds. This would save me tons of time if this could be automated with powershell. Does anyone know if this can be done? Thanks for your time

 

 

 

 

 

Jason

 

 

LucD Guru User Moderators vExpert 8,981 posts since
Oct 31, 2005
Currently Being Moderated
1. Oct 3, 2008 4:06 PM in response to: jasonrobinson
Re: Root password change

There has been some discussions on adding ESX host accounts recently (see  Add local users to multiple servers).

 

In that thread a problem with the CreateUser method and the shell field in the passwd file came to light.

To solve it the HostPosixAccountSpec object needs to be used.

 

For your question, this script should do what you require

Connect-VIServer -Server <VC-server>

$rootpswd = <root-password>

$accspec1 = New-Object VMware.Vim.HostPosixAccountSpec
$accspec1.id = "root"
$accspec1.password = <new-root-password>
$accspec1.shellAccess = "/bin/bash"

$accspec2 = New-Object VMware.Vim.HostPosixAccountSpec
$accspec2.id = "vmuser"
$accspec2.password = <new-vmuser-password>
$accspec2.shellAccess = "/bin/bash"

Get-VMHost | %{
  Connect-VIServer $_.Name -User root -Password $rootpswd
  $si = Get-View ServiceInstance
  $acctMgr = Get-View -Id $si.content.accountManager

  $acctMgr.UpdateUser($accspec1)
  $acctMgr.UpdateUser($accspec2)
}

 

You didn't state if these 50 ESX servers are all your ESX servers.

If not, the Get-VMHost will have to be replaced by something else.

Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
srinivasaddag… Enthusiast 114 posts since
Mar 13, 2007
Currently Being Moderated
2. Oct 15, 2008 12:50 PM in response to: LucD
Re: Root password change

 

Hi LucD,

 

 

 

 

 

I tried to use your script but get below error, could you please suggest?

 

 

You cannot call a method on a null-valued expression.

At line 9, position 73

$acctMgr = Get-View -Id $si.content.accountManager   $acctMgr.UpdateUser($accspec1)

 

 

Thanks

 

 

LucD Guru User Moderators vExpert 8,981 posts since
Oct 31, 2005
Currently Being Moderated
3. Oct 15, 2008 12:59 PM in response to: srinivasaddag…
Re: Root password change

Apparently the forum SW dropped some line feeds while you copied the script.

Try the attached version.

Attachments:
Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
srinivasaddag… Enthusiast 114 posts since
Mar 13, 2007
Currently Being Moderated
4. Oct 15, 2008 1:00 PM in response to: srinivasaddag…
Re: Root password change

 

Hi,

 

 

I could get rid-off  the null valued expression error below but now getting below even though the password got changed successfully. Any idea what this is related to ?

 

 

A parameter cannot be found that matches parameter name ''.

At line 9, position 20

$acctMgr = Get-View -Id $si.content.accountManager $acctMgr.UpdateUser($accspec1)

 

 

Thanks

 

 

srinivasaddag… Enthusiast 114 posts since
Mar 13, 2007
Currently Being Moderated
5. Oct 15, 2008 1:13 PM in response to: srinivasaddag…
Re: Root password change

 

sorry spoke too early I saw your latest response and the script actually worked wihtout any errors. When i viewed the code i couldn't really find anything changed except for the formatting. Do you mean that could create the difference in errors i was getting?

 

 

LucD Guru User Moderators vExpert 8,981 posts since
Oct 31, 2005
Currently Being Moderated
6. Oct 15, 2008 1:45 PM in response to: srinivasaddag…
Re: Root password change

Yes, the line in the message should in fact be 2 lines.

The forum SW, depending on the browser you're using, seems to have this strange behavior.

Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
srinivasaddag… Enthusiast 114 posts since
Mar 13, 2007
Currently Being Moderated
7. Oct 15, 2008 2:07 PM in response to: LucD
Re: Root password change

Cool..how do i secure the password i am typing?. I tried the steps suggested in few forums but couldn't succed. Actually get-credential worked but not sure how to assign it to $accspec1.password variable..any idea?

srinivasaddag… Enthusiast 114 posts since
Mar 13, 2007
Currently Being Moderated
8. Oct 15, 2008 2:46 PM in response to: srinivasaddag…
Re: Root password change

 

I got it...below is the simple code.

 

 

$newpswd= Get-Credential root

 

 

$accspec1 = New-Object VMware.Vim.HostPosixAccountSpec

 

 

$accspec1.Id ="root"

 

 

$accspec1.Password = $newrootpswd.GetNetworkCredential().Password

 

 

src1 Novice 21 posts since
Nov 18, 2008
Currently Being Moderated
9. Feb 6, 2009 6:30 PM in response to: srinivasaddag…
Re: Root password change

 

Hi

 

 

I have been using this script to change root password on multiple hosts. There is one problem, if there is an issue with any 1 host, the script terminates. I end up with only half the servers done and since the current root password becomes mismatched, I cannot run this anymore.

 

 

Is there a way to add error-check or something so the script skips any problem hosts and moves on changing the rest of the hosts? help appreciated

 

 

LucD Guru User Moderators vExpert 8,981 posts since
Oct 31, 2005
Currently Being Moderated
10. Feb 6, 2009 10:09 PM in response to: src1
Re: Root password change

What is the type of error you get when the script fails on a host ?

Could you perhaps include the error message and the script you're actually using ?

Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
src1 Novice 21 posts since
Nov 18, 2008
Currently Being Moderated
11. Feb 8, 2009 8:10 PM in response to: LucD
Re: Root password change

 

Script I am using is attached.

 

 

the script works perfectly fine if I have multiple hosts that have exact old password and am changing to new one.

 

 

Problem is when it is running through the 1...50 hosts and hiccups on say #33 because someone had changed the password to anything else other than what is listed as current root password in the above script. The script then fails with bad password on that host and does not continue to the rest of 34-50 hosts either. I want to be able to mark the one with the problem but still move ahead changing the pwd on rest of them.

 

 

Maybe before the script runs we could check password age on all hosts and make sure it is same? I am not sure how to do this.

 

 

Attachments:
LucD Guru User Moderators vExpert 8,981 posts since
Oct 31, 2005
Currently Being Moderated
12. Feb 9, 2009 3:00 AM in response to: src1
Re: Root password change

You can use the ErrorAction parameter to continue when a Connect-VIServer fails.

At the end the script dumps all errors so you can see on which hosts it failed.

$errReport =@()
$rootpswd = <root-password>

$accspec1 = New-Object VMware.Vim.HostPosixAccountSpec
$accspec1.id = <account>
$accspec1.password = <new-password>
$accspec1.shellAccess = "/bin/bash"

Get-VMHost | % {
     Connect-VIServer $_.Name -User root -Password $rootpswd -ErrorAction SilentlyContinue -ErrorVariable err
     $errReport += $err
     if($err.Count -eq 0){
       $si = Get-View ServiceInstance
       $acctMgr = Get-View -Id $si.content.accountManager 

       $acctMgr.UpdateUser($accspec1)
     }

     $errReport += $err
     $err = ""
}

$errReport

 

Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
_David Enthusiast 45 posts since
Aug 28, 2008
Currently Being Moderated
13. Feb 9, 2009 3:25 AM in response to: jasonrobinson
Re: Root password change

 

This can also be done with Koen Warsons ESX tasks

 

 

http://www.svmotion.com/

 

 

If you found this or any other answer useful please consider the use of the Helpful or correct buttons to award points
src1 Novice 21 posts since
Nov 18, 2008
Currently Being Moderated
14. Feb 9, 2009 6:28 AM in response to: LucD
Re: Root password change

 

yes, this works. thanks so much.

 

 

will also checkout the svmotion script. thanks again

 

 

Bookmarked By (0)

Share This Page

Communities