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
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
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%
'@
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
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
PS: I feel a blog post and a new Invoke-VMScriptPlus coming up :smileygrin:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
I'm afraid not, but there are ways to work around it.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD Do you have any suggestions or work around to get around this? Could you please share if you are aware ?
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
Thank you LucD . Your solution worked..
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