Page 1 of 1
How to spool batch script logic to create another batch script but not expanded?
Posted: 06 Feb 2022 05:19
by SIMMS7400
Hi Guys -
I have the following piece of code that I need to spool to another batch script. However, when I redirect, the functions/variables are done so expanded which isn't right.
I need to pool this piece of code to "Testbatch.cmd":
Code: Select all
>>"..\Batch\TestBatch.cmd" (
@ECHO OFF
SET "Script=%~n0"
FOR /F %%a IN ('POWERSHELL.exe -c "(Get-Date).ToString('MM.dd.yyyy-HH.mm.ss')"') DO SET "LogTime=%%a"
FOR /F %%i IN ('POWERSHELL.exe -c "(Get-Random -Minimum 0 -Maximum 99999).ToString('00000')"') DO SET "ID=%%i"
pwsh.exe -executionpolicy bypass -c "& {try{& '..\ps1\%Script%.ps1' %LogTime% %Script% %ID%}catch{exit 1}}"
)
And so, the end results, but look exactly like what's being spooled. How would I go about doing that?
Re: How to spool batch script logic to create another batch script but not expanded?
Posted: 06 Feb 2022 05:43
by T3RRY
Escape the Percent characters with themselves for any variables you don't want expanded
Re: How to spool batch script logic to create another batch script but not expanded?
Posted: 06 Feb 2022 05:54
by SIMMS7400
THanks, Terry!
That did the trick, but having one issue where when trying to redirect a for loop command, it's recognizing the DO. Here is my almost finished code:
Code: Select all
@ECHO OFF
REM Read PS1 files from this directory
PUSHD "..\PS1"
FOR %%A IN ("*") DO (
REM Ensure cmd is purged before writing to it
DEL /F /Q "..\Batch\%%~nA.cmd" >nul 2>&1
REM Spool the below contents to the batch file using the PS1 name
>>"..\Batch\%%~nA.cmd" (
ECHO @ECHO OFF
ECHO(
ECHO(::-- Set Script Name --::
ECHO(SET "Script=%%~n0"
ECHO(
ECHO(::-- Set Log Time --::
REM ECHO(FOR /F %%a IN ('POWERSHELL.exe -c "(Get-Date).ToString('MM.dd.yyyy-HH.mm.ss')"') DO SET "LogTime=%%%%a"
ECHO(
ECHO(::-- Set Randomly Generated ID --::
REM ECHO(FOR /F %%a IN ('POWERSHELL.exe -c "(Get-Random -Minimum 0 -Maximum 99999).ToString('00000')"') DO SET "ID=%%%%a"
ECHO(
ECHO(::-- Execute Powershell Routine --::
ECHO pwsh.exe -executionpolicy bypass -c "& {try{& '..\ps1\%%Script%%.ps1' %%LogTime%% %%Script%% %%ID%%}catch{exit 1}}"
)
)
POPD
pause
Please see the two lines with REM. How do I successfully redirect that? If I put those two lines out of that overall for loop and reidrect there are no issues. Seems to be because I"m trying to redirect within a for loop.
Re: How to spool batch script logic to create another batch script but not expanded?
Posted: 06 Feb 2022 08:42
by T3RRY
Thats another escaping issue. When redirecting from within a parenthesised code block, closing parentheses to be output need caret escaping. likewise, Pipes, redirectors and ampersands that are intended to be output literally need caret escaping.
A somewhat simpler way is described here:
https://stackoverflow.com/a/68692713/12343998
Re: How to spool batch script logic to create another batch script but not expanded?
Posted: 06 Feb 2022 10:01
by Squashman
And here is where we discussed with you about escaping the parentheses.
viewtopic.php?t=8282
I bet if I do another search I will find another thread of yours where we explained escaping the percent symbols.
Regardless this site has discussed escaping the percent symbol ad nauseum. I would think with as many years you have been on this site that you would occasionally be reading the majority of questions and answers on this site when you have a bit of free time to further your skill set to help yourself and help others on this site by answering their questions.
Just reading through alot of your threads you have been told plenty of times about escaping special characters. The pipe in for commands. How to use the escape character in the findstr command. This is not a new concept to you. A simple search would have found an answer for you.
Re: How to spool batch script logic to create another batch script but not expanded?
Posted: 11 Feb 2022 10:34
by SIMMS7400
HI Squash & Team -
Yes, you're right. I'm sorry. It was the last paren in the for loop causing the issue, I should have known.
Final :
Code: Select all
SETLOCAL ENABLEDELAYEDEXPANSION
SET "EXCLUSION_LIST=Automation_Parameters.ps1,Encrypt_Credentials.ps1,Download_Backup.ps1"
SET "EXT=cmd"
REM Read PS1 files from this directory
PUSHD "..\PS1"
FOR %%A IN ("*") DO (
SET "SKIP="
ECHO "%%~A" | FINDSTR "%EXCLUSION_LIST:,= %" >nul 2>&1 && ( SET "SKIP=1" )
IF NOT DEFINED SKIP (
SET "CREATED_LIST=%%~A !CREATED_LIST!"
REM Spool the below contents to the batch file using the PS1 name
>"..\Batch\%%~nA.%EXT%" (
ECHO @ECHO OFF
ECHO(
ECHO(::-- Set Script Name --::
ECHO(SET "SCRIPT=%%~n0"
ECHO(
ECHO(::-- Set Log Time --::
ECHO(FOR /F %%%%a IN ('POWERSHELL.exe -c "(Get-Date).ToString('MM.dd.yyyy-HH.mm.ss')"'^) DO SET "LOGTIME=%%%%a"
ECHO(
ECHO(::-- Set Randomly Generated ID --::
ECHO(FOR /F %%%%a IN ('POWERSHELL.exe -c "(Get-Random -Minimum 0 -Maximum 99999).ToString('00000')"'^) DO SET "ID=%%%%a"
ECHO(
ECHO(::-- Execute Powershell Routine --::
ECHO pwsh.exe -executionpolicy bypass -c "& {try{& '..\ps1\%%SCRIPT%%.ps1' %%LOGTIME%% %%SCRIPT%% %%ID%%}catch{exit 1}}"
)
)
)
POPD
ECHO The following PS1 scripts were converted to cmd:
ECHO %CREATED_LIST%
ECHO(
ECHO Press [Enter] to Exit
SET /P=
GOTO :EOF
Squash, you need to get a little honey on your stinger. But hey, if condescending internet posts get your jollys off, who am I to judge!
Happy Friday and hope everyone has a great weekend!