I need to execute a shell script on a Mac VM machine. Shell script is copied on the VM and has executable permission.
The Shell file has "jfrog cli" command. jfrog cli is installed on the mac VM using "brew install jfrog-cli".
On executing `jfrog` command on Mac VM, it provides expected output.
But when the same command is executed via Invoke-VMScript, "jfrog: command not found" error is displayed.
Restarting the VM didn't help.
Below are my Powercli commands:
$Command_text = "~/downloadDmg.sh $winAppBranch $artifactoryUser $artifactoryPwd"
Invoke-VMScript -VM $TARGET_VM $Command_text -GuestUser $vm_user -GuestPassword $vm_pass -scriptType bash
Please suggest.
Sorry, but I don't have a Mac at my disposal.
You could try to use the full path to the jfrog binary.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
What do you have in that downloadDmg.sh file?
What does echo $PATH return, when run via Invoke-VMScript?
Do the paths include the one where jfrog is located?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
downloadDmg.sh has following code:
export dmgname=$(jfrog rt s --user $2 --password $3 --url https://artifactoryURL repo/$1/*.dmg| jq -r "max_by(.created)"."path")
echo "dmgname: $dmgname"
curl -u $2:$3 https://artifactoryURL/${dmgname} -o artifact.dmg
Script output of "env" command:
PS /Users/sgaikwad> $Command_text="env"
PS /Users/sgaikwad> Invoke-VMScript -VM $TARGET_VM $Command_text -GuestUser $vm_user -GuestPassword $vm_pass -scriptType bash
ScriptOutput ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | SHELL=/bin/zsh | USER=qa
| PATH=/usr/bin:/bin:/usr/sbin:/sbin
| _=/usr/bin/env
| PWD=/Users/qa
| XPC_FLAGS=0x0
| XPC_SERVICE_NAME=0
| SHLVL=3
| HOME=/Users/qa
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I get empty output when I run "$PATH" command. But as per the env command output, looks like PATH is set. Please suggest.
Sorry, but I don't have a Mac at my disposal.
You could try to use the full path to the jfrog binary.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you @LucD for the suggestion, added below line of code in the shell file and jfrog and jq started working.
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
@LucD facing a similar issue with a different tool Appium. If I just pass `appium`, then appium: command not found error is displayed. So as you suggested, passed the whole path and the error went away.
$Command_text = "/usr/local/bin/appium -p 4726 --nodeconfig ~/AcceptanceTestFramework/AppiumNodeMac.json"
However, the script is throwing new error:
| env: node: No such file or directory
Appium is based of Node and node is installed on the VM.
How do we make Invoke-VMScript to refer the local $PATH variables? Could you please suggest.
Can you check which environment variables are known under the user account you use with GuestCredential?
Try running 'printenv' in the bash shell
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This is the output returned by Invoke-VMScript:
PS /Users/sgaikwad> $Command_text = "printenv"
PS /Users/sgaikwad> Invoke-VMScript -VM $TARGET_VM $Command_text -GuestUser $vm_user -GuestPassword $vm_pass -scriptType bash
ScriptOutput ------------------------------------------------------------------------------------
| SHELL=/bin/zsh | USER=qa
| PATH=/usr/bin:/bin:/usr/sbin:/sbin
| _=/usr/bin/printenv
| PWD=/Users/qa
| XPC_FLAGS=0x0
| XPC_SERVICE_NAME=0
| SHLVL=3
| HOME=/Users/qa
This is the output from running `printenv` on the VM directly:
----
TERM_PROGRAM=Apple_Terminal
SHELL=/bin/zsh
TERM=xterm-256color
TMPDIR=/var/folders/yk/d21hdh3x667b1659fp12z55h0000gn/T/
TERM_PROGRAM_VERSION=433
TERM_SESSION_ID=30B7A4B2-596D-4852-9EEF-C8CA49F3C00F
USER=qa
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.d2Q50kgJmd/Listeners
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
LaunchInstanceID=BE4F799C-BF0F-4250-8351-A99CDD7E7580
PWD=/Users/qa/
LANG=en_US.UTF-8
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
SHLVL=1
HOME=/Users/qa
LOGNAME=qa
SECURITYSESSIONID=186a6
OLDPWD=/Users/qa/
_=/usr/bin/printenv
-------
The problem seems to come from how the VMware Tools start the bash shell (see else the SHLVL variable).
I'm not sure why the difference in environment variables.
You might try if you can set (export) the required environment variables as part of your script.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ultimately you might try with my Invoke-VMScriptPlus function to see if that makes any difference.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you @LucD for solving the mystery (SHLVL variable) of why the env variables were not getting displayed.
I was able to pipe the command and it worked
$Command_text = "export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin; appium -p 4726 --nodeconfig ~/AppiumNodeMac.json"