Example that doesn't work, but to show the idea
Code: Select all
powershell -command "& {"^
#this is a comment
Get-service^
"}"
Code: Select all
powershell -command "& {"^
<#this is a comment#^>;^
Get-service^
"}"
Moderator: DosItHelp
Code: Select all
powershell -command "& {"^
#this is a comment
Get-service^
"}"
Code: Select all
powershell -command "& {"^
<#this is a comment#^>;^
Get-service^
"}"
Code: Select all
powershell -command "& {"^
%= this is a comment =%
Get-service^
"}"
Code: Select all
@echo off
set LF=^
%# First empty line - Do not remove #%
%# Second empty line - Do not remove #%
set ^"LF1=^%LF%%LF%"
set ^"LF2=^^^%LF1%%LF1%^%LF1%%LF1%"
set ^"LF3=^^^^^^%LF2%%LF2%^^%LF2%%LF2%"
set ^"\n=%LF3%^^^" &:# Insert a LF and continue macro on next line
PowerShell -Command ^& { %\n%
# This is a comment %\n%
$PSVersionTable %\n%
}
exit /b
Code: Select all
C:\JFL\Temp>test.bat
Name Value
---- -----
PSVersion 5.1.19041.1
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.1
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
C:\JFL\Temp>
So how is this interpreted and why does it work? Is there an article I can read about this? I've never seen anything like this hack before.jfl wrote: ↑08 May 2020 12:01The # comment does not work the way you did, because it's a line comment, and the PowerShell command you generated was all on one line for PowerShell.
To use such a line comment, you need to generate a PowerShell script that contains multiple lines.
This is akin to what we do for generating multi-line batch macros:
The %\n% macro above generates line feeds and continuation characters.Code: Select all
@echo off set LF=^ %# First empty line - Do not remove #% %# Second empty line - Do not remove #% set ^"LF1=^%LF%%LF%" set ^"LF2=^^^%LF1%%LF1%^%LF1%%LF1%" set ^"LF3=^^^^^^%LF2%%LF2%^^%LF2%%LF2%" set ^"\n=%LF3%^^^" &:# Insert a LF and continue macro on next line PowerShell -Command ^& { %\n% # This is a comment %\n% $PSVersionTable %\n% } exit /b
So as far as the batch is concerned, this is all a single command on one line; And for PowerShell this is a 4-line script.
And this way the # line comment works:
Code: Select all
C:\JFL\Temp>test.bat Name Value ---- ----- PSVersion 5.1.19041.1 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.19041.1 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 C:\JFL\Temp>
Code: Select all
@echo off
(set \n=^^^
%= This creates an escaped Line Feed - DO NOT ALTER =%
)
PowerShell -Command ^& { %\n%
# This is a comment %\n%
Get-Service %\n%
}
pause
Code: Select all
(
echo Get-Service
echo #this is a comment
echo ls
) | powershell -command -
At first I was skeptical that this would work cause I thought I had tried this already, but looking closely at your code I see you used three carets instead of my attempted one:aGerman wrote: ↑09 May 2020 17:58Indeed the creation of the necessary escaped line feed can be simplified.You can read up in this thread viewtopic.php?f=3&t=8769#p57659Code: Select all
@echo off (set \n=^^^ %= This creates an escaped Line Feed - DO NOT ALTER =% ) PowerShell -Command ^& { %\n% # This is a comment %\n% Get-Service %\n% } pause
Steffen
Code: Select all
set LF=^
powershell -command "& {"%lf%
#this is a comment%lf%
Get-service%lf%
"}"