如何在 VB 脚本中为安全字符串分配密码字符串、中为、脚本、分配

2023-09-06 23:35:22 作者:八百逗比奔北波

我有一个脚本,其中包含生成 powershell 脚本的批处理文件.我自己承担了通过 VB 脚本完成相同的任务.到目前为止,我已经将我需要的大部分信息分配给了字符串.但是我希望提示输入密码,该密码存储为安全字符串,并且可以输出到文本文件中以供以后在进一步的脚本中使用.到目前为止,我发现的唯一代码不起作用,我想可能是因为它是为 VB 而不是 VBS 设计的.非常感谢任何帮助.

I have a script comprising of batch files that generate powershell scripts. I've taken it upon myself to accomplish the same task via VB Script. So far I've assigned most of the info I need to strings. But I would like to have a prompt for a password that is stored as a secure string and can be outputted to a text file for later use in further scripts. So far the only code I've found doesn't work I think perhaps because it was intended for VB rather than VBS. Any help greatly appreciated.

之前使用的powershell代码是.

The powershell code previously used was.

    echo Please enter admin credentials (This will be stored in a secure string:
    powershell -Command "& { read-host -assecurestring | convertfrom- securestring | out-file C:S3BSeportsinput.txt; } "

推荐答案

这个小代码可以配合powershell和batch使用

You can use this small code with powershell and batch

@ECHO OFF
Title Type a password with powershell and batch
:CheckPassword
Mode con cols=50 lines=3
cls & color 0A & echo.
set MyPassword=Hackoo
set "psCommand=powershell -Command "$pword = read-host 'Enter your 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
if %MyPassword%==%password% (Goto:Good) else (Goto:Bad)
exit/b
::***********************************************************************************************
:Good
Cls & Color 0A
echo(
echo                   Good Password
TimeOut /T 2 /NoBreak>nul
Exit
::***********************************************************************************************
:Bad
Cls & Color 0C
echo(
echo                   Bad password
TimeOut /T 1 /NoBreak>nul
Goto:CheckPassword
::***********************************************************************************************