在批处理文件中,如何使用Powershell屏蔽输入星号*?

时间:2017-01-01 14:12:27

标签: powershell batch-file hide mask

 cls
 @ECHO OFF
 title Folder Secure
 if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
 if NOT EXIST Secure goto MDLOCKER
 :CONFIRM
 echo (Y/N)
 set/p "cho=>"
 if %cho%==Y goto LOCK
 if %cho%==y goto LOCK
 if %cho%==n goto END
 if %cho%==N goto END
 echo Invalid choice.
 goto CONFIRM
 :LOCK
 ren Secure "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
 attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
 echo Folder locked
 goto End
 :UNLOCK
 set/p "variable=>"
 if NOT %variable%== (Here is Enter Your Password) goto FAIL
 attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
 ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Secure
 echo Folder Unlocked successfully
 goto End
 :FAIL
 echo Invalid password
 goto end
 :MDLOCKER
 md Secure
 echo Secure created successfully
 goto End
 :End

这是我的Echo Code,它用于使用命令隐藏文件夹,y / n并使用密码取消隐藏。它是正常的,我认为没有错误。

但问题是,我需要批处理文件用*。

来屏蔽输入文本

我找到了它:

batch file to mask input with * without an external file

以下是Matt Williamson在批处理文件中使用Powershell进行此操作的方法

set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%pass

在这个精彩的解决方案之后,它可以很好地掩盖我的文字。 但经过一些修改,我无法理解我的密码放在哪里?

有人帮帮我!!我在哪里把密码放在批处理文件中?

以下是最终守则:

 cls
 @ECHO OFF
 title Folder Secure
 if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
 if NOT EXIST Secure goto MDLOCKER
 :CONFIRM
 echo (Y/N)
 set/p "cho=>"
 if %cho%==Y goto LOCK
 if %cho%==y goto LOCK
 if %cho%==n goto END
 if %cho%==N goto END
 echo Invalid choice.
 goto CONFIRM
 :LOCK
 ren Secure "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
 attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
 echo Folder locked
 goto End
 :UNLOCK

set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%pass

 if NOT %pass%== folder goto FAIL
 attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
 ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Secure
 echo Folder Unlocked successfully
 goto End
 :FAIL
 echo Invalid password
 goto end
 :MDLOCKER
 md Secure
 echo Secure created successfully
 goto End
 :End

但是我的密码不被接受。 我不了解Powershell!

1 个答案:

答案 0 :(得分:0)

您必须使用 %% p ,因为您将其存储在那里。其次,你根本不提供任何类似的安全保障。在相应的Powershell中您可以使用Powershell部分下面的代码,它可以帮助您使用自己的密钥加密,也可以解密它。

代替:

set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%pass

使用时应执行此操作:

set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p

Powershell:

该链接的示例是我使用了基于 AES 的加密密码。您可以在代码中加入相同内容。

## For the current user , it can encrypt and decrypt as well
$username = "user1@domain.com"
$pwdTxt = Get-Content $SecurePwdFilePath ;
$securePwd = $pwdTxt | ConvertTo-SecureString ;
$credObject = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $securePwd ;
# You can use the $credObject anywhere in your script now.

希望它能帮助你理解。