VMware Cloud Community
Gaetan123
Enthusiast
Enthusiast

Invoke-vmscript bash syntax

Hello

I'm trying to automate the population of /etc/hosts via the following command. The command ran on the host will populate with the IP address hostname, FQDN

echo "`hostname -i` $HOSTNAME `echo $HOSTNAME| cut -d"." -f1`" >> /etc/hosts

but running the same command via script will not fulfill the purpose. It will append the command at the end of /etc/hosts

I tried skipping double quotes using backslash but I get the following error

$GuestCred=$Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for VM", "root", "")

$script = @"
echo \"`hostname -i` $HOSTNAME `echo $HOSTNAME| cut -d'.' -f1`\" >> /etc/hosts
"@

Invoke-VMScript -VM $VM -GuestCredential $GuestCred -ScriptType bash -ScriptText $script

ScriptOutput
-----------------------------------------------------------------------------------------------------------------------|  cut: invalid byte, character or field list
|  Try 'cut --help' for more information.
|  
-----------------------------------------------------------------------------------------------------------------------

 Has to be a syntax issue. 

Reply
0 Kudos
1 Reply
LucD
Leadership
Leadership

Try replacing the quotes on the here-string with single quotes, and then use the ExpandString method to substitute your variables.

See my https://www.lucd.info/knowledge-base/here-string-and-variable-substitution/ and https://www.lucd.info/knowledge-base/here-strings-and-the-expandstring-method/ dives.


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