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
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
@{
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
Fantastic, thank you. I'll give this a shot.
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!
The break should exit from the loop.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you so much for the prompt reply, and I 100% missed the 'break'.
Thanks!