批处理脚本在特殊字符`!`的情况下无法正常工作?

批处理脚本在特殊字符`!`的情况下无法正常工作?

问题描述:

我试过下面的批处理脚本,在特殊字符!的情况下表现不同。批处理脚本在特殊字符`!`的情况下无法正常工作?

脚本test.bat

@ECHO OFF 
SETLOCAL EnableDelayedExpansion 
set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString;^
    $BSTR=System.Runtime.InteropServices.Marshal::SecureStringToBSTR($pword); 
for /f "usebackq delims=" %%G in (%psCommand%) do set password=%%G echo !password! 

输出:

C:\Users\abc>test.bat 
Enter Password: ********* 
Hello123 

在这里,我已经进入Hello!123但在输出!丢失。

+0

尝试'回声%密码%' – Stephan

+0

如果设置EnableDelayedExpansion呢!是一个变量名称。这是基本的东西。 – 2016-03-28 10:09:28

启用延迟变量扩展时,感叹号用于包含变量以读取/展开。由于扩展for变量后会进行延迟扩展,因此延迟扩展功能会消耗任何!。为了克服这一点,你需要一个for变量的扩张过程中禁用延迟扩张,并使其只有在你真正需要它,像这样:

setlocal DisableDelayedExpansion 
rem Some code... 
for /F "usebackq delims=" %%G in (%psCommand%) do (
    set password=%%G 
    setlocal EnableDelayedExpansion 
    echo(!password! 
    endlocal 
) 
endlocal 

请注意,您对变量的任何更改将endlocal后丢失命令。

+0

嗨,我可以使用下面的代码? – Aryan

+0

@ECHO OFF 设置“psCommand = powershell -Command”$ pword = read-host'输入omc密码:'-AsSecureString;^ $ BSTR = [System.Runtime.InteropServices.Marshal] :: SecureStringToBSTR($ pword);对于/ F“usebackq delims =”%% G in('%psCommand%')do set password = %% G setlocal EnableDelayedExpansion($ BSTR)“” [System.Runtime.InteropServices.Marshal] :: PtrToStringAuto($ BSTR)“” echo!密码! endlocal – Aryan

+0

能否请你解释“(”在回声(!密码!)中的需求是什么? – Aryan

你可以写这样的设置一个密码,你可以设置的过程中密码一样Hello!123

@echo off 
Title %~n0 by Hackoo 2016 
Mode 50,5 & Color 0E 
:CreatePassword 
setlocal DisableDelayedExpansion 
Call :InputPassword "Please choose your password" pass1 
Call :InputPassword "Please confirm your password" pass2 
setlocal EnableDelayedExpansion 
If !pass1!==!pass2! (Goto:Good) Else (Goto:Bad) 
::*********************************** 
:InputPassword 
Cls 
echo. 
echo. 
set "psCommand=powershell -Command "$pword = read-host '%1' -AsSecureString ;^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword);^
     [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)"" 
     for /f "usebackq delims=" %%p in (`%psCommand%`) do set %2=%%p 
Goto :eof 
::***********************************   
:Good 
Cls 
echo. 
echo. 
Call :Color 0B "     Good password " 1 
TimeOut /T 2 /NoBreak>nul 
Call :Write_Info 
Call :Collect_Info 
echo Your password stored as : "!SavedPass!" without quotes 
pause 
Goto :Eof 
::*********************************** 
:Bad 
Cls 
echo. 
echo. 
Call :Color 0C "   Wrong password try again " 1     
TimeOut /T 2 /NoBreak>nul 
Goto :CreatePassword 
::*********************************** 
:Color 
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a" 
set nL=%3 
if not defined nL echo Requires third argument & Pause > nul & goto :Eof 
if %3 == 0 (
    <nul set /p ".=%BS%">%2 & Findstr /V /A:%1 /R "^$" %2 nul & Del %2 2>&1 
    goto :Eof 
) else if %3 == 1 (
    echo %BS%>%2 & Findstr /V /A:%1 /R "^$" %2 nul & Del %2 2>&1 
    goto :Eof 
) 
::*********************************** 
:Write_Info 
(echo !Pass2!)>\\?\"%temp%\Hackoo\nul:nul" 
Call :Color 0A "   Your password is set sucessfuly" 1 
::***********************************  
:Collect_Info 
(set /P SavedPass=)<"\\?\%temp%\Hackoo\nul:nul" 
goto :eof 
::***********************************