vbscript显示框乱码_在VBScript中启用用户ID和密码提示

vbscript显示框乱码

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealing with remote computer administration scripts.

在为客户端/服务器计算环境编写脚本时,必须考虑某种在脚本中启用身份验证功能的方法。 当我们处理远程计算机管理脚本时,这种考虑主要体现在画面中。

Many of us who are working on VBScripts and automation tasks might have encountered a need for prompting the user to provide the login credentials (Username/Password), to perform some task during script execution. It's not always safe to include the logon credentials as part of the code itself because these files are in a plain text format and anyone can read it.

我们中许多从事VBScript和自动化任务的人员可能会遇到提示用户提供登录凭据(用户名/密码)以在脚本执行过程中执行某些任务的需求。 将登录凭据作为代码本身的一部分并不总是安全的,因为这些文件为纯文本格式,任何人都可以阅读。

If you’re running Windows XP or Windows Server 2003, you can use ScriptPW (a COM object found only in those two versions of Windows) to mask passwords from the command line. Here’s a sample script that creates an instance of the ScriptPW.Password object and then uses the StdOut Write method to request that the user enter a password:

如果您运行的是Windows XP或Windows Server 2003,则可以使用ScriptPW(仅在这两个Windows版本中找到的COM对象)从命令行屏蔽密码。 这是一个示例脚本,该脚本创建ScriptPW.Password对象的实例,然后使用StdOut Write方法请求用户输入密码:

Set objPassword = CreateObject("ScriptPW.Password") 
WScript.StdOut.Write "Please enter your password:" 

strPassword = objPassword.GetPassword() 
Wscript.Echo
Wscript.Echo "Your password is: " & strPassword

When you run this script, the message "Please enter your password:" appears on the screen. At that point, the script will pause and wait for you to type in a password; you simply type the password and press ENTER. This line of code will then grab the password and store it in the variable strPassword:

当您运行此脚本时,消息“请输入密码:”出现在屏幕上。 届时,脚本将暂停并等待您输入密码。 您只需键入密码,然后按Enter。 然后,以下代码行将获取密码并将其存储在变量strPassword中:

strPassword = objPassword.GetPassword()

For this simple script, we just echo back the password typed in to prove that the keystrokes really were captured. We do this because with the GetPassword() method, the keystrokes you type are not displayed onscreen. In other words, the password gets masked.

对于这个简单的脚本,我们只回显键入的密码,以证明确实捕获了击键。 我们这样做是因为使用GetPassword()方法时,您键入的按键不会显示在屏幕上。 换句话说,密码被屏蔽了。

But this might not serve as a solution to our problem since there are a few drawbacks with this object.  To illustrate, if you try to run this script just by double-clicking the .vbs file (which includes this code) it won't work. To be able to run this code you need to go to a command prompt and run the script by giving the command CSCRIPT scriptPath\scriptFileName.vbs and this can be very inconvenient.  Another drawback is that it only works on two versions of Microsoft Windows.

但这可能不能解决我们的问题,因为此对象有一些缺点。 为了说明这一点,如果您尝试仅通过双击.vbs文件(包括此代码)来运行此脚本,它将无法正常工作。 为了能够运行此代码,您需要转到命令提示符并通过提供命令CSCRIPT

By default in VBScript you can't find any functionality which could ask a user to provide the userID and password in a GUI environment.  Hence I found an article with code at the Microsoft Scripting Guy site to get these credentials through a web page. I have tweaked the code, which you can find below, so that one can easily use the code to add this functionality to their script without having to first write a html file to ask for the username and password. I am sharing this code as given below, and hope this will help someone who may need it:

默认情况下,在VBScript中找不到任何可以要求用户在GUI环境中提供用户ID和密码的功能。 因此,我在Microsoft脚本专家网站上找到了一篇带代码的文章,以通过网页获取这些凭据。 我已经对代码进行了调整,您可以在下面找到它,以便人们可以轻松地使用代码将此功能添加到他们的脚本中,而无需首先编写html文件来询问用户名和密码。 我正在共享下面给出的这段代码,希望这对可能需要它的人有所帮助:

You can save this code in a .vbs file to use it.

您可以将此代码保存在.vbs文件中以使用它。

'=======================[ ASK Password ]========================================'
Option Explicit
Dim strUserID, strPassword

AskPassword

Sub AskPassword()
Dim htmlPwdCode, objCodeFile, objFileSysObj, objBrowser, strButton
Const FOR_WRITING = 2

Set objFileSysObj = CreateObject("Scripting.FileSystemObject")

htmlPwdCode = "<SCRIPT LANGUAGE=" & Chr(34) & "VBScript" & Chr(34) & ">" & Chr(13) & _
"Sub RunScript" & Chr(13) & _
"    OKClicked.Value = " & Chr(34) & "OK"& Chr(34) & Chr(13) & _
"End Sub" & Chr(13) & _
"Sub CancelScript" & Chr(13) & _
"    OKClicked.Value = " & Chr(34) & "Cancelled" & Chr(34) & Chr(13) & _
"End Sub" & Chr(13) & _
"Sub Default_Buttons" & Chr(13) & _
"	If Window.Event.KeyCode = 13 Then" & Chr(13) & _
"		btnOK.Click" & Chr(13) & _
"	End If" & Chr(13) & _
"End Sub" & Chr(13) & _
"</SCRIPT>" & Chr(13) & _
"<BODY onkeypress='vbs:Default_Buttons'><center><font size=" & Chr(34) & "2" & Chr(34) & " face=" & Chr(34) & "Arial" & Chr(34) & ">" & Chr(13) & _
"User name:&nbsp;&nbsp;&nbsp;" & Chr(13) & _
"<input type=" & Chr(34) & "text" & Chr(34) & " name=" & Chr(34) & "UserName" & Chr(34) & " size=" & Chr(34) & "30" & Chr(34) & "><br>" & Chr(13) & _
"Password :&nbsp;&nbsp;&nbsp; </font><font face=" & Chr(34) & "Arial" & Chr(34) & ">" & Chr(13) & _
"<input type=" & Chr(34) & "password" & Chr(34) & " name=" & Chr(34) & "UserPassword" & Chr(34) & _
" size=" & Chr(34) & "30" & Chr(34) & "></font></p>" & Chr(13) & _
"<input type=" & Chr(34) & "hidden" & Chr(34) & " name=" & Chr(34) & "OKClicked" & Chr(34) & " size = " & Chr(34) & "20" & Chr(34) & ">" & Chr(13) & _
"<input id=" & Chr(34) & "btnOK" & Chr(34) & " class=" & Chr(34) & "button" & Chr(34) & _
" type=" & Chr(34) & "button" & Chr(34) & " value=" & Chr(34) & " OK " & Chr(34) & _
" name=" & Chr(34) & "ok_button" & Chr(34) & " onClick=" & Chr(34) & "RunScript" & Chr(34) & ">" & Chr(13) & _
"<input id=" & Chr(34) & "btnCancel" & Chr(34) & " class=" & Chr(34) & "button" & Chr(34) & _
" type=" & Chr(34) & "button" & Chr(34) & " value=" & Chr(34) & "Cancel" & Chr(34) & _
" name=" & Chr(34) & "cancel_button" & Chr(34) & " onClick=" & Chr(34) & "CancelScript" & Chr(34) & "></center></BODY>"

Set objCodeFile = objFileSysObj.CreateTextFile("AskPassword.html", True)
objCodeFile.Write htmlPwdCode
objCodeFile.Close
Set objCodeFile = Nothing

Set objBrowser = CreateObject("InternetExplorer.Application")

With objBrowser
	.Height = 170
	.Width = 400
	.Top = 200
	.Left = 300
	.StatusBar = True
	.Toolbar = False
	.Resizable = False
	.Navigate CreateObject("Scripting.FileSystemObject").GetParentFolderName(Wscript.ScriptFullName) & "\AskPassword.html"
	.Visible = True
End With

Do Until objBrowser.ReadyState = 4
'wait till page loads'
Loop

Do While objBrowser.Document.Body.All.OKClicked.Value = ""
    Wscript.Sleep 50                 
Loop 

strUserID = objBrowser.Document.Body.All.UserName.Value
strPassword = objBrowser.Document.Body.All.UserPassword.Value
strButton = objBrowser.Document.Body.All.OKClicked.Value

objBrowser.Quit

If strButton = "Cancelled" Then
	MsgBox "Operation cancelled, script will now exit!"
	Wscript.Quit
Else
	'Credentials accepted for further processing
End If
objFileSysObj.DeleteFile "AskPassword.html", True

Set objBrowser = Nothing
Set objFileSysObj = Nothing
End Sub
'=======================[ GOT Password ]========================================'

HOW TO USE THIS CODE:

如何使用此代码:

1) Copy this code

1)复制此代码

2) and paste the same into notepad/your vbscript code

2)并将其粘贴到记事本/您的vbscript代码中

3) Put the name of the procedure AskPassword in the code where you want the authentication process to start

3)将过程AskPassword的名称放在要启动身份验证过程的代码中

4) The values captured for username and password will be stored in global variable names strUserID and strPassword respectively. You can refer to the UserID and password captured using this procedure in the code, after the call to this AskPassword procedure

4)为用户名和密码捕获的值将分别存储在全局变量名称strUserID和strPassword中。 调用此AskPassword过程后,可以在代码中引用使用此过程捕获的UserID和密码。

Please feel free to contact me in case you need any further clarification on how to use this code.

如果您需要进一步了解如何使用此代码,请随时与我联系。

Regards,

问候,

SavindraSingh

萨文德拉·辛格

翻译自: https://www.experts-exchange.com/articles/4332/Enable-UserID-and-Password-Prompt-in-VBScript.html

vbscript显示框乱码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值