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.
LVL 1
the_omnificAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vasil Michev (MVP)Commented:
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
0
the_omnificAuthor Commented:
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

0
Vasil Michev (MVP)Commented:
OK, do this:

1) Enter your credentials

$Credential = Get-Credential

Open in new window


2) Store the password in a file

$credential.Password | ConvertFrom-SecureString | Set-Content C:\password.txt

Open in new window


3) Whenever you want to use this in a script, include this

$user = “user@domain.com”
$password = Get-Content C:\password.txt | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PsCredential($user, $password)

Open in new window


Now $cred will contain the username and password extracted from the file, and you dont need to enter anything. For example, to connect to WAAD, you will simply use:

Connect-MSOLService -Credential $cred

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hey MSSPs! What's your total cost of ownership?

WEBINAR: Managed security service providers often deploy & manage products from a variety of solution vendors. But is this really the best approach when it comes to saving time AND money? Join us on Aug. 15th to learn how you can improve your total cost of ownership today!

the_omnificAuthor Commented:
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?
0
Vasil Michev (MVP)Commented:
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.
0
the_omnificAuthor Commented:
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.
0
Vasil Michev (MVP)Commented:
You arent using the step to READ the stored credentials, the path you have given is wrong or the variable names don't match.
0
the_omnificAuthor Commented:
My bad! All good!
0
the_omnificAuthor Commented:
Awesome!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Office 365

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.