Skip to content

Instantly share code, notes, and snippets.

@phyoewaipaing
Last active September 10, 2023 06:06
Show Gist options
  • Save phyoewaipaing/b118f7b843a062fbe6036b23b6556334 to your computer and use it in GitHub Desktop.
Save phyoewaipaing/b118f7b843a062fbe6036b23b6556334 to your computer and use it in GitHub Desktop.
Script to verify Esxi credentials on multiple Esxi hosts
<#
.SYNOPSIS
The script that will verify the given credentials on multiple VMware hosts.
.DESCRIPTION
This script will verify the given Esxi credentials on multiple Esxi hosts. It works by logging in each esxi host by port 443(TCP) with the given credentials and disconnect the esxi host once the connection is succeed, which in turn means the given credentials are correct.
To avoid the powershell modules (PoshRSJob and VMware Powercli) errors, it is recommend not to run against more than 100 Esxi servers at a time. And, sometimes you might see the following warning if you run more than 50 esxi hosts on each script run, but it doesn't impact the operation or the accuracy of the output.
WARNING: The process cannot access the file 'C:\Users\****\AppData\Roaming\VMware\PowerCLI\RecentServerList.xml' because it is being used by another process.
These following powershell modules are needed to run the script. If not, install it with Install-Module -Name <ModuleName> -Confirm:$false
- PoshRSJob
- VMware.VimAutomation.Core
Example usage:
.\Test_Esxi_Credentials_on_Multiple_Esxi_Hosts.ps1 -VMHostListFile vmhostfilelist.txt
This will verify the credentials of each esxi host in vmhostfilelist.txt file and output the result.
Author: Phyoe Wai Paing
Country: Myanmar(Burma)
Initial Released: 28.July.2021
Changed Log : v1.0 : 28.July.2021 : Initial Released
Tested and Verfied on: Esxi 6.7, PowerCLI 12.2.0, Powershell 5
.EXAMPLE
.\Test_Esxi_Credentials_on_Multiple_Esxi_Hosts.ps1 -VMHostListFile vmhostfilelist.txt
This will make the connection to each esxi host in vmhostfilelist.txt file and output the result.
.PARAMETER VMHostListFile
The file which contains the hostnames or IP addresses of esxi hosts
.LINK
You can find this script and more at: https://www.sysadminplus.blogspot.com/
#>
param([Parameter(Position=0,Mandatory=$true)][string]$VMHostListFile)
## Notify user to install PowerCLI Core module if not already installed. Then exit script ##
If (!((Get-Module -Name VMware.VimAutomation.Core -ListAvailable).Name))
{
Write-host -fore yellow "VMware.VimAutomation.Core module is not available. Please install it by typing Install-Module VMware.VimAutomation.Core in elevated powershell. Script exits." ;
Exit;
}
## Notify user to install PoshRSJob module if not already installed. Then exit script ##
If (!((Get-Module -Name poshrsjob -ListAvailable).Name))
{
Write-host -fore yellow "PoshRsJob module is not available. Please install it by typing Install-Module PoshRSJob in elevated powershell. Script exits." ;
Exit;
}
## Exit the script if the credential is not inputted ##
Try {
$cred = get-credential -EA Stop;
}
catch {
write-host -fore yellow "You will need to enter the esxi credential to continue. Script exits."
Exit;
}
$JobDelayCounter = 0; ## The incremental counter which will be multiple of 25ms (duration) before accessing the RecentServerList.xml file (to avoid the file lock)
## Action starts here: loop each esxi host, create PoshRSJob, gives the output and delete the finished jobs ##
Get-content -Path $VMHostListFile | % {
$IP=$_;
[array]$RsJob += Start-RSJob -ScriptBlock {
param($IP,$cred,$JobDelayCounter)
$WaitForFileLock = 1;
while ( ($WaitForFileLock -ne 0) -AND ($WaitForFileLock -ne 3) ) ## We will continue the loop until the file is locked or until the Connect-VIserver gives error up to 3 retries ##
{
Start-sleep -milliseconds ($JobDelayCounter * 250)
Try {
[IO.File]::OpenWrite("$env:APPDATA\VMware\PowerCLI\RecentServerList.xml").close(); ## Check if RecentServerList.xml is locked or not. If locked, then wait with random duration (defined in catch statement)
Try
{
#"Going to access the file for $IP at $((get-date).ToString("hh:mm:ss:fff"))" ## Comment this out for debugging purpose
Connect-VIServer -Server $IP -Credential $cred -EA Stop -WarningAction SilentlyContinue | Out-Null;
#"Password is correct: $IP" ## Comment this out for debugging purpose
$Obj = New-Object -TypeName PsObject -Property @{"EsxiIP" = $IP;Status = "[OK] Credential Correct" }
$Obj;
$WaitForFileLock = 0;
}
catch
{
If ($_.exception.message -match "There was no endpoint listening at")
{
#"Cannot connect to host by port 443: $IP"
$Obj = New-Object -TypeName PsObject -Property @{"EsxiIP" = $IP;Status = "[Error] Cannot connect by port 443" }
$Obj;
$WaitForFileLock = 0;
}
elseif ($_.exception.message -match "incorrect user name or password")
{
#"Incorrect Credentials: $IP"
$Obj = New-Object -TypeName PsObject -Property @{"EsxiIP" = $IP;Status = "[Error] Incorrect Credentials" }
$Obj;
$WaitForFileLock = 0;
}
else
{
If ($WaitForFileLock -eq 3)
{
$Obj = New-Object -TypeName PsObject -Property @{"EsxiIP" = $IP;Status = "[Errx] $($_.exception.Message)" }
$Obj;
}
$WaitForFileLock++; ## We will increment the counter to stay in while loop until the counter is 3, meaning that the Connect-VIserver will try up to 3 times even it encounters the errors
}
};
}
catch ## If the RecentServerList.xml file is locked, then wait for 1100ms to 3900ms ##
{
$RandomMilliseconds = get-random @(100,200,300,400,500,600,700,800,900);
$RandomSeconds = get-random @(1,2,3);
$RandomDuration = ($RandomSeconds * 1000) + $RandomMilliseconds
# "File is locked for $IP ................................. Waiting $RandomMilliseconds milliseconds" ## Comment this out for debugging purpose
Start-Sleep -Milliseconds $RandomDuration; ## Random sleep between 1100 and 2400 milliseconds
$WaitForFileLock = 1;
}
}
} -ArgumentList $IP,$cred,$JobDelayCounter
$JobDelayCounter++;
}
## Receive and Remove the completed Jobs ##
do
{
Start-Sleep 3;
$RsJobs = Get-RSJob | ? { $_.State -eq "Completed" };
$RsJobs | Receive-RsJob;
$RsJobs | Remove-RSJob;
Start-Sleep 1;
} while ((Get-RSJob).Count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment