Sureshadmin
Contributor
Contributor

Need a powershell script to create a local user on esx box from given list of esx servers

Jump to solution

hi,

I need a powershell script to create a local user on esx from a given list of esx servers input via a csv sheet. The script should prompt me the username and the password for the username to be created on the given list of esx servers.

Thanks in advance!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Just noticed that there is a blank missing after the -Password parameter.

It should be

$userName = Read-Host "Username"
$password = Read-Host "Password" -AsSecureString:$true
Import-Csv "C:\vmhosts.csv" | %{
	Connect-VIServer -Server $_.Hostname -Credential (Get-Credential)
	New-VMHostAccount  -Id $userName -Password $password -UserAccount:$true -GrantShellAccess:$true -Confirm:$false
	Disconnect-VIServer -Confirm:$false
}

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

0 Kudos
19 Replies
LucD
Leadership
Leadership

Try this

$userName = Read-Host "Username"
$password = Read-Host "Password" -AsSecureString:$true
Import-Csv "C:\vmhosts.csv" | %{
	Connect-VIServer -Server $_.Hostname -Credential (Get-Credential)
	New-VMHostAccount  -Id $userName -Password $password-UserAccount:$true -GrantShellAccess:$true -Confirm:$false
	Disconnect-VIServer -Confirm:$false
}

You need to be connected to the ESX server to define a new user.

The Connect-VIServer prompts for the server credentials via the Get-Credential cmdlet.

____________

Blog: LucD notes

Twitter: lucd22


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

Sureshadmin
Contributor
Contributor

Luc,

When i give credentials of the esx server i get this error,

Cannot validate argument on parameter 'Server'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At :line:4 char:25

+ Connect-VIServer -Server <<<< $_.Hostname -Credential (Get-Credential)

I have the vmhosts.csv file in the specified path and it has one esx server name in it.

0 Kudos
LucD
Leadership
Leadership

Do you have a header row in the CSV file ?

Like

Hostname
server1

The script assumes there is a header row.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Sureshadmin
Contributor
Contributor

Luc,

I had a header row but it was named "name". Now i changed it to "Hostname" and then executed the script, but got this error,

6/8/2010 3:21:56 PM New-VMHostAccount 521a1d92-e83b-18f4-10b8-39a8aa44f0d6 User name or password has an invalid format.

At :line:5 char:18

+ New-VMHostAccount <<<< -Id $userName -Password $password-UserAccount:$true -GrantShellAccess:$true -Confirm:$false

am i doing something wrong?

0 Kudos
LucD
Leadership
Leadership

ESX has some password complexity rules.

Can you create an ESX manually with that password ?

Could be that ESX doesn't accept the password.

____________

Blog: LucD notes

Twitter: lucd22


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

Sureshadmin
Contributor
Contributor

Luc,

I successfully created that user name manually,

[root@xxxxxxxxxxxx /]# useradd temp
[root@xxxxxxxxxxxx /]# passwd temp
Changing password for user temp.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

0 Kudos
LucD
Leadership
Leadership

Just noticed that there is a blank missing after the -Password parameter.

It should be

$userName = Read-Host "Username"
$password = Read-Host "Password" -AsSecureString:$true
Import-Csv "C:\vmhosts.csv" | %{
	Connect-VIServer -Server $_.Hostname -Credential (Get-Credential)
	New-VMHostAccount  -Id $userName -Password $password -UserAccount:$true -GrantShellAccess:$true -Confirm:$false
	Disconnect-VIServer -Confirm:$false
}

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Sureshadmin
Contributor
Contributor

Luc,

It works perfect now and saves lot of time instead of logging into each box. Thanks a lot.

0 Kudos
GabrielTubbs
Contributor
Contributor

Lucd -

any chance you could tweak this script to change a pre-existing local ESX accounts password in a automated fashion...? (Meaning, you don't have to manually still type in old and new password when it connects to each ESX host?

0 Kudos
LucD
Leadership
Leadership

I assume you want to change the password for the same account with which you connect to the servers.

Then you could do something like this

$userName = "MyAccount" 
$oldPassword
= "OldPassword"
$newPassword
= "NewPassword"
Import-Csv
"C:\vmhosts.csv" | %{      Connect-VIServer -Server $_.Hostname -User $userName -Password $oldPassword
    
Get-VMHostAccount -Id $userName | Set-VMHostAccount -Password $newPassword
    
Disconnect-VIServer -Confirm:$false
}

If you don't want to store the passwords in clear text in the script, then you could use something like Hal's Export-PSCredential and Import-PSCredential functions.


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

0 Kudos
avlieshout
VMware Employee
VMware Employee

Have a look at

http://www.van-lieshout.com/2009/02/bulk-change-your-esx-root-password/

To get you started. It's an older post, so it might need some tweaking.

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
sachink0312
Enthusiast
Enthusiast

Hi Luc ,

I need script to create user account on multiple ESX servers with root permissions. Could you please help me for this?

Sachin

Sachin
0 Kudos
LucD
Leadership
Leadership

Instead of the Get- Set- construction, you can use the New-VMHostACcount cmdlet.

Make sure to use assign the correct group to the new account, with the AssignGroups parameter.


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

0 Kudos
sachink0312
Enthusiast
Enthusiast

how can i modify above script with this requirement. Also i want to create user with specific user name and passowrd. And also i want to reset the password of user if same user name is already exists. can you suggest the script for all this work to be automated.

Sachin
0 Kudos
LucD
Leadership
Leadership

Try something like this

$newUser = "NewUserAccount" 
$newPassword = "SomePassword"

Import-Csv "C:\hosts.csv" | %{      $srv = Connect-VIServer -Server $_.Hostname -User 'root' -Password 'rootpassword'
     $account = Get-VMHostAccount -Id $newUser -ErrorAction SilentlyContinue
    
if($account){         Set-VMHostAccount -UserAccount $account -Password $newPassword         }      else{         New-VMHostAccount -Id $newUser -Password $newPassword -GrantShellAccess:$true `
        
-AssignGroups 'root' -Description 'Test user creation' -UserAccount -Server $srv
     }     
Disconnect-VIServer -Server $srv -Confirm:$false
}


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

0 Kudos
sachink0312
Enthusiast
Enthusiast

you are genius Luc.... Thanks a lot for the help

Sachin
0 Kudos
sachink0312
Enthusiast
Enthusiast

i tried this script it works great but one thing i noticed that it doesnt add existing user in root group.

Sachin
0 Kudos
sachink0312
Enthusiast
Enthusiast

Hi Luc,

can u please help me with this, i want to add existing user in root group.

Sachin
0 Kudos
LucD
Leadership
Leadership

Change this line

     Set-VMHostAccount -UserAccount $account -Password $newPassword

into this

     Set-VMHostAccount -UserAccount $account -Password $newPassword -AssignGroups 'root'


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

0 Kudos