I've found several bugs over the last few months that are really frustrating when trying to run Bash scripts using Invoke-VMScript. The work around has been to re-write the script or simply put them in a file, Copy-VMGuestFile, dos2unix and chmod +x and run it.
Here's a list of the bugs, I would appreciate if VMware would pay attention to these because I'm ready to give up on PowerCLI and move to a Ruby implementation of the SOAP API.
( exit 1 ); echo $?
This will return 0 from Invoke-VMScript, when it should return the value given to exit in the sub-script (1 in this case). When I pasted this to VMWare community forum, I was told to use the “ExitCode” Object returned by Invoke-VMScript. But there are more bugs.
[[ $(/bin/true) ]] || echo TRUE
This should return TRUE always, but when run from Invoke-VMScript we get:
bash: -c: line 0: syntax error near `]]’
bash: -c: line 0: `[[ ]] || echo TRUE’
Simple assigning a command output to a variable and echoing it.
A=$(grep alias ~/.bashrc |tail -1); echo $A
This returns nothing. Running ‘ps -ef’ while it’s executing reveals:
/bin/bash -c bash > /tmp/vmware-root/powerclivmware255 2>&1 -c “A=alias mv=’mv -i’; echo ; sleep 30”
Finally $! is broken. As an environmental variable in bash it should return the PID of the last process spawned when using &:
/bin/true & echo $!
Returns nothing.
I use the following Invoke-VMScript syntax:
Invoke-VMScript -VM "VM_Name" -ScriptType bash -ScriptText "blah" -GuestUser "root" -GuestPassword "blah"
It appears anywhere you use variables is likely to result in unexpected output. I'm escaping the $ symbol from within PowerShell and have even tried using here documents (@' <script> '@) to eliminate PowerShell as the cause. I've tested all of these scenarios in bash from the command line and all work as described.
EDIT: Forgot to mention using backticks instead of $() to get the output of an command yields the same results in examples 2 & 3
PowerShell tries to expand variables in a string surrounded by double quotes. Variables in PowerShell start with a dollar sign. All your examples have dollar signs in them. You can try to use single quotes because PowerShell will not expand anything in a string surrounded by single quotes:
Invoke-VMScript -VM "VM_Name" -ScriptType bash -ScriptText 'blah' -GuestUser "root" -GuestPassword "blah"
As I said, I'm escaping $ symbol with a backtick and have even tried a here document in PowerShell, which ensures the $ is not interpreted as a variable.
i.e.
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> $script = @'
>> A=$(grep alias ~/.bashrc |tail -1)
>> echo $A
>> '@
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> $script
A=$(grep alias ~/.bashrc |tail -1)
echo $A
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Invoke-VMScript -VM "VM_Name" -ScriptType bash -ScriptText $script -GuestUser "root" -GuestPassword "blah"
WARNING: The version of VMware Tools on VM 'VM_Name' is out of date and may cause Invoke-VMScript to work improperly.
ScriptOutput
-----------------------------------------------------------------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------------------------------------------------------------
Have you tried it also on a VM that does not give the "WARNING: The version of VMware Tools on VM 'VM_Name' is out of date and may cause Invoke-VMScript to work improperly." message? Maybe the version of the VMware tools is the cause of this problem?
The version is the latest as far as I can tell, not sure why it's throwing an error, and these bugs have been around for a few versions (8.6 at least).
[root@VM_Name]# vmware-toolbox-cmd --version
9.0.1.18551 (build-913578)
Thanks for raising this probe, its actually a known issue on our side that we will be looking to resolve in the future.
Thanks Alan,
Could you provide an idea of when this will be fixed?
For legal reasons unfortunately not.
Well I'm afraid I'll be moving away from PowerCLI
Sorry to hear that, I hope you are able to find what you need from our other SDKs and tools.