Powershell comment

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ImAhNoBoDy
Posts: 11
Joined: 16 Nov 2016 12:03

Powershell comment

#1 Post by ImAhNoBoDy » 07 May 2020 21:12

Is there a way to write a comment in powershell, but in a batch script? Unfortunately I can't use polyglot for this.

Example that doesn't work, but to show the idea

Code: Select all

powershell -command "& {"^
#this is a comment
Get-service^
"}"
I can only get the comment to work if I use a comment block, but I don't really want to use it.

Code: Select all

powershell -command "& {"^
<#this is a comment#^>;^
Get-service^
"}"
Last edited by ImAhNoBoDy on 09 May 2020 20:56, edited 1 time in total.

aGerman
Expert
Posts: 3890
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Powershell comment

#2 Post by aGerman » 08 May 2020 07:23

The only other possibility that I can think of is to have the comment on the CMD side rather than on the PS side. Typically you would use a variable that expands to nothing in hybrid scripts like that.

Code: Select all

powershell -command "& {"^
%= this is a comment =%
Get-service^
"}"
Steffen

jfl
Posts: 136
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: Powershell comment

#3 Post by jfl » 08 May 2020 12:01

The # 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:

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
The %\n% macro above generates line feeds and continuation characters.
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>

ImAhNoBoDy
Posts: 11
Joined: 16 Nov 2016 12:03

Re: Powershell comment

#4 Post by ImAhNoBoDy » 09 May 2020 11:10

jfl wrote:
08 May 2020 12:01
The # 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:

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
The %\n% macro above generates line feeds and continuation characters.
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>
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.

I think when \n is expanded it would be 4 or 8 line feeds?

aGerman
Expert
Posts: 3890
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Powershell comment

#5 Post by aGerman » 09 May 2020 17:58

Indeed the creation of the necessary escaped line feed can be simplified.

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
You can read up in this thread viewtopic.php?f=3&t=8769#p57659

Steffen

Eureka!
Posts: 97
Joined: 25 Jul 2019 18:25

Re: Powershell comment

#6 Post by Eureka! » 09 May 2020 18:39

"Plan B" (or C?):

Code: Select all


(
echo Get-Service
echo #this is a comment
echo ls
) | powershell -command -

Notes:
- Get-Service without the trailing S
- The & should be inside the scriptblock
- I doubt if scriptblocks are even supported from a CMD prompt.

ImAhNoBoDy
Posts: 11
Joined: 16 Nov 2016 12:03

Re: Powershell comment

#7 Post by ImAhNoBoDy » 09 May 2020 21:07

aGerman wrote:
09 May 2020 17:58
Indeed the creation of the necessary escaped line feed can be simplified.

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
You can read up in this thread viewtopic.php?f=3&t=8769#p57659

Steffen
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:

Code: Select all

set LF=^



powershell -command "& {"%lf%
#this is a comment%lf%
Get-service%lf%
"}"
Note: I'll be using [^& {] instead from now one.

Thank you everyone for all your help. Definitely made my work easier. I hate that I can't use polyglot/cmd & powershell hydrid cause fireeye flags it as malicious.

jfl
Posts: 136
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: Powershell comment

#8 Post by jfl » 13 May 2020 04:20

@aGerman:

Hi Steffen,

There's an important difference between your %\n% macro and mine. (Beyond the fact that I generated mine with an overly complex sequence)
  • Mine expands to <LF>^
  • Yours expands to ^<LF>
Obviously both work for generating a one-line batch command, which in turn creates a multi-line PowerShell command.
But I don't understand how yours allows this, since the ^ continuation character does not end up being the last character on each batch line.
So why are the consecutive batch lines glued together with your macro too?

Jean-François

Post Reply