Skip to content

Instantly share code, notes, and snippets.

@jbarber
Created November 25, 2010 11:30
Show Gist options
  • Save jbarber/715238 to your computer and use it in GitHub Desktop.
Save jbarber/715238 to your computer and use it in GitHub Desktop.
Add new local user to all the ESX servers managed by a vSphere vCenter
$vsphere = "my-vc"
$new_user = "newbie"
$new_user_passwd = "new_sekret"
$new_user_grp = "root"
$root_user = "root"
$root_passwd = "really_sekret"
# Get all of the ESX servers (connect using Windows credentials)
connect-viserver -server $vsphere
$hosts = get-view -type hostsystem
disconnect-viserver -confirm:$false
# For each ESX server, connect and see if the new account exists.
# If it does, reset the password and ensure the account is granted shell access.
# If it doesn't, create it and add to the root group (this seems to be necessary to allow ssh login in ESX4.0)
$hosts | %{ $_.name } | %{
echo $_
connect-viserver -server $_ -user $root_user -password $root_passwd
if ($?) {
if (! (get-vmhostaccount | ?{ $_.id -eq $new_user })) {
new-vmhostaccount -useraccount -id $new_user -password $new_user_passwd -grantshellaccess
set-vmhostaccount -groupaccount -id root -assignusers $new_user
}
else {
set-vmhostaccount -useraccount $new_user -password $new_user_passwd -grantshellaccess $true
}
disconnect-viserver -confirm:$false "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment