Hi,
I'm trying this to connect to vCenter, however would like to validate the $creds first before even executing the below try/catch.
Would like to validate $creds, if the credentials are incorrectly typed, blank or the prompt window for credentials is accidentally closed by the end user. If any of this is true, the script should EXIT.
And if the credentials are right, the vCenter Connection try/catch should get executed next.
As of now, the below code is throwing error: "Invalid/Incomplete Credentials. Please verify." even though the credentials are correct, looks like it's not coming out of the first try/catch block.
$creds = Get-Credential -Message "Enter your credentials for $vCenter"
try {
#This will help validate if the credentials entered are valid if it can read any other random user account (this will be embedded in the script, no user input is required here).
Get-ADUser -identity somerandomADuser -credential $creds -ErrorAction SilentlyContinue
}
catch {
$Error1 = $Error[0].Exception.Message
Write-Host "Invalid/Incomplete Credentials. Please verify."
Exit
}
try {
$Conn = Connect-VIServer $vCenter -Credential $creds -ErrorAction SilentlyContinue
}
catch {
$Error2 = $Error[0].Exception.Message
Write-Host "Connection failed to $vCenter. Please recheck the vCenter name/verify the network connectivity"
Exit
}
if($Conn)
{
#primary script code goes here
}
Afaik, you can't enter a blank username (at least not in PowerShell 5.*).
You can test if there was a blank password by checking if $creds.Password.Length -eq 0
But that is all I think.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
We can ignore the blank credentials case, as its not valid.
I'm still trying to understand why the script is not executing second try/catch even when the credentials are valid.
Shouldn't the ErrorAction in the Try blocks be Stop instead of SilentlyContinue?
Otherwise you will never get into the Catch block except for terminating exceptions.
Does a run produce any error messages?
Can you try adding the -Verbose switch on the cmdlets in the Try blocks?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Evening.
This is the code block I've used to validate credentials before moving on. It's a different approach, but perhaps it would work for you.
Best,
Scott
### get Domain Admin credentials and validate them before moving on
$i=0
do {
do {
$cred=$host.ui.PromptForCredential("Enter Domain Admin credentials", "Please enter your Domain Admin user (domainname\username) and password for domain: '$domain'", "", "")
}until ($cred.username.contains("\"))
add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext $ct,$Domain
$i+=1
if ($i -eq 3){
write-host "You've entered your credentials incorrectly three times. This script will now exit to prevent you from locking your account out."
exit
}
#$i
} until ($pc.ValidateCredentials($(if($cred.UserName.Contains("\")) {@($cred.username.Split("\"))[1]}else{$cred.UserName}),$($cred.GetNetworkCredential().password)))
Thanks. Will try this one.
Appreciate that.
Regards.
