Pass-through User and Password Credential for O365 Cloud session

I have a script that creates session to Office 365. However I need a way to include the credentials without always having to type it in. Can someone assist?

thanks!
Anthony K O365Asked:
Who is Participating?
 
John SalleSenior IT Systems EngineerCommented:
If you can have the script use a standardized account you could hard code them into the script. The problem with doing it this way is that you have an admin username/password hard coded into a script. You'd have to consider other ways to secure it.

Something like this:
Import-Module MSOnline

$un = "<O365 Username@domain.com>"
$pw = ConvertTo-SecureString "<O365 Admin PW>" -AsPlainText -Force

$O365Cred = new-object management.automation.pscredential ($un,$pw)


$O365Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Cred -Authentication Basic -AllowRedirection

Import-PSSession $O365Session

Connect-MsolService –Credential $O365Cred

Open in new window

I always use this TechNet article to get the connection commands: https://technet.microsoft.com/en-us/magazine/hh750396.aspx
0
 
John SalleSenior IT Systems EngineerCommented:
I'm assuming you're talking about connecting to the PowerShell back end of Office 365. Are you set up with standalone "In-Cloud" accounts, or using ADFS/DirSync?
0
 
Anthony K O365Author Commented:
Using Dirsync
0
 
Vasil Michev (MVP)Commented:
Using the above method has the downside of having the credentials viewable in plain text, so you might consider storing them securely instead:

$x = Get-Credential
$x.Password | ConvertFrom-SecureString | Set-Content C:\password.txt

Open in new window


Then simply encrypt the password.txt file. To reuse the credentials in the script, include this:

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

Import-Module MSOnline
Connect-MsolService -Credential $credential

Open in new window


And so on for the rest of the connections.
0
 
Anthony K O365Author Commented:
this worked ideally.

thanks!
0
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.

All Courses

From novice to tech pro — start learning today.