Link to home
Start Free TrialLog in
Avatar of Carl Billington
Carl BillingtonFlag for Australia

asked on

Logon to Office 365 via command line without prompting for credentials

Is it possible to logon to Office 365 from command line without prompting for credentials? I would like credentials to be a part of the script.
 
Thank you.
Avatar of Vasil Michev (MVP)
Vasil Michev (MVP)
Flag of Bulgaria image

You can store the password securely in a file and load it when you are running the script, without prompts. Check for example here: https://powertoe.wordpress.com/2011/06/05/storing-passwords-to-disk-in-powershell-with-machine-based-encryption/

Or here: https://gallery.technet.microsoft.com/scriptcenter/Execute-PowerShell-Script-38881dce
Avatar of Carl Billington

ASKER

Thanks for your response. Am I executing this wrong?

######################################################################################## 
###Script        :    To Execute PowerShell script with other credentials without prompts 
###Developer    :    Chendrayan Venkatesan 
###Company        :     Tata Consultancy Service 
######################################################################################## 
 
#SharePoint Admin Account 
$SPAdmin = "name@myemailaddress.com.au" 
$Password = Get-Content C:\securestring.txt | convertto-securestring 
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $SPAdmin, $Password 
 
Get-WmiObject -Class Win32_Service -ComputerName "Server" -Filter "Name='ServiceName'" -Credential $Credential
 
$LiveCred = Get-Credential
Connect-MSOLService -Credential $cred

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Vasil Michev (MVP)
Vasil Michev (MVP)
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, thanks for your reply. Step 1 asked for your credentials. Is there any way we can extract this information from a text file so that you do not need to input them?
You do realize you have to STORE those credentials to the text file first? This is what steps 1 and 2 do.

You do not need to include those steps in the script file. But you need to run them at least once on each machine you will be running the script from.
Hi, on the final step, as soon as I run;
Connect-MSOLService -Credential $cred

Open in new window

I am prompted for a username and password?
 
I completed the other steps successfully.
You arent using the step to READ the stored credentials, the path you have given is wrong or the variable names don't match.
My bad! All good!
Awesome!