我有一个脚本,其中包含生成Powershell脚本的批处理文件。我已经通过VB脚本完成了相同的任务。到目前为止,我已经将所需的大部分信息分配给字符串。但是我希望提示输入一个存储为安全字符串的密码,并且可以输出到文本文件以供以后在进一步的脚本中使用。到目前为止,我发现的唯一代码不起作用,我认为可能是因为它是为 VB 而不是 VBS 设计的。任何帮助非常感谢。
以前使用的Powershell代码是。
echo Please enter admin credentials (This will be stored in a secure string:
powershell -Command "& { read-host -assecurestring | convertfrom- securestring | out-file C:S3BSreportsinput.txt; } "
您可以将此小代码与 powershell 和批处理一起使用
@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
::***********************************************************************************************
我认为这个函数PasswordBox可以帮助你,试一试;)
' 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> " & sPrompt & "<b> </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;'>" _
& " Okay " _
& "</button> <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
如果你通过cscript.exe
执行 VBScript,类似;
cscript.exe /nologo "test.vbs"
您可以使用 WScript
对象访问命令窗口的StdIn
(用于输入)和StdOut
(用于输出)流,使用如下所示的脚本;
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
输出:
Did you wish to continue? [Y/N]
y
You chose: Y
您可以调整它以提示输入密码,但请注意,输入不会隐藏,因此键入的所有字符在命令窗口中可见,直到它关闭。