Hi
Can anyone help me on this code. The scenario as follows...
1. Need to Invoke some query on multiple VMs(Windows or Non windows) (On this script I executed on windows machine to grab driver information)
2. All VMs doesn't have same password but password may vary between three password which I used in my environment.
In this script I use try catch block multiple time to invoke cmd into the vm with correct password.
My question :
Is this is the correct way of using try/catch method multiple times or we can write any other simpler form b/c based on this I need to build other script.
$vm=Get-VM 'testvm1'
if (Get-VMguest -VM $vm)
{
$script = @"
driverquery
"@
try{
$out1 = Invoke-VMScript -VM $vm -GuestUser "administrator" -GuestPassword "password@1" `
-ScriptText $script -ScriptType bat -ErrorAction Stop |
Select -ExpandProperty ScriptOutput
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out1 = "Invalid logon"
}
catch{
$out1 = "any other output"
}
try{
$out2 = Invoke-VMScript -VM $vm -GuestUser "administrator" -GuestPassword "password@12" `
-ScriptText $script -ScriptType bat -ErrorAction Stop |
Select -ExpandProperty ScriptOutput
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out2 = "Invalid logon"
}
catch{
$out2 = "any other output"
}
try{
$out3 = Invoke-VMScript -VM $vm -GuestUser "administrator" -GuestPassword "password@123" `
-ScriptText $script -ScriptType bat -ErrorAction Stop |
Select -ExpandProperty ScriptOutput
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out3 = "Invalid logon"
}
catch{
$out3 = "any other output"
}
Write-Output "VM: $($vm.Name): $out1"
Write-Output "VM: $($vm.Name): $out2"
Write-Output "VM: $($vm.Name): $out3"
}
That looks ok.
Nesting of Try-Catch is allowed, but not considered good coding practice.
I would just add a flag that avoids entering Tr-Catch blocks when a previous one already succeeded.
Something like this
$script = @"
drivererquery
"@
Get-VM 'testvm1'
if(Get-VMguest -VM $vm)
{
$found = $false
try{
$out1 = Invoke-VMScript -VM $vm -GuestUser "administrator" -GuestPassword "password@1" `
-ScriptText $script -ScriptType bat -ErrorAction Stop |
Select -ExpandProperty ScriptOutput
$found = $true
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out1 = "Invalid logon"
}
catch{
$out1 = "any other output"
}
if(!$found){
try{
$out2 = Invoke-VMScript -VM $vm -GuestUser "administrator" -GuestPassword "password@12" `
-ScriptText $script -ScriptType bat -ErrorAction Stop |
Select -ExpandProperty ScriptOutput
$found = $true
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out2 = "Invalid logon"
}
catch{
$out2 = "any other output"
}
}
if(!$found){
try{
$out3 = Invoke-VMScript -VM $vm -GuestUser "administrator" -GuestPassword "password@123" `
-ScriptText $script -ScriptType bat -ErrorAction Stop |
Select -ExpandProperty ScriptOutput
$found = $true
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out3 = "Invalid logon"
}
catch{
$out3 = "any other output"
}
}
Write-Output "VM: $($vm.Name): $found"
Write-Output "VM: $($vm.Name): $out1"
Write-Output "VM: $($vm.Name): $out2"
Write-Output "VM: $($vm.Name): $out3"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That looks ok.
Nesting of Try-Catch is allowed, but not considered good coding practice.
I would just add a flag that avoids entering Tr-Catch blocks when a previous one already succeeded.
Something like this
$script = @"
drivererquery
"@
Get-VM 'testvm1'
if(Get-VMguest -VM $vm)
{
$found = $false
try{
$out1 = Invoke-VMScript -VM $vm -GuestUser "administrator" -GuestPassword "password@1" `
-ScriptText $script -ScriptType bat -ErrorAction Stop |
Select -ExpandProperty ScriptOutput
$found = $true
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out1 = "Invalid logon"
}
catch{
$out1 = "any other output"
}
if(!$found){
try{
$out2 = Invoke-VMScript -VM $vm -GuestUser "administrator" -GuestPassword "password@12" `
-ScriptText $script -ScriptType bat -ErrorAction Stop |
Select -ExpandProperty ScriptOutput
$found = $true
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out2 = "Invalid logon"
}
catch{
$out2 = "any other output"
}
}
if(!$found){
try{
$out3 = Invoke-VMScript -VM $vm -GuestUser "administrator" -GuestPassword "password@123" `
-ScriptText $script -ScriptType bat -ErrorAction Stop |
Select -ExpandProperty ScriptOutput
$found = $true
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out3 = "Invalid logon"
}
catch{
$out3 = "any other output"
}
}
Write-Output "VM: $($vm.Name): $found"
Write-Output "VM: $($vm.Name): $out1"
Write-Output "VM: $($vm.Name): $out2"
Write-Output "VM: $($vm.Name): $out3"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Guru for your suggestion.
With the given code I am able to get output but somehow its failing in If statement.
Can you please correct..
I forgot the $-sign, it's corrected now
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
There is some problem.Now its not showing the script output.
Even if I have kept correct password.
$script = @"
drivererquery
"@
$vm=Get-VM 'test1'
if(Get-VMguest -VM $vm)
{
$found = $false
try{
$out1 = Invoke-VMScript -VM $vm -GuestUser "administrator" -GuestPassword "password@1" `
-ScriptText $script -ScriptType bat -ErrorAction Stop |
Select -ExpandProperty ScriptOutput
$found = $true
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out1 = "Invalid logon"
}
catch{
$out1 = "any other output"
}
if(!$found){
try{
$out2 = Invoke-VMScript -VM $vm -GuestUser "administrator" -GuestPassword "password@12" `
-ScriptText $script -ScriptType bat -ErrorAction Stop |
Select -ExpandProperty ScriptOutput
$found = $true
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out2 = "Invalid logon"
}
catch{
$out2 = "any other output"
}
}
if(!$found){
try{
$out3 = Invoke-VMScript -VM $vm -GuestUser "administrator" -GuestPassword "SWENGn2j4g5" `
-ScriptText $script -ScriptType bat -ErrorAction Stop |
Select -ExpandProperty ScriptOutput
$found = $true
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out3 = "Invalid logon"
}
catch{
$out3 = "any other output"
}
}
Write-Output "VM: $($vm.Name): $found"
Write-Output "VM: $($vm.Name): $out1"
Write-Output "VM: $($vm.Name): $out2"
Write-Output "VM: $($vm.Name): $out3"
}
Do you have a screenshot of the run? Any errors?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes. Below is the write output info.
VM: test1: False
VM: test1: any other output
VM: test1: any other output
VM: test1: any other output
That seems to indicate that none of the three tries worked.
And none of the three InvalidGuestLogon exceptions seems to have been triggered. Looks like there might be another issue with the Invoke-VMScript.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes LucD. Its not executing Invoke-VMScript.
What might be issue?
Was Invoke-VMScript working in your original script?
Which exception is being fired? Try to display $error[0].Exception in the Catch block.
You might also find some clues in the vmware.log for that VM
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes Invoke-VMScript is working with Original Code.
I will check in vmware.log also.
This is the errors
7/20/2016 6:53:14 PM Invoke-VMScript Value cannot be found for the mandatory parameter ScriptText
7/20/2016 6:53:14 PM Invoke-VMScript Value cannot be found for the mandatory parameter ScriptText
7/20/2016 6:53:14 PM Invoke-VMScript Value cannot be found for the mandatory parameter ScriptText
But If I execute Original Script it worked successfully. I don't know where is the issue.
Check if you have any blanks after the back-tick on the Invoke-VMScript line
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes LucD. That is the issue after removing blanks it worked
