I have a script that does:
Connect-VIServer -server $server ` -User domain\username ` -Password my-password
Doesn't work (with or without the ` marks). I get this:
Connect-VIServer : Login failed due to a bad username or password.
At E:\SCM_Virt_vSphereGetHostNICsXml.ps1:17 char:17
+ Connect-VIServer <<<< $server -User domain\username -Password my-password
+ CategoryInfo : NotSpecified: (:) [Connect-VIServer], InvalidLog
inException
+ FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_Reconnect_SoapE
xception,VMware.VimAutomation.Commands.ConnectVIServer
If I comment out the -User -Password parameters so that I just have Connect-VIServer -server $server it will prompt me for credentials. And when I enter them it connects fine.
Why would it not like the User/Password hard coded in the script, but works fine if I enter the exact same credentials?
Outside of the script, on the PowerCLI Command Line I get nearly the same results. If I supply username/password then I get the error. If I don't then it just grabs my user credential and connects right away.
This is a cross domain connection. The box I am on is in one domain and the vCenter instance is in another domain. So supplying the domain\username is essential. Not sure if that matters.
For clarity, here is another attempt in a different way with the same results
...Comment out the -Credential switch and it will prompt, enter the SAME credentials and it works.
...Use the -Credential switch and it fails with invalid username/password
Script contents:
param ( [Parameter(Position=0, Mandatory=$true)] [string] $server ) $UserName = "domain\username" $SecurePass = ConvertTo-SecureString "my$password" -asplaintext -force $Credential = New-Object System.Management.Automation.PSCredential $UserName, $SecurePass Connect-VIServer -server $server -Credential $Credential
Try leaving out the domain part.
$UserName = "username" Connect-ViServer -server $server -user $UserName -password "Your_passwsord"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Nope, that's not it either.
But I found it. You have to escape characters in strings (fun learning TWO languages at once)
So for a username like "domain\password" you need to supply "domain`\password".
and for a password like "pa$$word" you need "pa`$`$word".
I agree for the "pa$$word" string in double quotes.
PS will try to resolve variables (who all start with a $-sign).
Btw you don't have this when you use single quotes, like 'pa$$word'.
But the first one with the back-slash seems strange.
I don't think that PS is doing that.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That may be. I was able to switch to an admin account with a slash in it so, moot. But when I tried it with a domain\user account with the ` it worked. May not be needed but it works.
Thanks for the single tick tip too. Beats marking up your strings. Not that I'm going to leave scripts with hard coded usernames and passwords in them!