I have built a batch file that runs a BTEQ script and returns the data in a txt file. However, I am expanding my code to allow for user input of user_id, password, and a period parameter YYYYMM. I initially wrote hard coded my personal login information into the BTEQ file and tested with just the snapshot parameter like so.
echo off
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
SET /P SNAPSHOT="What snapshot should I use [YYYYMM]?"
DEL EXPORT.TXT
POWERSHELL -COMMAND "(GET-CONTENT TEST.TXT) | FOREACH-OBJECT {$_ -REPLACE \"\?YYYYMM\"", %SNAPSHOT% "} | OUT-FILE TEST.TXT";
bteq < test.txt > output.txt 2>&1
POWERSHELL -COMMAND "(GET-CONTENT TEST.TXT) | FOREACH-OBJECT {$_ -REPLACE " %SNAPSHOT% ,"\"?YYYYMM\"} | OUT-FILE TEST.TXT";
@echo off goto end
en @echo exit
The code worked as intended. However, when I go to add the userid and password, I simply copy the snapshot code for PowerShell and replaced the variables as needed.
echo off
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
SET /P USER_ID=Enter User ID:
SET /P USER_PASSWORD=Enter Password:
SET /P SNAPSHOT="What snapshot should I use [YYYYMM]?"
DEL EXPORT.TXT
POWERSHELL -COMMAND "(GET-CONTENT TEST.TXT) | FOREACH-OBJECT {$_ -REPLACE \"$USER_ID\"", %USER_ID% "} | OUT-FILE TEST.TXT";
POWERSHELL -COMMAND "(GET-CONTENT TEST.TXT) | FOREACH-OBJECT {$_ -REPLACE \"\$USERPASSWORD\"", %USER_PASSWORD% "} | OUT-FILE TEST.TXT";
POWERSHELL -COMMAND "(GET-CONTENT TEST.TXT) | FOREACH-OBJECT {$_ -REPLACE \"\?YYYYMM\"", %SNAPSHOT% "} | OUT-FILE TEST.TXT";
bteq < test.txt > output.txt 2>&1
::POWERSHELL -COMMAND "(GET-CONTENT TEST.TXT) | FOREACH-OBJECT {$_ -REPLACE " %SNAPSHOT% ,"\"?YYYYMM\"} | OUT-FILE TEST.TXT";
@echo off goto end
en @echo exit
The snapshot code still works but I get errors for the User_ID and User_Password user inputs. As follows:
Enter User ID:userTest
Enter Password:TestPass
What snapshot should I use [YYYYMM]?202102
At line:1 char:66
+ ... -CONTENT TEST.TXT) | FOREACH-OBJECT {$_ -REPLACE "\$USER_ID", userTes ...
+ ~
Missing expression after ','.
At line:1 char:67
+ ... TEST.TXT) | FOREACH-OBJECT {$_ -REPLACE "\$USER_ID", userTest } | OUT ...
+ ~~~~~~~~
Unexpected token 'userTest' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingExpressionAfterToken
At line:1 char:71
+ ... ENT TEST.TXT) | FOREACH-OBJECT {$_ -REPLACE "\$USERPASSWORD", TestPas ...
+ ~
Missing expression after ','.
At line:1 char:72
+ ... TXT) | FOREACH-OBJECT {$_ -REPLACE "\$USERPASSWORD", TestPass } | OUT ...
+ ~~~~~~~~
Unexpected token 'TestPass' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingExpressionAfterToken
$_ -REPLACE \"\?YYYYMM\"", %SNAPSHOT% ", or$_ -REPLACE " %SNAPSHOT% ,"\"?YYYYMM\"are correct syntax, to begin with, (regardless of your statement that it worked as intended). You should be using this syntax,PowerShell -NoProfile -Command "(Get-Content -Path \".\test.txt\") | ForEach-Object {$_ -Replace \"StringBefore\",\"%StringAfter\"} | Set-Content -Path \".\test.txt\"". I've changed yourOut-FiletoSet-Content, and doublequoted.\test.txt, just to highlight the correct escape syntax, (you could, if you wish change those).%StringAfteris just a placeholder, not a variable, just change it to your string, or batch created variable as needed. The syntax is very simple, everything you are passing topowershellas-Commandis enclosed in a single set of doublequotes, every doublequote inside those must be escaped with a backwards slash. That is the correct syntax, it has always worked for me on every system I've tried it on. If you want to know what-NoProfiledoes look it up, I'm not Alexa, Cortana, Google, or Siri