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