VMware Cloud Community
DBofTLP
Contributor
Contributor
Jump to solution

Invoke-VMScript and escaping variables

Hello VMTN,

     I have what is probably a simple issue.  I've got a set of functions that get passed two variables; the name of a VM and which NIC to enable\disable in Windows within that VM.  The name of that NIC has spaces, i.e. Network Interface 4.  The function goes on to define the command to inject as:

$cmd = "c:\windows\system32\netsh.exe interface set interface name=""$NICtoEnable"" admin=ENABLED"

It then calls the invoke-vmscript cmdlt as follows:

Invoke-VMScript -VM $VMName -GuestCredential $GuestCredential -ScriptType bat -ScriptText $cmd

and it does return the output, however I'm only getting "Invalid Parameter" in return.  What that tells me is that it is running netsh within the VM, however on of the parameters is not being defined properly.  I'm sure it's because I haven't properly escaped the $NICtoEnable variable in $cmd that get's passed to -ScriptText.  I've also tried, instead of double double-quotes, using single quotes (' ...') to no avail.

Any help would be greatly appreciated and thanks in advance.

Best,

@dboftlp

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try your $cmd like this

$cmd = "c:\windows\system32\netsh.exe interface set interface name=" + '"' + $NICtoEnable + '"' + " admin=enabled" 

The trick is to pass the double quotes around the NIC name, hence the single quotes around the double quotes.


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

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

Try your $cmd like this

$cmd = "c:\windows\system32\netsh.exe interface set interface name=" + '"' + $NICtoEnable + '"' + " admin=enabled" 

The trick is to pass the double quotes around the NIC name, hence the single quotes around the double quotes.


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

DBofTLP
Contributor
Contributor
Jump to solution

Great Luc, that worked!  Thanks again for your help.

You continue to be invaluable to the community.

See you at VMworld!

:smileygrin:

@dboftlp

0 Kudos
FirstByte
Enthusiast
Enthusiast
Jump to solution

Thanks for the tip to pass a variable into the -ScriptText section, but how would one pass a variable out of the -ScriptText section, so that it can be used outside the Invoke-VMScript command, for example, to export to a CSV

Thanks

Darren

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure I get the question, the variable is available in your script, and can be included in an Export-Csv cmdlet.


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

0 Kudos