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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

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
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

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
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
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
Cloud Services

From novice to tech pro — start learning today.