2016-03-28 91 views
2

我試過下面的批處理腳本,在特殊字符!的情況下表現不同。批處理腳本在特殊字符`!`的情況下無法正常工作?

腳本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

回答

3

啓用延遲變量擴展時,感嘆號用於包含變量以讀取/展開。由於擴展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

0

你可以寫這樣的設置一個密碼,你可以設置的過程中密碼一樣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 
::*********************************** 
相關問題