【问题标题】:In Batch File, How to mask Input With asterisk * using Powershell?在批处理文件中,如何使用 Powershell 用星号 * 屏蔽输入?
【发布时间】:2017-05-15 22:16:19
【问题描述】:
 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 代码,用于使用命令隐藏文件夹,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一无所知!

【问题讨论】:

标签: powershell batch-file hide mask


【解决方案1】:

你必须使用 %%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.

希望能帮助你理解。

【讨论】:

  • @AliHyder :如果对您有帮助,请接受答案。
  • 但这对我不起作用:(你能完全为我做吗?我知道它不安全,但我只在家里使用就足够了。我不是一个大程序员但仍然是初学者。并且不了解powershell。
  • @AliHyder:这就是为什么我给了你出发的逻辑。至少你先试试。那么我们随时为您提供帮助;
  • 当我放P而不是pass时,它不起作用,什么也没做,你能先在你的电脑上检查一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
  • 1970-01-01
相关资源
最近更新 更多