使用变量从批处理文件中调用 powershell 替换

亚历克西斯

我需要用批处理文件中的 xml 文件中的用户输入替换字符串,我通过使用 powershell“replace”命令并从批处理中调用它来使其工作,但我无法弄清楚如何在 powershell 中使用批处理变量命令。

在职的 :

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

不工作:

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

信息:我使用的是 Windows 7

编辑:使用更简单的脚本,它可以工作,所以问题似乎来自我脚本中的其他地方(亮度部分工作正常)。

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%"

测试和真实脚本之间的执行顺序似乎不同。这是工作测试脚本:

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

有了它的输出:

>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...

以及完整脚本的输出:

>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...
格哈德

您需要delayedexpansion,请注意变量中%with替换!!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
)

我只显示了与您的问题相关的部分代码,因此setlocal如果您有其他代码块设置和使用变量,请确保将enabledelayedexpansion放置在正确的位置。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Powershell:如何替换批处理文件中带引号的文本

批处理文件将“ =”替换为“?” 在文件或变量中

使用批处理文件替换 csv 文件中的 (C*)

在批处理文件中调用Powershell命令

在Powershell中设置批处理文件的变量

从批处理文件调用PowerShell脚本

使用powershell编辑Windows批处理文件中的文件

在批处理文件中调用Powershell脚本时,如何使用嵌套引号?

喜欢在变量中获取Powershell脚本的输出以在批处理文件中使用

从Powershell调用批处理文件(批处理文件调用.exe)

Powershell中的部分替换

替换Powershell中的'字符

使用批处理文件比较和替换文件

替换批处理文件变量进给ffmpeg程序中的特殊字符

用于替换批处理文件中的延迟扩展变量

通过PowerShell调用批处理文件的问题

将Powershell变量传递到Windows批处理文件

将 Powershell 变量传递给批处理文件

直接在Powershell中测试批处理语句,而无需将调用包装在批处理文件中

如何用&替换&在批处理文件中?

批处理文件中的字符串替换问题

使用批处理文件替换字符(替代SED)

如何使用批处理文件替换Windows文件名中的单个字符?

批处理文件仅使用整个单词在文本文件中查找和替换?

需要使用批处理文件将csv文件中的所有记录的“””替换为“”

使用批处理文件替换csv文件中的字符串

如何使用批处理文件以递归方式替换文件中的字符串?

使用所有参数从批处理文件调用PowerShell脚本

尝试在一个位置使用批处理文件并调用Powershell脚本提升