HI Guys,
sorry to be revisiting a thread that's been marked as correct already but, this looks like a really handy bit of code and i was trying (without success) to modify it so that
- you can give two parameters (username + password) and it will check all Hosts in a VC - you have this working already
- you can give 3 parameters (username, Password + Hostname) and it will check only that host (or list of hosts)
I haven't been able to figure out how to change the "Get-VMHost -PipelineVariable esx | ForEach-Object" line so that it checks for the $ESXiHost variable. If the variable has been passed, only check those hosts, if it has not been passed, check all Hosts.
I've written a second function what works for the 3 variable option but, having two functions to do basically the same thing seems redundant and i suspect PowerCLI should be able to do this in one ?
function Check-Credentials {
<#
.SYNOPSIS
Checks ESXi Host Credentials
.DESCRIPTION
Accepts Username,Password & Hostname parameters, attempts to login to the Host using provided credentials,
If only Username and Password are passed, attempts to login to all ESXi Hosts in the VC. Creates logfile with results
.EXAMPLE
Check-Credentials root password 'MyEsxiHost.mycorp.com'
.EXAMPLE
Check-Credentials root password
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, Position=0)]
[string] $user,
[Parameter(Mandatory=$true, Position=1)]
[string[]] $passwds,
[Parameter(Mandatory=$false, Position=2)]
[string[]] $ESXiHost
)
$Logfile = "C:\Temp\CredentialLog-" + (Get-Date -Format yyyy-MMM-dd-HHmm) + ".log"
Function LogWrite {
Param (
[string]$logstring
)
Add-content $Logfile -value $logstring
}
Get-VMHost -PipelineVariable esx | ForEach-Object -Process {
foreach ($passwd in $passwds){
try {
Connect-VIServer -Server $esx.Name -User $user -Password $passwd -ErrorAction Stop | Out-Null
LogWrite "Host $($esx.Name) password $passwd is correct"
Disconnect-VIServer -Server $esx.Name -Confirm:$false
}
catch {
LogWrite "Host $($esx.Name) password $passwd failed"
}
}
}
Invoke-Item $Logfile
}
Thanks in advance !