1

I have a large batch script to which I need to add some Powershell code for some regex capture which I am unable to do in batch. I was hoping to have this code integrated in my batch script using the method outlined in Link, but when adding comments I get a missing } error. I've simplified my code just to be able to replicate the issue.

This, without a comment, works:

@echo OFF
setlocal enabledelayedexpansion enableextensions
set "var=variable"
PowerShell ^
foreach ($file in Get-ChildItem -File -Include *.* -Recurse) ^
{ ^
    Write-Host $file; ^
    Write-Host $env:var; ^
}
%End PowerShell%
echo Test
pause > nul

This, with a comment, does not work:

@echo OFF
setlocal enabledelayedexpansion enableextensions
set "var=variable"
PowerShell ^
foreach ($file in Get-ChildItem -File -Include *.* -Recurse) ^
{ ^
    #Comment ^
    Write-Host $file; ^
    Write-Host $env:var; ^
}
%End PowerShell%
echo Test
pause > nul

I have tried escaping the # in a few different ways, but no matter what I do, I get the error message

Missing closing '}' in statement block or type definition.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndCurlyBrace

The only difference is the comment. Does anyone know how to get around this? (using this long-line method that is). If it's not at all possible I guess I will go for base64 encoding

  • What happens if you drop the caret after the comment line (`#Comment ^` -> `#Comment`)? – Mathias R. Jessen Sep 29 '22 at 10:30
  • I cannot see any special regex within what you've posted. Why ask for help for a specific problem, then post something which is different? – Compo Sep 29 '22 at 10:33
  • @MathiasR.Jessen I get the same error error message, but also now "with 'Write-Host' is not recognized as an internal or external command, operable program or batch file." I guess without the caret continuing the line the script tries to parse Write-Host as a batch command – HenrikKlausen Sep 29 '22 at 10:36
  • @Compo I apologize, I included the information about the regex capture to qualify why I had to use PowerShell code in my batch code, but since the issue I was having only had to do with comments, I figured I would boil my code down to just that – HenrikKlausen Sep 29 '22 at 11:08
  • I understand you were looking for the technique best suited to your chosen method, however, regex can often include characters which are problematic passed from batch files, and using that method will therefore require additional escaping of those characters in order to achieve it. There are better methods to use in those cases, one of which has already been submitted as an answer, by Hackoo, and hence the need for your actual PowerShell code. – Compo Sep 29 '22 at 11:14
  • @Compo Yeah I see what you mean, I definitely see the merit in Hackoo's code, especially since with that one you wouldn't have to deal with adding carets and ; after every line of Powershell code – HenrikKlausen Sep 29 '22 at 13:38

2 Answers2

2

What works for me if when I do the comment line in between <# and #> as if it were a comment block.

Then of course for cmd you need to escape the < and > characters with a ^:

^<# Comment #^> ^

P.S. Don't forget that using Get-ChildItem without a -Path or -LiteralPath, the cmdlet will use PowerShell's current working folder ($pwd), which is most probably not the same as the current working path cmd uses..

Theo
  • 57,719
  • 8
  • 24
  • 41
  • Thanks, this solved it! So far it seems to have used the same directory as cmd, but I will add some code to make absolutely sure, thanks for the tip! – HenrikKlausen Sep 29 '22 at 11:05
1

This an hybrid code Batch and Powershell exmaple is just to show you how to put a multiline comment block with powershell and how to execute Batch section and powershell section :


<# : Batch Script Section
@rem # The previous line does nothing in Batch, but begins a multiline comment block in PowerShell. This allows a single script to be executed by both interpreters.
@echo off
Title Wifi Passwords Recovery by Hackoo 2022 & Mode 70,3
setlocal
cd "%~dp0"
Color 0B & echo(
Echo(      Please Wait a while ... Getting SSID and Wifi Keys ...
Powershell -executionpolicy bypass -Command "Invoke-Expression $([System.IO.File]::ReadAllText('%~f0'))"
EndLocal
goto:eof
#>
# Powershell Script Section begin here...
# here we execute our powershell commands...
$Var=netsh wlan show profiles|SLS "\:(.+)$"|%{$SSID=$_.Matches.Groups[1].Value.Trim(); $_}|%{(netsh wlan show profile name="$SSID" key=clear)}|SLS "Conte.*:(.+)$"|%{$pass=$_.Matches.Groups[1].Value.Trim(); $_}|%{[PSCustomObject]@{SSID=$SSID;PASSWORD=$pass}}
$var | Format-List | Out-File -FilePath ".\WifiKeys_List_Format.txt"
$var | ConvertTo-Json | Out-File -FilePath ".\WifiKeys_JSON_Format.txt"
$var | OGV -Title "Wifi Passwords Recovery by Hackoo 2022" -wait
ii ".\WifiKeys_JSON_Format.txt"
ii ".\WifiKeys_List_Format.txt"
Hackoo
  • 18,337
  • 3
  • 40
  • 70