The exact command I'm trying to run unattended is:
Invoke-command -Session $sessionname { Connect-VIServer vcenterserver.asdf.com }
However, I keep getting prompted for credentials even if I am running the script as a user with administrator rights on vCenter. I think I can use the -User and -Password parameter (as a secure string stored in a file), but I really think passthru credentials should work here, right?
just add your credentials one of two ways.
If it is not accepting your current credentials you can add -verbose to the end of your connect-viserver command to try to see where it is failing...
1) change the connect-viserver line to the following: Connect-VIServer vcenterserver.asdf.com -user <username> -password <password>
2) store your credentials in PowerCLI with the following: New-VICredentialStoreItem -Host vCenter01 -User <username> -password <password>
Hope this helps
Thanks, Brian. That's helpful. Adding the verbose parameter gave me some input that for some reason my admin account I'm running the script as is failing, but if I put our service acct credentials in, it eventually works.
I fired up the thick client as my same admin account I was getting access denied under, and it opened vCenter just fine...
My goal is to not have to hard code credentials into the script, as it could be read/used by multiple people.
Ok well it looks like there have got to be some issues with your account... how do you run this script? can anyone run it?
I had three of my coworkers try that same invoke-command one-liner and each got the same credential pop up. I tried running New-VICredentialStoreItem -Host vCenter01 -User Admin -Password pass to store my credentials in the credential store, but then realized it's the server defined in the $sessionname variable that is causing the prompt.
What is most bizarre is that when I execute the prior invoke-command one line up to load the pssnapin for VMware.VimAutomation.Core everything works fine within that same -session $sessionname
Thoughts?
Found solution that worked for me. You need to add the -credential behind connect-viserver $using:cred is the special format if in invoke-command
$SecurePassword = $env:serviceaccount | ConvertTo-SecureString -AsPlainText -Force
$User = "serviceaccount"
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $User, $SecurePassword
Invoke-Command -ComputerName $env:COMPUTER -ScriptBlock { connect-viserver -credential $using:cred }