当用户输入PIPE(' |')作为密码中的一个字符
时,我的test.bat崩溃了代码:
Set vAppPoolUser=
Set /p vAppPoolUser= Enter MicroStrategyWebPool user name (domain\username):
Echo.
setlocal enabledelayedexpansion
set vAppPoolUserPswd=
set "psCommand=powershell -Command "$pword = read-host 'MicroStrategyWebPool password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set "vAppPoolUserPswd=%%p"
Echo !vAppPoolUserPswd!
Echo.
------------------另一个验证---------------------
的块REM verify user credentials
setlocal enabledelayedexpansion
set vCredenvalid=
Set "vpsCommand=Powershell -Command "(new-object directoryservices.directoryentry '','%vAppPoolUser%','%vAppPoolUserPswd%').psbase.name -ne $nul""
for /f "usebackq delims=" %%p in (`%vpsCommand%`) do set vCredenvalid=%%p
Echo !vCredenvalid!
if %vCredenvalid%==False (
call :ColorTextMain 0C "Incorrect username or password"
Echo.
REM verify user credentials
setlocal enabledelayedexpansion
set vCredenvalid=
Set "vpsCommand=Powershell -Command "(new-object directoryservices.directoryentry '','%vAppPoolUser%','!vAppPoolUserPswd!').psbase.name -ne $nul""
for /f "usebackq delims=" %%p in (`%vpsCommand%`) do set vCredenvalid=%%p
Echo !vCredenvalid!
if !vCredenvalid!==False (
call :ColorTextMain 0C "Incorrect username or password"
Echo.
goto :readIISUser
)
echo.
echo Credentials Validated......
echo.
echo Applicaiton message.......Credentials Validated >>%vLog_FileLocation%
REM set apppool user
Echo Application Message.....Setting MicroStrategyWebPool user >>%vLog_FileLocation%
Echo Setting MicroStrategyWebPool user
"%vAppcmdpath%\appcmd.exe" set config /section:applicationPools /[name='MicroStrategyWebPool'].processModel.identityType:SpecificUser /[name='MicroStrategyWebPool'].processModel.userName:%vAppPoolUser% /[name='MicroStrategyWebPool'].processModel.password:!vAppPoolUserPswd! >>%vLog_FileLocation%
REM restart appoool
"%vAppcmdpath%\appcmd.exe" recycle apppool /apppool.name:MicroStrategyWebPool
输出:
输入WebPool用户名(域\用户名):testUser
WebPool密码:{} |
密码后按Enter键时崩溃
答案 0 :(得分:1)
您需要使用^
为某些人转义特殊字符,为其他人使用%
。
对于这一点,你只需要setlocal enabledelayedexpansion
@echo off
setlocal enabledelayedexpansion
set /p "pass=password: "
!pass!
在这种情况下,我输入密码pass|word
,它将回显passw|word
我无法测试您的实际示例,但您的代码应该是这一行:
@echo off
setlocal enabledalayedexpansion
Set vAppPoolUser=
Set /p "vAppPoolUser=Enter WebPool user name (domain\username): "
Echo.
set vAppPoolUserPswd=
set "psCommand=powershell -Command "$pword = read-host 'WebPool password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set "vAppPoolUserPswd=%%p"
Echo !vAppPoolUserPswd!
修改强>
至于在评论中逃避其他字符的问题。通常,大多数其他特殊字符是enabledelayedexpansion
处理的句柄,但在设置变量后,您通常需要转义!
,有时^
:
for /f "usebackq delims=" %%p in (`%psCommand%`) do set "vAppPoolUserPswd=%%p"
set "vAppPoolUserPswd=!vAppPoolUserPswd:^^=^^!"
set "vAppPoolUserPswd=!vAppPoolUserPswd:^^!=^^!!"
echo !vAppPoolUserPswd!
从那里你需要用%vAppPoolUserPswd%
替换任何!vAppPoolUserPswd!
变量delayedexpansion
才能做到这一点。