VMware Cloud Community
arvindathere
Enthusiast
Enthusiast
Jump to solution

Invoke Vm Script with Multi line Text Doesnt work

Hi,

Am trying to use the below command . The txt file is not created in the temp dir.

PowerCLI C:\> $scripttext=@'

>>

>>

>> set output_file=%TEMP%\ipconfig_output.txt

>>

>> ipconfig /all > %output_file%

>> '@

>>

PowerCLI C:\> Invoke-VMScript -VM $vm  -ScriptType Bat -ScriptText $scripttext   -GuestUser <Username> -GuestPassword <Password>

ScriptOutput

-------------------------------------------------------------------------------------------------------------------------------------------------------------------|

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

When i use the filepath directly , this works and the txt file is created.

PowerCLI C:\> $scripttext=@'

>>

>> ipconfig /all > %TEMP%\ipconfig_output.txt

>> '@

>>

PowerCLI C:\> Invoke-VMScript -VM $vm  -ScriptType Bat -ScriptText $scripttext -GuestUser <Username> -GuestPassword <Password>

ScriptOutput

-------------------------------------------------------------------------------------------------------------------------------------------------------------------|

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Note:

1. Am using PowerCLI 6.5 R1 to run these commands

2. The Guest VM has Windows 7 OS and VMware tools is installed and running.

My question is why is the set command not working  ?

Thanks,

Arvind S

29 Replies
LucD
Leadership
Leadership
Jump to solution

But can still find the file when you do a second Invoke-VMScript?

Can you do a dir or a type of the file.


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

0 Kudos
arvindathere
Enthusiast
Enthusiast
Jump to solution

We are not finding the file even after second run.  However, we are able to "type %output_file%" and see contents of the file. The file seems to be not available physically on guest VM in the specified directory, in our case it is c:\demo.

Here is the script once again:

$scripttext = @'

@echo off

set output_file=C:\demo\ipconfig_output.txt

set output_file

ipconfig /all > output_file

type %output_file%

'@

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Have a look in C:\Windows\System32 and check for a file named %output_file%
The default directory for the cmd.exe, which runs your BAT script is C:\Windows\System32, and it seems that is where the file is created.

Why the variable substitution is not working is still unclear to me.


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

And this is the explanation why the variable substitution is not working as expected.

Remember when I explained earlier that multi-line BAT files are combined on 1 line with the & operator.

Since the environment variable that is created by the set command is only 'known' at the end of the line, a reference to the variable in the same line will not work.

As the following will show

set.jpg

PS: I feel a blog post and a new Invoke-VMScriptPlus coming up :smileygrin:


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

0 Kudos
arvindathere
Enthusiast
Enthusiast
Jump to solution

Hi LucD

Yeah . think i get what are saying. Below is what i tried.

>set a=abc & echo %a%

abc

>set a=klm & echo %a%

abc

>echo %a%

klm

So there is no way to set and use an environmental variable within a single Invoke-VMScript?

Regards,

Arvind S.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid not, but there are ways to work around it.


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

0 Kudos
arvindathere
Enthusiast
Enthusiast
Jump to solution

LucD Do you have any suggestions or work around to get around this? Could you please share if you are aware ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, it looks a bit convoluted, but this seems to work for me.

It creates a CMD file in the user's temp folder, and then executes it.

And there is some character escaping in there for some special characters

$code = @'

echo Set output^=C:\Temp\test.txt > %temp%\mytemp.cmd

echo ipconfig/all ^> %output% >> %temp%\mytemp.cmd

cmd.exe /c %temp%\mytemp.cmd

del %temp%\mytemp.cmd

'@

Invoke-VMScript -VM ws2 -ScriptType Bat -ScriptText $code


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

0 Kudos
arvindathere
Enthusiast
Enthusiast
Jump to solution

Thank you LucD​ . Your solution worked.. Smiley Happy

0 Kudos
ElVirtualJefe
Contributor
Contributor
Jump to solution

There is a special setting for this situation, called Delayed Variable Expansion:

@Echo OFF
SETLOCAL ENABLEDELAYEDEXPANSION

If you put this at the top of your script, you will be able to expand the variables in real time, without having to create a cmd file to execute.

Here is a great link about this: https://www.robvanderwoude.com/variableexpansion.php

 

0 Kudos