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

1 Solution

Accepted Solutions
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

View solution in original post

Reply
0 Kudos
29 Replies
LucD
Leadership
Leadership
Jump to solution

Can you try like this, that seems to work for me.

$code = @'

set output_file=%TEMP%\ipconfig_output.txt

ipconfig /all > %output_file%

type %output_file%

'@

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


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

Reply
0 Kudos
vijaysaiv
Contributor
Contributor
Jump to solution

Hi,

How is this script different from original post?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

With the 3th line, type, the content of the file will be displayed.

That would show:

  • that ipconfig, and hence multi-line scripts are working
  • that the file is created and contains the output of the command


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

Reply
0 Kudos
arvindathere
Enthusiast
Enthusiast
Jump to solution

Thanks for the prompt response LucD.  LucD

Yes with the 3rd line i could see the script output.

Upon logging into the VM, strangely no file is seen in the temp dir. Any idea on that ?

Thanks,

Arvind S

Reply
0 Kudos
vijaysaiv
Contributor
Contributor
Jump to solution

I am trying without type. I logged into the VM to see if the file is created. However, there is no file in specified directory.

Then I tried to echo the contents of output_file variable. It is just printing %output_file% instead of the file path. It looks like something is wrong while setting the output_file variable.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's most probably because you ask for a 'temp' file.

Try creating the file in another location, for example C:\Temp


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

Reply
0 Kudos
vijaysaiv
Contributor
Contributor
Jump to solution

This is precisely what I am trying to do:

$command = @'

set output_file=c:\ipconfig_output.txt
echo %output_file%

ipconfig /all > %output_file%

'@

And I don't think it is working.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you allowed to write in the root folder of the c-partition?

Btw, am I answering two people on the same thread, or one person using two accounts?


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

Reply
0 Kudos
vijaysaiv
Contributor
Contributor
Jump to solution

I am allowed to write to root folder.

Before even going to 3rd line, why my echo is not printing the contents of outout_file.

Two differnet persons with two completely different accounts. 🙂

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The reason you are not seeing the variable substituted, is because that variable substitution for an echo command only happens when a line is read.

And, for ScriptType Bat, the multiple lines are sent as 1 line with the ampersand.

So

$code = @'

set foo=bar

echo %foo%

'@

is sent to cmd.exe inside the guest OS as "set foo=bar & echo %foo%".

One line, so no substitution happened yet when the 2nd command, the echo, is executed.

You can bypass this by using the set command to display the content of a variable.

$code = @'

set foo=bar

set foo

'@

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

And btw, using a predefined variable like %temp% is probably not a good idea.

Use something else.

$code = @'

set output_file=c:\ipconfig_output.txt

ipconfig /all > %output_file%

type %output_file%

'@

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


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

Reply
0 Kudos
jrodsguitar
Enthusiast
Enthusiast
Jump to solution

Excellent follow up.

LucD No direspect meant but isn't it bad practice to use hard coded paths?

For example why would someone use c:\users\username\appdata\temp over %temp%? Not using an environmental variable in a script paints you into a corner and is not scalable or portable.

Blog: https://powershell.house/
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are of course correct, use the %temp% variable.


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

vijaysaiv
Contributor
Contributor
Jump to solution

Thank you for the good explanation.

I am still not able to create the file inside the guest VM. Are there anymore steps to be followed?

Reply
0 Kudos
jrodsguitar
Enthusiast
Enthusiast
Jump to solution

A few things I can think of:

The file will be saved under the context of the user you run the script as. So if you run as 'admin123' then the file will be located in c:\users\admin123\appdata\local\temp\ipconfig_output.txt".

Also I literally translated the batch script to powershell so you can try this. I don't know why you are creating environmental variables so I left that in the powershell version:

$code = [scriptblock]::create({

[Environment]::SetEnvironmentVariable("output_file", "$([Environment]::GetEnvironmentVariable('TEMP')+'\ipconfig_output.txt')")

$ipconfig_output =  [Environment]::GetEnvironmentVariable("output_file")

$ipconfig = invoke-command {& cmd "/c ipconfig /all"} | out-file $ipconfig_output

Get-Content $ipconfig_output

})

Invoke-VMScript -VM 'machine_name' -ScriptType powershell -ScriptText $code

Blog: https://powershell.house/
Reply
0 Kudos
arvindathere
Enthusiast
Enthusiast
Jump to solution

LucD​​

I tried the same script you had mentioned on Windows 7.

The script output is displayed using type command.

However no file found in the C:\  directory. Am running as Administrator.

$scripttext = @'

@echo off

set output_file=C:\ipconfig_output.txt

set output_file

ipconfig /all > output_file

type %output_file%

'@

Invoke-VMScript -VM $vm  -ScriptType Bat -ScriptText $scripttext -GuestUser Administrator -GuestPassword XXXXXX

I checked its certainly not hidden ? What am i missing here ? Hows the file content getting printed without the file ..

Regards,

Arvind S

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you also try with another location (i.e. not in the rootfolder of the C-partition)


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

Reply
0 Kudos
arvindathere
Enthusiast
Enthusiast
Jump to solution

$scripttext = @'

@echo off

set output_file=C:\demo\ipconfig_output.txt

set output_file

ipconfig /all > output_file

type %output_file%

'@

PowerCLI C:\> Invoke-VMScript -VM $vm  -ScriptType Bat -ScriptText $scripttext -GuestUser Administrator -GuestPassword XXXXXX

Still the output is displayed but file is not seen inside the demo folder

Regards,

Arvind S

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is the file still there when you run a second Invoke-VMScript with just the set and the type lines in there?

And the content hasn't changed?


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

Reply
0 Kudos
arvindathere
Enthusiast
Enthusiast
Jump to solution

The file itself is not visible even after the first time. The "type" is displaying the file contents though.

Reply
0 Kudos