VMware Cloud Community
probo
Enthusiast
Enthusiast
Jump to solution

Invoke-VMScript is not returning expected output

Hi There

I'm using Invoke-VMScript to invoke a script, then return the exit status of the script. Since this is Linux I'm returning the $? variable.

To simplify the problem, I've written a small script on my linux box that is as follows:

#!/bin/bash
exit 1

I've saved this in /tmp and called it exit1.sh. If I run this from the command line I get this return:

(root@au-lab)/tmp$ ./exit1.sh; echo $?

1

If I run it from VM Script I get:

Invoke-VMScript -VM AU-GSCLAB-7 -ScriptText "/tmp/exit1.sh; echo `$?" -GuestUser root -GuestPassword password | Select -ExpandProperty ScriptOutput

0

PowerCLI Version

----------------

   VMware vSphere PowerCLI 5.0.1 build 581491

---------------

Snapin Versions

---------------

   VMware AutoDeploy PowerCLI Component 5.0 build 544967

   VMware ImageBuilder PowerCLI Component 5.0 build 544967

   VMware License PowerCLI Component 5.0 build 544881

   VMware vSphere PowerCLI Component 5.0 build 581435

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
Pavel_Dimitrov
VMware Employee
VMware Employee
Jump to solution

Hi,

It seems that we have some problems with the output redirection and the "echo". I am logging a bug for that. I don't know if this will help you, but you can get the script exit code from the VMScriptResult returned from Invoke-VMScript:

Invoke-VMScript -VM AU-GSCLAB-7 -ScriptText "/tmp/exit1.sh"  -GuestUser root -GuestPassword password | Select -ExpandProperty ExitCode

1

View solution in original post

0 Kudos
7 Replies
Grzesiekk
Expert
Expert
Jump to solution

Hi probo,

  do you see proper result if you do it in the same way as here ?

http://communities.vmware.com/message/1805544

exit/echo

Regards,

Greg

--- @blog https://grzegorzkulikowski.info
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You didn't say which Linux flavour you are running in the guest ?


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

0 Kudos
probo
Enthusiast
Enthusiast
Jump to solution

Hi Greg,

Using just 'echo $?' won't work, as $? is interpreted by powershell and return that value (not the value from linux).

To simulate it I've used a here document:

PowerCLI C:\Program Files\CDM_Builder\Testing> $script = @'
>> /tmp/exit1.sh; echo $?
>> '@
>>
PowerCLI C:\Program Files\CDM_Builder\Testing> Invoke-VMScript -VM AU-GSCLAB-7 -ScriptText "$script" -GuestUser root -GuestPassword password | Select -ExpandProperty ScriptOutput
0

I've worked around the problem for now by using:

./exit1.sh || echo 1

The shell interprets the return status correctly. In case of the '||' it will only execute the command on the right if the command on the left returns something other than 0.

0 Kudos
probo
Enthusiast
Enthusiast
Jump to solution

CentOS 4.9 with vmware tools 8.6.5.11214

Also tested on RHEL 5.8, same vmware tools, same issue

0 Kudos
Pavel_Dimitrov
VMware Employee
VMware Employee
Jump to solution

Hi,

It seems that we have some problems with the output redirection and the "echo". I am logging a bug for that. I don't know if this will help you, but you can get the script exit code from the VMScriptResult returned from Invoke-VMScript:

Invoke-VMScript -VM AU-GSCLAB-7 -ScriptText "/tmp/exit1.sh"  -GuestUser root -GuestPassword password | Select -ExpandProperty ExitCode

1

0 Kudos
probo
Enthusiast
Enthusiast
Jump to solution

Thanks Pavel, very helpful.

Pat

0 Kudos
iyivanov
Contributor
Contributor
Jump to solution

In my case I am executing a PowerShell script on the remote machine as in

============

Param( [int]$code=0)

Write-Host "Exit Code is $code";

exit $code;

============

Then Invoke-VMScript does not return the correct exit code that is returned from the script. The VMScriptResult object's ExitCode does not have the proper value.

If I use throw statement in my script then ExitCode has 1, no matter what the remote script actually returns (throws).

I think this is how Invoke-VMScript is designed, actually, just to say if the remote command succeeded or failed.

0 Kudos