Announcement

Collapse
No announcement yet.

Script to auto login to office 365 for School District

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to auto login to office 365 for School District

    The administration wants me to set it up so when a student logs in it logs into office 365 and maps a drive to their one drive for them. Right now they have to open it login SELECT keep me logged in and from there it works. So for example on my PC at some point I had clicked that keep me logged in check box so the script works on mine but on another machine where I have not clicked that box it does not. Below is what I have so far. It works for users who have manually clicked that keep me logged in box but not for new users or those who haven't. Can anyone tell me whre I'm going wrong?
    Code:
    #$user = [Environment]::UserName
    $user = "myusername"
    $email = $user + "@emaildomain"
    $sitename = $user + "_email domain"
    $siteurl = "https://oursite-my.sharepoint.com/Personal/$sitename/documents"
    $mapurl = "\\[email protected]\DavWWWRoot\Personal\$sitename/documents"
    
    
    # Create the IE com object and logout first
    $ie = new-object -com InternetExplorer.Application 
    $ie.visible = $true #Uncomment this for debugging 
    $ie.navigate("http://login.microsoftonline.com/logout.srf")
    do {sleep 1} until (-not ($ie.Busy)) 
    $ie.quit()
    
    
    
    
    
    $ie = new-object -com InternetExplorer.Application
    $ie.navigate("https://login.microsoftonline.com/login.srf") 
    $ie.visible = $true #Uncomment this for debugging 
    # Wait for the page to finish loading 
    do {sleep 1} until (-not ($ie.Busy)) 
    #while($ie.ReadyState -ne 4) {start-sleep -m 100} # another way to do thisaa
    # We have to click the remember me checkbox before logging in, we also have to have IE be automated for this to work
    try { 
    		$ie.document.GetElementById("_link").click()
    		do {sleep 1} until (-not ($ie.Busy)) 
    	} catch {$null} 
    try { 
    		$ie.document.GetElementById("cred_keep_me_signed_in_checkbox").click()
    		$ie.document.GetElementById("cred_userid_inputtext").value = $email
    		$ie.document.GetElementById("cred_sign_in_button").click()
    		#do {sleep 1} until (-not ($ie.Busy)) 
    	} catch {$null} 
    
    
    	sleep -seconds 15 # give plenty of time to redirect
    ie.navigate($siteurl) # open o365 to verify we grab the token
    do {sleep 1} until (-not ($ie.Busy)) 
    cmd.exe /c 'c:\user data\onedrivemap.cmd'
    sleep -seconds 5 #wait it out for a redirect
    #$ie.quit()
    Last edited by Dumber; 4th December 2014, 22:55.

  • #2
    Re: Script to auto login to office 365 for School District

    I actually looked into this for a client but there's lots of problems with this. One being that you're dependent on the site not changing.

    A better solution would be to download the OneDrive app which will sync to the computer.

    Another option, if you don't want to sync, would be to setup federation and single sign on with ADFS. That way you can just map the drive and not worry about authentication.
    Regards,
    Jeremy

    Network Consultant/Engineer
    Baltimore - Washington area and beyond
    www.gma-cpa.com

    Comment


    • #3
      Re: Script to auto login to office 365 for School District

      We have the SSO with an ADFS setup but to map the drive the user has to open IE and put in their username and hit ok or TAB and then it auto logs in but it still takes that step. Thats what I'm trying to automate

      Comment


      • #4
        Re: Script to auto login to office 365 for School District

        Hmm...

        You could try using cmdkey to add the credentials.
        On a computer that works, run cmdkey /list to see if it's stored. You can then programmatically add the target, username, and password for the user without having to launch the site. But I'm not sure that it will work.
        Regards,
        Jeremy

        Network Consultant/Engineer
        Baltimore - Washington area and beyond
        www.gma-cpa.com

        Comment

        Working...
        X