We help IT Professionals succeed at work.

PowerShell script that will launch & sign onto Office 365 admin console

In my test environment at home I need to create a Server 2016 PowerShell script that will launch the Office 365 PowerShell command line commands and automatically sign on with an Office 365 administrator username and password.

This isn't a security hazard since this will only be used on an Office 365 test domain on a computer at home that no one else has access to.

Please provide me with the correct syntax for doing this.
Comment
Watch Question

Jose Gabriel Ortega CastroCEO Faru Bonon IT /Top Rated Freelancer on Upwork / Photographer
Awarded 2018
Distinguished Expert 2018
Commented:
This is for connecting to exchangeOnline
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

Import-PSSession $Session

<#
Powershell commands to run on server goes here
#>
Remove-PSSession $Session

Open in new window


Source: https://technet.microsoft.com/en-us/library/jj984289%28v=exchg.160%29.aspx?f=255&MSPPError=-2147217396

The same logic is done for any connection to any services:
here's the another example to connecto to Msol service.

https://technet.microsoft.com/en-us/library/dn975125.aspx
IT GuyNetwork Engineer

Author

Commented:
I need to be able to enter the Office 365 administrator username & password within this script so that when the script is run it will automatically logon with the Office 365 administrator username and password.

Please provide me with the correct syntax of the PowerShell command for doing this.
Senior Network Consultant / Engineer
Commented:
In my post here I explain how to securely store your password for use in a script to do exactly what you're wanting to do:
https://www.experts-exchange.com/questions/29061982/Powershell-Using-credentials-stored-in-Credential-Manager.html#a42327537
Jeremy WeisingerSenior Network Consultant / Engineer

Commented:
Glad to help. :)