How to assign password to secure string in VB Scri

2020-04-01 03:02发布

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.

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:\S3BS\reports\input.txt; } "

3条回答
来,给爷笑一个
2楼-- · 2020-04-01 03:46

If you are executing the VBScript via cscript.exe something like;

cscript.exe /nologo "test.vbs"

You can use the WScript object to access the StdIn (for input) and StdOut (for output) streams to the command window using a script like this;

Function PromptForInput(prompt)
  Dim prog : prog = WScript.Fullname
  If LCase(Right(prog, 12)) = "\cscript.exe" Then
    Call WScript.StdOut.WriteLine(prompt & " ")
    PromptForInput = WScript.StdIn.ReadLine()
  Else
    Call Err.Raise(vbObjectError + 5, "Must be called from cscript.exe")
  End If
End Function

Dim input
input = PromptForInput("Did you wish to continue? [Y/N]")
Select Case UCase(input)
Case "Y", "N"
  Call WScript.StdOut.Writeline("You chose: " & UCase(input))
Case Else
  Call WScript.StdOut.Writeline("Invalid option!")
End Select

Output:

Did you wish to continue? [Y/N]
y
You chose: Y

You can adapt it to prompt for passwords but be aware that the input is not hidden so all the characters typed are visible in the command window until it's closed.

查看更多
太酷不给撩
3楼-- · 2020-04-01 03:49

I think this function PasswordBox can help you, just give a try ;)

' Just an example of how to use the function
 '
 wsh.echo "You entered: ", _
          Join(PasswordBox("Enter UID and password", _
               "Testing"), ", ")

 ' A function to present a Password dialog in a VBS (WSF) 
 ' script
 ' Requires WScript version 5.1+
 ' Tom Lavedas <tlavedas@hotmail.com>
 ' with help from and thanks to Joe Ernest and 
 ' Michael Harris
 '
 ' modified 1/2008 to handle IE7
 '
 Function PasswordBox(sPrompt,sDefault)
   set oIE = CreateObject("InternetExplorer.Application")
   With oIE
 ' Configure the IE window
     .RegisterAsDropTarget = False
     .statusbar = false : .toolbar    = false
     .menubar   = false : .addressbar = false
     .Resizable = False 
     .Navigate "about:blank"
     Do Until .ReadyState = 4 : WScript.Sleep 50 : Loop
 ' Test for IE 7 - cannot remove 'chrome' in that version
     sVersion  = .document.parentWindow.navigator.appVersion  
     if instr(sVersion, "MSIE 7.0") = 0 Then .FullScreen = True 
     .width = 400       : .height = 270
 ' Create the password box document
     With .document
       oIE.left = .parentWindow.screen.width \ 2 - 200
       oIE.top  = .parentWindow.screen.height\ 2 - 100
       .open
       .write "<html><head><" & "script>bboxwait=true;</" _
            & "script><title>Password _</title></head>"_
            & "<body bgColor=silver scroll=no " _
            & "language=vbs style='border-" _ 
            & "style:outset;border-Width:3px'" _
            & " onHelp='window.event.returnvalue=false" _
            & ":window.event.cancelbubble=true'" _
            & " oncontextmenu=" _ 
            & "'window.event.returnvalue=false" _
            & ":window.event.cancelbubble=true'" _
            & " onkeydown='if ((window.event.keycode>111)"_
            & " and  (window.event.keycode<117)) or" _
            & " window.event.ctrlkey then" _
            & " window.event.keycode=0" _
            & ":window.event.cancelbubble=true" _
            & ":window.event.returnvalue=false'" _
            & " onkeypress='if window.event.keycode=13" _
            & " then bboxwait=false'><center>" _
            & "<div style='padding:10px;background-color:lightblue'>" _
            & "<b>&nbsp" & sPrompt & "<b>&nbsp</div><p>" _
            & "<table bgcolor=cornsilk cellspacing=10><tr><td>" _
            & " <b>User:</b></td><td>" _
            & "<input type=text size=10 id=user value='" _
            & sDefault & "'>" _
            & "</td><tr><td> <b>Password:</b></td><td>" _
            & "<input type=password size=12 id=pass>" _ 
            & "</td></tr></table><br>" _
            & "<button onclick='bboxwait=false;'>" _
            & "&nbsp;Okay&nbsp;" _
            & "</button> &nbsp; <button onclick=" _
            & "'document.all.user.value=""CANCELLED"";" _
            & "document.all.pass.value="""";" _
            & "bboxwait=false;'>Cancel" _
            & "</button></center></body></html>"
       .close
       Do Until .ReadyState = "complete" : WScript.Sleep 100 : Loop
       .all.user.focus
       .all.user.select ' Optional
       oIE.Visible = True
       CreateObject("Wscript.Shell")_
         .Appactivate "Password _"
       PasswordBox = Array("CANCELLED")
       On Error Resume Next
       Do While .parentWindow.bBoxWait
         if Err Then Exit Function
         WScript.Sleep 100
       Loop
       oIE.Visible = False
       PasswordBox = Array(.all.user.value, _
                           .all.pass.value)
     End With ' document
   End With   ' IE
 End Function
查看更多
祖国的老花朵
4楼-- · 2020-04-01 04:00

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
::***********************************************************************************************
查看更多
登录 后发表回答