Avatar of Carl Billington
Carl Billington
Flag 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.
Microsoft 365Powershell

Avatar of undefined
Last Comment
Carl Billington

8/22/2022 - Mon
Vasil Michev (MVP)

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
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
Vasil Michev (MVP)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Carl Billington

ASKER
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?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Vasil Michev (MVP)

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.
Carl Billington

ASKER
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.
Vasil Michev (MVP)

You arent using the step to READ the stored credentials, the path you have given is wrong or the variable names don't match.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Carl Billington

ASKER
My bad! All good!
Carl Billington

ASKER
Awesome!