如何自动登录使用VBS脚本 [英] How to Auto Login using VBS script

查看:208
本文介绍了如何自动登录使用VBS脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是新来这个。我目前正在为一个项目在我们这里的办公室,我已经对如何在一个窗口中打开多标签IE脚本。这里是我使用它的脚本。

I'm just new to this. I am currently working for a project here in our office and I already have the script on how to open multi-tab IE in one window. Here is the script that I use for it.

Const navOpenInBackgroundTab = &H1000

site1 = "http://site1.com/"
site2 = "https://site2.com"



Set oIE = CreateObject("InternetExplorer.Application")
oIE.Visible = True
oIE.Navigate2 site1
oIE.Navigate2 site2,navOpenInBackgroundTab



Set oIE = Nothing 

但其中两个网站都需要登录信息。我想知道的脚本,让我自动登录这些网站。我想知道我需要在脚本中添加上述这样它会自动记录在每一个我使用的脚本的时间。

But two of these websites needs log in information. I want to know the script that will allow me to auto log in to these websites. I was wondering what I need to add on the script above so that it will auto-log in every time I use the script.

下面是网站之一:
https://myapps.uhc.com/Citrix/AccessPlatform/auth/login.aspx

我希望有人能帮助我指点迷津,谢谢

I hope someone can help me figure it out, thanks

推荐答案

长话短说,你使用Firefox + Greasemonkey的或Chrome + Tampermonkey有一个更容易的时间。

Long story short, you'd have an easier time using Firefox + Greasemonkey or Chrome + Tampermonkey.

枚举背景标签需要使用 Shell.Application 对象 .Windows()的方法。模棱两可的窗口对象似乎打破 DOMelement.parentNode 和类似的,所以每次你导航DOM,你必须重新建立开始 tabCollection.item(我).document 。是用户名字段中指定的用户,用户名,登录名,登录ID,电子邮件或其他什么东西?你可以尝试用正则表达式predict,但有可能会出现一些试错参与,如果你打算把这个脚本扩展用于登录到其他网站。

Enumerating background tabs requires the use of Shell.Application object .Windows() method. The ambiguous window object seems to break DOMelement.parentNode and similar, so that each time you navigate the DOM you have to re-establish the complete hierarchy starting with tabCollection.item(i).document. Is the username field named user, username, login name, login ID, email, or something else? You can try to predict it with a regex, but there'll probably be some trial and error involved if you plan to extend this script for logging into other sites.

在不可能的零件,虽然是表单提交。请问表单提交调用回发到设定张贴之前隐藏的输入值?如果是这样,那么 form.submit()提交前可能不会闪光必要的事件,导致AUTH失败。这是我测试过两个网站的情况。

The impossible part, though, is form submission. Does the form submission call a postback to set hidden input values prior to post? If so, then form.submit() might not fire the necessary events before submission, resulting in an auth failure. This was the case for two sites I tested.

如果你能火点击()提交按钮的情况下,这可能帮助。但提交按钮一个按钮?输入类型=按钮?一个图像?一个程式化的超级链接?一个DIV? OK,所以也许发送<大骨节病>在密码字段中输入会更好。我一直在尝试与 initKeyboardEvent passwordField.dispatchEvent(EVT)的几个小时,现在,它似乎在这里再次,暧昧窗口 Shell.Application 生成的对象是preventing成功。

If you could fire the click() event of the submit button, that might help. But is the submit button a button? An input type=button? An image? A stylized hyperlink? A div? OK, so maybe sending Enter in the password field would be better. I've been experimenting with initKeyboardEvent and passwordField.dispatchEvent(evt) for a couple of hours now, and it seems that here again, the ambiguous window object resulting from Shell.Application is preventing success.

下面是我的几乎的木材加工的解决方案。这是一个批次的Jscript +混合动力。以.bat扩展名保存它,有了它你会做什么。 *耸肩*也许 form.submit()会为你正在使用的网站工作的?

Here's my almost-working solution. It's a batch + Jscript hybrid. Save it with a .bat extension, and do with it what you will. *shrug* Maybe form.submit() will work for the sites you are using?

@if (@CodeSection == @Batch) @then
@echo off & setlocal

cscript /nologo /e:JScript "%~f0"
goto :EOf

@end // end Batch / begin JScript hybrid code

var navOpenInBackgroundTab = 4096,
    site = [
        "http://www.site1.com/",
        "http://www.site2.com/"
    ],
    IE = WSH.CreateObject("InternetExplorer.Application"),
    oSH = WSH.CreateObject("Shell.Application"),
    username = 'username',
    password = 'password';

IE.Visible = true;

for (var i=0; i<site.length; i++) {
    IE.Navigate(site[i], i ? 4096 : null);
}

var tabs = oSH.Windows();

for (var x = 0; x < tabs.Count; x++) {
    if (!!(tabs.item(x))) {
        WSH.Echo('tab ' + x);
        var entered = { 'username': 0, 'password': 0 };
        while (tabs.item(x).Busy || tabs.item(x).ReadyState != 4) WSH.Sleep(50);
        // sleep 1 second regardless, just in case there's some jquery crap going on
        WSH.Sleep(1000);
        for (var i in tabs.item(x).document.forms) {
            if (i == 'length') break;
            var inputs = tabs.item(x).document.forms[i].getElementsByTagName('input');
            for (var j = inputs.length; j--;) {
                if (/\b(login|user(name|id)?|e\-?mail)\b/i.test(inputs[j].name)
                && inputs[j].type.toLowerCase() == "text") {
                    entered.username = inputs[j];
                    inputs[j].value = username;
                } else if (inputs[j].type.toLowerCase() == 'password') {
                    entered.password = inputs[j];
                    inputs[j].value = password;
                }
            }
            if (entered.username && entered.password) {
                tabs.item(x).document.forms[i].submit();

                /* === NOT QUITE WORKING ===
                try {
                    var evt = tabs.item(x).document.createEvent('KeyboardEvent');
                    // What is "window"? tabs.item(x) doesn't work, nor does IE.
                    evt.initKeyboardEvent('keypress', true, true, tabs.item(x),
                        false, false, false, false, 13, 13);
                    entered.password.dispatchEvent(evt);
                }
                catch(e) { WSH.Echo(e.message) }
                */

                i = tabs.item(x).document.forms.length;
            }
        }
    }
}

var IE = null;

另一个想法是做 passwordElement.focus(),并使用 WshShell.SendKeys()发送输入,但你将无法加载标签的背景。我还没有找到一个方法来激活标签编程之中;但如果他们在前台正装,他们已经活跃。尽管这不会在后台加载的标签,它比第一种方法更好。

Another idea would be to do passwordElement.focus() and use WshShell.SendKeys() to send Enter, but then you wouldn't be able to load the tabs in the background. I haven't found a way to activate tabs programmatically yet; but if they're loaded in the foreground, they're already active. Although this doesn't load tabs in the background, it works better than the first method.

@if (@CodeSection == @Batch) @then
@echo off & setlocal

cscript /nologo /e:JScript "%~f0"
goto :EOf

@end // end Batch / begin JScript hybrid code

var sites = {
        "http://www.site1.com/" : {
            'username': 'site1user',
            'password': 'site1pass'
        },
        "http://www.site2.net/" : {
            'username': 'site2user',
            'password': 'site2pass'
        }
    },
    IE = WSH.CreateObject("InternetExplorer.Application"),
    oSH = WSH.CreateObject("Shell.Application"),
    WshShell = WSH.CreateObject("WScript.Shell"),
    proc = GetObject("winmgmts:").ExecQuery("SELECT Handle FROM Win32_Process "
        + "WHERE Name='iexplore.exe'"),
    handle = new Enumerator(proc).item().Handle;

awesomeness:
for (var url in sites) {

    // if not a new window, open a new tab
    IE.Navigate(url, IE.Visible ? 2048 : null);
    IE.Visible = true;

    // give the tab a chance to load
    WSH.Sleep(1000);

    var tabs = oSH.Windows(),
        tab = tabs.item(tabs.Count - 1),
        entered = { 'username': 0, 'password': 0 };

    while (tab.Busy || tab.ReadyState != 4) WSH.Sleep(50);

    for (var i in tab.document.forms) {
        if (i == 'length') break;
        var inputs = tab.document.forms[i].getElementsByTagName('input');
        for (var j = inputs.length; j--;) {
            if (/\b(login|user(name|id)?|e\-?mail)\b/i.test(inputs[j].name)
            && inputs[j].type.toLowerCase() == "text") {
                entered.username = inputs[j].value = sites[url].username;
            } else if (inputs[j].type.toLowerCase() == 'password') {
                entered.password = inputs[j];
                inputs[j].value = sites[url].password;
            }
            if (entered.username && entered.password) {
                // force IE window to have focus
                while (!(WshShell.AppActivate(handle))) WSH.Sleep(50);
                entered.password.focus();
                WshShell.SendKeys('{END}{ENTER}');
                continue awesomeness;
            }
        }
    }
}

var IE = null;

这篇关于如何自动登录使用VBS脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆