IKirill
Enthusiast
Enthusiast

Invoke-VMScript. Pass variable in BASH

Hi LUCD, hi all!

Small script to change IP in CentoS

$IPguest = Read-Host -Prompt 'Enter IP Guest'

$script = "sed -i 's:^IPADDR=.*$:IPADDR=$IPguest:g' /etc/sysconfig/network-scripts/ifcfg-ens160"

Invoke-VMScript -VM CentOS-7 -ScriptType bash -ScriptText $script -GuestUser root -GuestPassword P@ssw0rd

Error:sed: -e expression #1, char 13: unterminated `s' command

The problem here: $:IPADDR=$IPguest.

How can I pass variable in this case?

Reply
0 Kudos
LucD
Leadership
Leadership

There are 2 issues:

  • your end of line $ sign: you will have to escape it with a back-tick
  • you have the indicate the variable that needs to be substituted by enclosing it in parenthesis

Try like this

$IPguest = Read-Host -Prompt 'Enter IP Guest'

$script = "sed -i 's:^IPADDR=.*`$:IPADDR=$($IPguest):g' /etc/sysconfig/network-scripts/ifcfg-ens160"

Invoke-VMScript -VM CentOS-7 -ScriptType bash -ScriptText $script -GuestUser root -GuestPassword P@ssw0rd


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

View solution in original post

Reply
0 Kudos
IKirill
Enthusiast
Enthusiast

Thanks LucD!

You are absolutely amazing master!!!

Reply
0 Kudos
DanteMc
Contributor
Contributor

I may be having a similar problem. I am getting the following error:

PS> Invoke-VMScript -VM CentOS7 -ScriptText 'sed -ri "s|10.28.183.204|10.28.164.138|" /etc/sysconfig/network-scripts/ifcfg-ens192' -GuestUser root -GuestPassword password

Invoke-VMScript : 12/3/2020 5:12:26 PM Invoke-VMScript An error occurred while sending the request.
At line:1 char:1
+ Invoke-VMScript -VM CentOS7 -ScriptText 'sed -ri "s|10.28.183. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-VMScript], ViError
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_DownloadFileFromGuest_DownloadError,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript

After hitting enter on the PS command, it takes 3 minutes 52 seconds (I used a stopwatch) before PS returns the error message but the SED change is made on the host within a few seconds.  Why is the console hanging up for 3m52s before returning the error but the command executed within seconds on the host?

GuestOS:  CentOS7_1908 w/ VMWare Tools v11.0.5.17716 running

PowerShell v5.1.14393.2969 with PowerCLI v12.1.0 on Win Svr 2016

Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership

I don't think you are experiencing a similar problem.
Your code inside the Guest OS is working, it seems to be the transfer back from the result of the run to the calling station that seems to be experiencing an issue.
This might be a certificate issue on the ESXi node where the VM is running (missing SAN = Subject Alternative Name, more specifically the IP address).

I would suggest opening a new thread.


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

Reply
0 Kudos
DanteMc
Contributor
Contributor

Thank you!

Reply
0 Kudos