VMware Cloud Community
firefox15
Contributor
Contributor

Running Invoke-VMScript With Multiple Credentials

Is it possible to run Invoke-VMScript with a list of credentials to try?  I have a script I'd like to run on all VMs in the vCenter, but we have a few VMs that are in our DMZ and therefore have different credentials.

I know this won't work since I can only specify the parameter once, but conceptually, something like this?

$DomainCreds = Get-Credential

$DMZCreds = Get-Credential

Invoke-VMScript -VM "TestVM" -ScriptText {sample script} -GuestCredential $DomainCreds -GuestCredential $DMZCreds

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership

Yes, you can only specify one credential per call.

But you can try multiple ones in a loop, and exit the loop when successful.

Something like this

$creds = @(

   @{

   User = 'domain\user1'

   Pswd = 'VMware1!'

   },

   @{

   User = 'domain\user2'

   Pswd = 'VMware1!'

   }

)


$vmName = 'MyVM'


foreach ($cred in $creds) {

   Write-Host "Trying with $($cred.User)"

   try {

   $result = Invoke-VMScript -VM $vmName -ScriptText '$PSVersionTable' -ScriptType Powershell -GuestUser $cred.User -GuestPassword $cred.Pswd -ErrorAction Stop

   $result.ScriptOutput

   break

   }

   catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin] {

   Write-Host "Invalid credential $($cred.User)"

   }

   catch {

   $out = "Other error for $($cred.User)"

   }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

firefox15
Contributor
Contributor

Fantastic, thank you.  I'll give this a shot.

Reply
0 Kudos
wutangdangie
Contributor
Contributor

This is working for me, except it does not exit when successful. Where in the script is the part that says, "Hey, if you succeed in logging in, stop trying creds"?

Thanks you so much for all that you do! 

Reply
0 Kudos
LucD
Leadership
Leadership

The break should exit from the loop.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

wutangdangie
Contributor
Contributor

Thank you so much for the prompt reply, and I 100% missed the 'break'.

Thanks! 

Reply
0 Kudos