1

I need to replace a string by a user input in a xml file from a batch file, I made it working by using powershell "replace" command and calling it from the batch but I can't figure how to use batch variable inside the powershell command.

Working :

powershell -Command "(gc personalWifiProfile.xml) -replace 'password','somePassword' | Out-File -encoding ASCII tempProfile.xml"

Not working :

set /p pwd="Enter pwd: "
powershell -Command "(gc personalWifiProfile.xml) -replace 'password','%pwd%' | Out-File -encoding ASCII tempProfile.xml"

Info : I'm on Windows 7

Edit : With simpler script, it work so the problem seems to come from elsewhere in my script (the brightness part is working fine).

for /f "tokens=3" %%i in ('netsh wlan show interface ^| findstr /i "SSID"') do set "myssid=%%i" & goto next
:next
set "myssid=%myssid: =%"
if /i "%myssid%"=="someSSID" (
set "brightness=80"
 ) ELSE (
set "brightness=30"
set /p pwd="Enter pwd: "
powershell -Command "(gc personalWifiProfile.xml) -replace 'password','%pwd%' | Out-File -encoding ASCII tempProfile.xml"
pause
)
CALL "brightness.bat" "%brightness%"

The execution order between the test and real script seems different. Here is the working test script :

set /p pwd="Enter pwd: "
echo %pwd%
powershell -Command "(gc personalWifiProfile.xml) -replace 'password','%pwd%' | Out-File -encoding ASCII tempProfile.xml"
pause

With it's output :

>set /p pwd="Enter pwd: "
Enter pwd: hell
>echo hell
hell
>powershell -Command "(gc personalWifiProfile.xml) -replace 'password','hell' | Out-File -encoding ASCII tempProfile.xml"
>pause
Appuyez sur une touche pour continuer...

And the output of the full script :

>for /F "tokens=3" %i in ('netsh wlan show interface | findstr /i "SSID"') do set "myssid=%i"   & goto next
>set "myssid=mySSID"   & goto next
>set "myssid=mySSID"
>if /I "mySSID" == "SomeSSID" (set "brightness=80" )  ELSE (
set "brightness=30"
 set /p pwd="Enter pwd: "
 powershell -Command "(gc personalWifiProfile.xml) -replace 'password','' | Out-File -encoding ASCII tempProfile.xml"
 pause
)
Enter pwd: 123
Appuyez sur une touche pour continuer...
5
  • Works for me. Are launching the batch-file from the same directory where you have personalWifiProfile.xml ? Commented Jul 24, 2020 at 9:35
  • @Gerhard Yes and it work fine without a variable as argument (first example) but with a variable it replace 'password' by nothing (it juste remove it) Commented Jul 24, 2020 at 9:45
  • Please show the output you get, as I said, I am not experiencing the same issue. also show the output of echo "%pwd%" as screenshot is fine. Commented Jul 24, 2020 at 9:47
  • @Gerhard Edited question accordingly. Commented Jul 24, 2020 at 10:11
  • ok, now that makes way more sense. You are setting and using a variable inside a parenthesized code block, so you need delayedexpansion Commented Jul 24, 2020 at 10:22

1 Answer 1

2

You require delayedexpansion, note the replacement of % with ! in the variable as well !pwd!:


setlocal enabledelayedexpansion
if /I "mySSID" == "SomeSSID" (
      set "brightness=80"
   )  ELSE (
      set "brightness=30"
      set /p pwd="Enter pwd: "
      powershell -Command "(gc personalWifiProfile.xml) -replace 'password','!pwd!' | Out-File -encoding ASCII tempProfile.xml"
      pause
)

I only show part of the code relevant to your issue, so be sure you place setlocal enabledelayedexpansion` in the correct position, should you have other code blocks setting and using variables.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ! I had already tried delayedexpansion without success but I was missing the ! around the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.