Auto Logon to Office 365 Sharepoint using VBS

On September 9, 2014, in Technology, by Mike Waldron

A client of mine has a SyncToy process which synchronizes Office 365 Sharepoint document library files to a local drive.  We map local drives to Sharepoint document libraries and then run SyncToy to copy the data locally.  A friend of mine helped me with this VBS script (thanks Nick), which automatically logs a user in and then maps the drives so we don’t have to deal with the 24 hour SAML token timeout, requiring a user to logon every 24 hours.  We use this script to automate our SyncToy procedure which runs throughout the day.

On Error Resume Next

Const PAGE_LOADED = 4

Set objIE = CreateObject("InternetExplorer.Application.1")
Call objIE.Navigate("https://yourdomain.sharepoint.com")

objIE.Visible = True

Do Until objIE.ReadyState = PAGE_LOADED : Call WScript.Sleep(500) : Loop

objIE.Document.all.Login.Value = "username@yourdomain.com"
objIE.Document.all.Passwd.Value = "office365password"
objIE.Document.all.persist.checked=True

'The "Submit" button needs to be "clicked" twice in order to sign the user in
' * It appears that some javascript needs to take place behind the scenes to properly initiate the logon
Call objIE.Document.getElementById("cred_sign_in_button").click
Call objIE.Document.getElementById("cred_sign_in_button").click

WScript.Sleep(4000)

Do Until objIE.ReadyState = PAGE_LOADED : Call WScript.Sleep(500) : Loop

Set wShell=CreateObject("WScript.Shell")

NetCommand="net.exe use p: ""https://yourcompany.sharepoint.com/Library 1"" /user:username@yourdomain.com office365password"
wShell.Run "cmd.exe /C " & NetCommand, 1, True

NetCommand="net.exe use w: ""https://yourcompany.sharepoint.com/Library 2"" /user:username@yourdomain.com office365password"
wShell.Run "cmd.exe /C " & NetCommand, 1, True

Set wShell=Nothing

objIE.quit

Set objIE = Nothing


Tagged with:  

3 Responses to Auto Logon to Office 365 Sharepoint using VBS

  1. Charles Mc Carthy says:

    Hi is this script still working i just try to make it work for our schoolboard and i can’t make it works.

    IE open but i stay on the login page without any data in the email or password field

    • Hey Charles, we use this on a Windows 2008 (not R2) server. I’ve tried it with IE 11 and you’re right it doesn’t work. I’ll look into what might be causing the problem on newer versions of Windows and let you know.

  2. Charles Mc Carthy says:

    can i do the same to map drive one onedrive enterprise?

    thx

Leave a Reply

Your email address will not be published. Required fields are marked *

Web Hosting