Kasraeian

vWorld v.3

Using VMware PowerCLI (#002) – User Accounts

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.

[wpdm_file id=4]

#####################################################
Write-Host &quot;Name:<code>tAdd/Remove User Accounts Script
By:</code>t<code>tSohrab Kasraeian Fard (@Kasraeian)
Ver:</code>t`t0.1&quot;
#####################################################

<h1>0. Enabling multiple connection</h1>

$PCC = Get-PowerCLIConfiguration
if ($test.DefaultVIServerMode -eq &quot;single&quot;) {
    Set-PowerCLIConfiguration -DefaultVIServerMode Multiple
}

<h1>1. vCenter Server Connection</h1>

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

<h1>2. Hosts connection</h1>

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

<h1>Main Body</h1>

ListUser
Menu

<h1>Listing Available Users On All Hosts</h1>

function ListUser {
    foreach ($VMHost in $VMHosts) {
        $VMHA = Get-VMHostAccount -Server $VMHost.Name
        Write-Host &quot;---------- &quot; $VMHA.Count &quot; users found on host <code>&amp;quot;$VMHost</code>&quot; ----------&quot;
        $i = 1
        foreach ($HA in $VMHA) { Write-Host &quot;Name&quot;$i&quot;: &quot; $HA.Name; $i++ }
        Write-Host &quot;&quot; }
}

<h1>Main Menu</h1>

function Menu {
    $FAns = Read-Host (&quot;[A] Add User<code>n[R] Remove User</code>n[Other keys] Exit`nPlease specify the action&quot;)
    switch ($FAns) {
    A {AddUser{}}
    R {RemoveUser{}}
    default {Quit{}}
    }
}

<h1>Refresh Process</h1>

function Refresh {
    cls
    ListUser
    Menu
}

<h1>Adding User To All Hosts</h1>

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

<h1>Removing User From All Hosts</h1>

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

<h1>Quiting</h1>

function Quit {
    Write-Host &quot;Thanks for your uasge.<code>nId be glad to receive your feedback/comment on sohrab@kasraeian.com</code>n`n&quot;
    Disconnect-VIServer -Server * -Confirm:$true
}

It's your kindness to leave a reply/feedback

Kasraeian © 2016
%d bloggers like this: