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:  
Web Hosting