有没有办法在 vbs 中输入 hide/mask?

Is there a way to hide/mask an input in vbs?

我正在用vbs做一个简单的用户名密码存储程序。输入用户名后,我需要输入密码。但是,我不能找到一种不以明文形式显示输入并将其转换为 ****** 的方法 这是我已经拥有的:

x=MsgBox("VBScript username and password storage")
username = InputBox("Please enter your username", "Credentials storage", "Your username goes here")
passwd = InputBox("Please enter your password", "Credentials storage", "Your password goes here")

Set obj = CreateObject("Scripting.FileSystemObject")   
Const ForWriting = 2               
Set obj1 = obj.OpenTextFile("test.txt", ForWriting) 
obj1.WriteLine(username & " " & passwd)      
obj1.Close                                                                  
Set obj=Nothing   

我也试过这样做,我发现这是另一个问题的答案

Set oInput = CreateObject("ScriptPW.Password")
WScript.StdOut.Write "Enter password: "
pw = oInput.GetPassword

但是当我 运行 它说“ActiveX 组件无法创建对象”ScriptPW.Password“

有没有办法隐藏第 3 行中的文本或解决我的问题?

你可以试试这个功能PasswordBox

PasswordBox.vbs

Option Explicit
Dim bPasswordBoxWait,bPasswordBoxOkay,Password
Password = PasswordBox("Veuillez taper votre mot de passe",False)
wscript.echo Password
'----------------------------------------------------------------------------'
Function PasswordBox(sTitle,FullScreen) 
    Dim oIE
    set oIE = CreateObject("InternetExplorer.Application") 
    With oIE
        If  FullScreen = True Then 
            .FullScreen = True
        Else 
            .FullScreen = False
        End if  
        .ToolBar   = False : .RegisterAsDropTarget = False 
        .StatusBar = False : .Navigate("about:blank") 
        .Resizable = False
        While .Busy : WScript.Sleep 100 : Wend 
            With .document 
                .Title = "Veuillez taper votre mot de passe * * * * * * * * * * * * *"
                With .ParentWindow 
                    .resizeto 350,100 
                    .moveto .screen.width/2-200, .screen.height/2-50 
                End With 
                .WriteLn("<html><title>Veuillez taper votre mot de passe * * * * * * *</title><body text=white bgColor=DarkOrange><center>") 
                .WriteLn(sTitle) 
                .WriteLn("<input type=password id=pass>" & _ 
                "<input type=Submit id=but0 value=Envoyer>") 
                .WriteLn("</center></body></html>") 
                With .ParentWindow.document.body 
                    .scroll="no" 
                    .style.borderStyle = "outset" 
                    .style.borderWidth = "1px" 
                End With 
                .all.but0.onclick = getref("PasswordBox_Submit") 
                .all.pass.focus 
                oIE.Visible = True 
                bPasswordBoxOkay = False : bPasswordBoxWait = True 
                On Error Resume Next 
                While bPasswordBoxWait 
                    WScript.Sleep 100 
                    if oIE.Visible Then bPasswordBoxWait = bPasswordBoxWait 
                    if Err Then bPasswordBoxWait = False 
                Wend 
                PasswordBox = .all.pass.value 
            End With
            .Visible = False
            .Quit 
        End With
    End Function 
'----------------------------------------------------------------------------'
Sub PasswordBox_Submit() 
    bPasswordBoxWait = False 
End Sub
'----------------------------------------------------------------------------'

玩过这个,从来没有需要它。到目前为止,我可怜的人的解决方案是这样的。我还不知道是否可以创建一个可以直接在 VBScript 中使用的 powershell 对象,我认为那里存在固有的障碍,因为 PS 是 .Net。在这种情况下,我仍然 return 密码 return 由 Get-Credential 编辑为纯文本,所以认为这是一个 hack。它只屏蔽输入框。毫无疑问,有更好的解决方案。从好的方面来说:这段代码非常小,而且——重要的是——不依赖于 IE。

Option Explicit

Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2
Dim objShell, oExec, strOutput, strPS1Cmd

'create Powershell command
'Note that we manipulate the credentials object into a plain string
strPS1Cmd = "& { $cred = Get-Credential; Write-Output ($cred.Username + '|and-now-comes-the-password-in-plaintext|' + $cred.GetNetworkCredential().Password) } "

' Create a shell and execute powershell, pass the script    
Set objShell = WScript.CreateObject("wscript.shell")
Set oExec = objShell.Exec("powershell -windowstyle hidden -command """ & strPS1Cmd & """ ")

Do While oExec.Status = WshRunning
     WScript.Sleep 100
Loop

Select Case oExec.Status
   Case WshFinished
       strOutput = oExec.StdOut.ReadAll()
   Case WshFailed
       strOutput = oExec.StdErr.ReadAll()
 End Select

WScript.Echo(strOutput)