2016-11-22 25 views
2

我有一個批處理文件,它接受用戶輸入的密碼。由於某種原因,它不需要最後一個字符,我無法弄清楚。批處理文件中未設置最後一個字符的密碼

powershell -Command $pword = read-host "Enter password " -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt & set /p password=<.tmp.txt & del .tmp.txt 
echo psexec \\%computerName% -u %domainName%\%userName% -p %password% cmd 

我知道這是不是PowerShell命令,因爲我與set /p password="Enter password: "測試它仍然無法呼應的最後一個字符。

按我的意見,如果我使用的語句這裏是:

set /P domainName= 
    set params = %*:"=""?! 
    if "%domainName%" == "1" (
     set "domainName=domain1" 
     goto nextstep 
    ) 
    if "%domainName%" == "2" (
     set "domainName=domain2" 
     goto nextstep 
    ) 
    if "%domainName%" == "3" (
     set "domainName=domain3" 
     goto nextstep 
    ) else (
     goto resetfile 
    ) 

    :nextstep 

我的問題是2的3個領域,PSEXEC運行的那個。對於domain1,它不會運行。我回應了這個命令,並且它完美地打印出來。當我從命令中刪除echo時,它跳過所有這些並重新啓動文件。

nextstep是:

:nextstep 
    set /p userName="Enter your Admin username: " 
    powershell -Command $pword = read-host "Enter password " -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt & set /p password=<.tmp.txt & del .tmp.txt 
    psexec \\%computerName% -u %domainName%\%userName% -p %password% cmd 
    :resetfile 
    set resetfile= 
    set /p resetfile="Do you want to restart this file? Press 'y' for Yes or 'n' for No then Press ENTER: " 
    if not '%resetfile%'=='' set resetfile=%resetfile:~0,1% 
    if '%resetfile%'=='y' goto start 

腳本甚至沒有問我是否要重新啓動腳本。它只是回到:start,這是在腳本的開始。

請幫忙,謝謝!

+0

使用它,我想密碼的最後一個字符是像'&^%'或'!'特殊字符和延遲擴充被啓用。嘗試'echo psexec.exe「\\%computerName%」-u「%domainName%\%userName%」-p「%password%」cmd「。任何情況下,執行'psexec.exe'都應該使用雙引號。但是,如果密碼包含啓用延遲擴展的'%'或'!',則最好使用'echo psexec.exe「\\%computerName%」-u「%domainName%\%userName%」-p「!password!」 cmd「,當然還有'setlocal EnableDelayedExpansion'。 – Mofi

+4

老實說,難道你不能只是從PowerShell運行psexec,而不是做一個讓SecureString再次變得不安全的糟糕舞蹈嗎? – Joey

+0

@joey我想我可以。 –

回答

0

首先嚐試這與您的密碼,看它是否是完整的

setlocal EnableDelayedExpansion 
set /p password= 
echo my password '!password!' 

如果仍然失敗,必須有參與非ASCII字符。
然後你應該改變與chcp的代碼頁。

然後你就可以與您的代碼

psexec \\%computerName% -u %domainName%\%userName% -p !password! cmd 
+0

除非設置和讀取變量發生在同一塊('if','for'),否則不需要'EnableDelayedExpansion'。 – Melebius

+5

@Melebius嘗試沒有延遲擴展和我的默認密碼'^ 1&「&',那麼你知道你爲什麼要使用延遲擴展 – jeb

+0

@jeb @Melebius我實際上有'setlocal EnableDelayedExpansion'在我的文件中,但我刪除它,現在,我的問題是,腳本中的3個域中有2個運行了psexec comamd,但是對於一個域,它不起作用,我可能需要提出一個新問題。出來psexec命令,它打印正確,沒有任何拼寫錯誤,我將添加我在問題中使用的if語句 –

相關問題