As part of my studying, in this post I’d post one little scripts which I made so far for working with user accounts on “VMware ESXi” hosts.
This script will add or remove user account from all hosts connected to one “VMware vCenter” server, in next version I’d add selecting function so user can be added/removed from specific data center, cluster or just single host.

I’m a beginner so this script might have (sure have) some problems and I’d be really grateful if you could help me to complete it better by posting your comment and feedback and I hope you find it useful.

You can copy/paste bellow code to “.ps1″ file and run it using “VMware vSphere PowerCLI” or simply download it.

Download176 downloads

#####################################################
Write-Host "Name:`tAdd/Remove User Accounts Script
By:`t`tSohrab Kasraeian Fard (@Kasraeian)
Ver:`t`t0.1"
#####################################################
# 0. Enabling multiple connection
$PCC = Get-PowerCLIConfiguration
if ($test.DefaultVIServerMode -eq "single") {
	Set-PowerCLIConfiguration -DefaultVIServerMode Multiple
}

# 1. vCenter Server Connection
Write-Host ""
$Target = Read-Host ("Which vCenter Do you want to connect")
if ($global:DefaultVIServer.Name -eq $Target) {
	Write-Host "You already connected to this vCenter"
} else {
	$TCred = Get-Credential
	Connect-VIServer -Server $Target -Credential $TCred
}

# 2. Hosts connection
if ($global:DefaultVIServer.Name -eq $Target) {
	Write-Host ("`nPlease provide user credential for host level access")
	$VMHCred = Get-Credential
	$VMHosts = Get-VMHost | where {$_.PowerState -eq "PoweredOn"} | sort-Object Name
	foreach ($VMHost in $VMHosts) {
		Connect-VIServer -Server $VMHost -Credential $VMHCred
	}
	cls
	Write-Host ""
} else {
	Write-Host "Error on connecting to vCenter server ($Target)"
}

# Main Body
ListUser
Menu

# Listing Available Users On All Hosts
function ListUser {
	foreach ($VMHost in $VMHosts) {
		$VMHA = Get-VMHostAccount -Server $VMHost.Name
		Write-Host "---------- " $VMHA.Count " users found on host `"$VMHost`" ----------"
		$i = 1
		foreach ($HA in $VMHA) { Write-Host "Name"$i": " $HA.Name; $i++ }
		Write-Host "" }
}

# Main Menu
function Menu {
	$FAns = Read-Host ("[A] Add User`n[R] Remove User`n[Other keys] Exit`nPlease specify the action")
	switch ($FAns) {
	A {AddUser{}}
	R {RemoveUser{}}
	default {Quit{}}
	}
}

# Refresh Process
function Refresh {
	cls
	ListUser
	Menu
}

# Adding User To All Hosts
function AddUser {
	$NUID = Read-Host ("Please enter new username")
	$NUPS = Read-Host ("Please enter password for $NUID")
	$SAAns = Read-Host ("Do you want this user to have shell access (Y/N)")
	foreach ($VMHost in $VMHosts) {
		Write-Host "Adding $NUID user account to $VMHost"
		if ($SAAns -eq "Y") {
			New-VMHostAccount -Id $NUID -Password $NUPS -GrantShellAccess:$true -Server $VMHost.Name }
		elseif ($SAAns -eq "N") {
			New-VMHostAccount -Id $NUID -Password $NUPS -GrantShellAccess:$false -Server $VMHost.Name }
	}
	Refresh
}

# Removing User From All Hosts
function RemoveUser {
	$TUID = Read-Host ("Please enter target username")
	$TURC = Read-Host ("Do you confirm you want to remove $TUID from all hosts (Y/N)")
	if ($TURC -eq "Y") {
		foreach ($VMHost in $VMHosts) {
		Get-VMHostAccount -Server $VMHost.Name -Id $TUID | Remove-VMHostAccount -Confirm:$true }
	Refresh
	}
}

# Quiting
function Quit {
	Write-Host "Thanks for your uasge.`nId be glad to receive your feedback/comment on [email protected]`n`n"
	Disconnect-VIServer -Server * -Confirm:$true
}