PowerCLI

 View Only
Expand all | Collapse all

Invoke Vm Script with Multi line Text Doesnt work

  • 1.  Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 12, 2018 04:42 PM

    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



  • 2.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 12, 2018 05:01 PM

    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



  • 3.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 12, 2018 06:01 PM

    Hi,

    How is this script different from original post?



  • 4.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 12, 2018 06:03 PM

    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


  • 5.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 12, 2018 06:14 PM

    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.



  • 6.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 12, 2018 06:12 PM

    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



  • 7.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 12, 2018 06:15 PM

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

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



  • 8.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 12, 2018 06:25 PM

    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.



  • 9.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 12, 2018 06:27 PM

    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?



  • 10.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 12, 2018 06:32 PM

    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. :-)



  • 11.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 12, 2018 07:36 PM

    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



  • 12.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 12, 2018 07:48 PM

    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.



  • 13.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 12, 2018 08:00 PM

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



  • 14.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 12, 2018 10:53 PM

    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?



  • 15.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 13, 2018 12:31 AM

    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



  • 16.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 13, 2018 11:24 AM

    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



  • 17.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 13, 2018 11:37 AM

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



  • 18.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 13, 2018 11:40 AM

    $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



  • 19.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 13, 2018 12:04 PM

    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?



  • 20.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 13, 2018 12:12 PM

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



  • 21.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 13, 2018 12:24 PM

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

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



  • 22.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 13, 2018 12:55 PM

    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%

    '@



  • 23.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 13, 2018 01:43 PM

    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.



  • 24.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 13, 2018 01:52 PM

    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:



  • 25.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 13, 2018 04:56 PM

    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.



  • 26.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 13, 2018 05:01 PM

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



  • 27.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 13, 2018 05:04 PM

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



  • 28.  RE: Invoke Vm Script with Multi line Text Doesnt work
    Best Answer

    Posted Feb 13, 2018 05:48 PM

    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



  • 29.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Feb 16, 2018 02:05 PM

    Thank you LucD​ . Your solution worked.. :smileyhappy:



  • 30.  RE: Invoke Vm Script with Multi line Text Doesnt work

    Posted Oct 29, 2021 02:43 PM

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

     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