Validate Username / Password on ESX(i) Host

A simple script to validate the login credentials against an ESX(i) host. Useful when validating that the credentials are what you think they should be.

<#
 
.SYNOPSIS
Validates credentials by doing a login to the target host via the VI API.
 
.EXAMPLE
.\Test-VMHostAccountPassword.ps1 -VMHost "host1","host2","host3"
 
Get prompted for all credential information so it is not on the command line.
 
.EXAMPLE
"host1","host2","host3" | .\Test-VMHostAccountPassword.ps1
 
Use pipelining.
#>
 
PARAM 
(
    [parameter(Mandatory=$true,
    ValueFromPipeline=$true,
    HelpMessage="An array of VM host names.")]
    [String[]]$VMHost,
 
    [parameter(Mandatory=$false,
    HelpMessage="The credential used to connect to the host.")]
    [System.Management.Automation.PSCredential]$credential
)
 
BEGIN
{
    # Provide param prompting. This is a workaround for the multiple prompting that
    # happens if this were done in the PARAM block.
    if (!$credential)
    {
        $myCredential = Get-Credential
    }
    else
    {
        $myCredential = $credential
    }
}
 
PROCESS
{
    foreach ($h in $VMHost)
    {
        $connection = Connect-VIServer -Server $h -Credential $myCredential -ErrorAction SilentlyContinue
 
        $success = $false
        if ($connection)
        {
            $success = $true
            Disconnect-VIServer -Server $connection -Confirm:$false | Out-Null
        }
 
        "" | Select-Object @{Name="VMHost"; Expression={$h}}, @{Name="LoginSuccess"; Expression={$success}}
    }
}

Leave a Comment

Your email address will not be published.