In my previous post, which you can read here I showed you how you can simplify your life as an O365 admin, with Chrome profiles. With this post, I want to show you, how you can tune your PowerShell in order to establish PS profiles too, which can be used to automate common tasks, like login and connect to cloud services like Exchange Online, Office 365 or Azure.
Therefore, if you haven’t created a PS profile already, just run the following command in the PowerShell or, what I personally prefer, in the PS ISE.
Test-Path $Profile
If you receive a “False”, you do not have a profile already, so let’s create one with the following command and then open it in the notepad:
New-Item –Path $Profile –Type File –Force
Notepad $Profile
So now that you own a PS or ISE profile, you can do some nice stuff with it. And as an O365 admin, you probably love to do admin tasks with your PS instead with the GUI. But isn’t it annoying to login in everytime you open up your PS or ISE or even worse, login with different credentials, when you need to administer a customers tenant? That’s why I implemented my own login process in my ISE in order to define quick actions, which do the authentication process automatically with a few clicks. Simply paste the following code to your profile and replace with your credentials.
$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add(
"Tenant1", {
$CloudUsername = 'admin@tenant1.onmicrosoft.com'
$CloudPassword = ConvertTo-SecureString 'Pa$w0rd' -AsPlainText -Force
Clear-Host
$CloudCreds = New-Object System.Management.Automation.PSCredential $CloudUsername, $CloudPassword
Connect-MsolService -Credential $CloudCreds
$SessionEX = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $CloudCreds -Authentication Basic -AllowRedirection
Import-PSSession $SessionEX
Add-AzureAccount -Credential $CloudCreds
Select-AzureSubscription -SubscriptionName "AzureSubscription"
Login-AzureRmAccount -Credential $CloudCreds
Import-Module "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.PowerShell.dll"
Connect-SPOService -Url https://tenant1-admin.sharepoint.com -credential $CloudCreds
Clear-Host
},
"Control+Alt+1"
)
Now if you re-open up your ISE (if you have followed the previous steps in your ISE) you should see the following Add-Ons options:
So now you are good to go and click on your Tenant and start using your PS profile. And as you might guess you can paste more of these Add-Ons into your profile in order to add more tenants. But what’s not that ideal is that the password is inserted in plain text. Therefore you can follow the following routine in order to use encrypted passwords for your PS profiles.
At first, run the following command in your PS in order to create an encrypted password and save it to your disk (you may adapt the path):
(Get-Credential).Password | ConvertFrom-SecureString | Out-File "C:\Users\adm.sbi\Documents\Tools\PowerShell\tenant1_pw.txt"
Now that you have that in place, you need to adapt your profile in order to read the file, with the encrypted password and create your credential object with that password:
$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add(
"Tenant1", {
$CloudUsername = 'admin@tenant1.onmicrosoft.com'
$File = "C:\Users\adm.sbi\Documents\Tools\PowerShell\tenant1_pw.txt"
$CloudCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $CloudUsername, (Get-Content $File | ConvertTo-SecureString)
Connect-MsolService -Credential $CloudCreds
$SessionEX = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $CloudCreds -Authentication Basic -AllowRedirection
Import-PSSession $SessionEX
Add-AzureAccount -Credential $CloudCreds
Select-AzureSubscription -SubscriptionName "AzureSubscription"
Login-AzureRmAccount -Credential $CloudCreds
Import-Module "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.PowerShell.dll"
Connect-SPOService -Url https://tenant1-admin.sharepoint.com -credential $CloudCreds
Clear-Host
},
"Control+Alt+1"
)
So now you have secured your credentials for your PS sessions. Stay tuned as I’m going to blog about how to secure the credentials even more…