Create a function to Connect to Office 365 and add to your powershell profile
- By : Dom
- Category : Office 365
- Tags: Office 365, Powershell
Each time you want to run powershell commands to manage office 365 you need to load the relevant modules for each corresponding session (Office 365 admin center, SharePoint Online, Exchange Online, Skype for Business Online, and the Security & Compliance Center).
Instead of running the relevant cmdlets each time create a function to do the job and add it your powershell profile.
Before we begin, there are a few requirements
- Office 365 account with global admin role (This a requirement for Office 365 PowerShell, not necessarily for all other Office 365 services)
- You need to install the Microsoft .NET Framework 4.5.x and then either the Windows Management Framework 3.0 or the Windows Management Framework 4.0.You need to use a 64-bit version of Windows because of the requirements for the Skype for Business Online module and one of the Office 365 modules
-
You need to install the modules that are required for Office 365, SharePoint Online, and Skype for Business Online:
Powershell Profile
To check if you have a profile or modify your powershell profile see my post
The function(s)
Personally, in my profile I have several functions to load the various Office 365 modules. I do not necessarily want to load them all. Add this code to your profile.
These functions load the modules and also prompt you what to type to load the modules.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
Write-Host “To connect to Office 365 with Remote Powershell type 'Connect-O365'” -fore cyan Function Connect-O365 { $Creds = Get-credential <username or leave blank to fill out each time> $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Creds -Authentication Basic -AllowRedirection Import-PSSession $Session Import-Module Msonline Connect-MSOLService -credential $creds } Write-Host “To connect to Lync Online with Remote Powershell type 'Connect-LyncO'” -fore cyan Function Connect-LyncO { $credential = Get-Credential <username or leave blank to fill out each time> $session = New-CsOnlineSession -Credential $credential Import-PSSession $session #$Creds = Get-credential #$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Creds -Authentication Basic -AllowRedirection #Import-PSSession $Session #Import-Module Msonline #Connect-MSOLService -credential $creds } Write-Host “To connect to Sharepoint Online with Remote Powershell type 'Connect-SP'” -fore cyan Function Connect-SP { $Creds = Get-credential <username or leave blank to fill out each time> #$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Creds -Authentication Basic -AllowRedirection #Import-PSSession $Session Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking Connect-SPOService -Url https://momondogroup-admin.sharepoint.com -credential $creds } Write-Host “To connect to Office 365 and Lync Online with Remote Powershell type 'Connect-OL'” -fore Red Function Connect-OL { $Creds = Get-credential <username or leave blank to fill out each time> $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Creds -Authentication Basic -AllowRedirection $session = New-CsOnlineSession -Credential $Creds Import-PSSession $Session Import-Module Msonline Connect-MSOLService -credential $creds #$Creds = Get-credential #$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Creds -Authentication Basic -AllowRedirection #Import-PSSession $Session #Import-Module Msonline #Connect-MSOLService -credential $creds } |
save and start powershell and you should see the prompts that will load the modules:
If you type Connect-O365, you’ll get the prompt to enter your credentials.
You could also include your credentials in the functions:
1 2 3 |
$Username = "Office 365 Username" $Password = ConvertTo-SecureString ‘YourPassword’ -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential $Username, $Password |
I have seen another way of implementing this which looks interesting but have not tried it.
In your profile add the following code:
1 2 |
$loadscripts=”C:\AutoloadScripts” #place the downloaded script from above in this location (Get-ChildItem “$loadscripts”).FullName | ForEach-Object {Write-Host “Loading Script “$_”” -foregroundcolor green;.$_} |
This will automatically load scripts that are placed in”C:\AutoLoadScripts” (could be interesting for other things).
Place this script for Office 365 in C:\AutoloadScripts or wherever you specfied in the profile. This script can be downloaded from Microsoft Technet Script Gallery or see https://syscloudpro.com/2016/12/18/all-in-one-office-365-powershell-connect/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
<# =========================================================================== Created on: 12/15/2016 1:32 PM Created by: Vikas Sukhija Organization: Filename: o365.ps1 ------------------------------------------------------------------------- O365 shells ALL in One =========================================================================== #> #############################Exchange Online################## Function LaunchEOL { $UserCredential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection Import-PSSession $Session -Prefix "EOL" -AllowClobber } Function RemoveEOL { $Session = Get-PSSession | where {$_.ComputerName -like “outlook.office365.com”} Remove-PSSession $Session } ########################Skype Online############################# function LaunchSOL { param ( $Domain, $UserCredential ) Write-Host "Enter Skype Online Credentials" -ForegroundColor Green $CSSession = New-CsOnlineSession -Credential $UserCredential -OverrideAdminDomain $Domain -Verbose Import-pssession $CSSession -Prefix "SOL" -AllowClobber } Function RemoveSOL { $Session = Get-PSSession | where { $_.ComputerName -like "admin1a.online.lync.com" } Remove-PSSession $Session } #####################Sharepoint Online############################### function LaunchSHO { param ( $orgName ) Write-Host "Enter Sharepoint Online Credentials" -ForegroundColor Green $userCredential = Get-Credential Connect-SPOService -Url https://$orgName-admin.sharepoint.com -Credential $userCredential } Function RemoveSHO { disconnect-sposervice } #########################Secuirty and Compliance########################## Function LaunchCOL { $UserCredential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection Import-PSSession $Session -Prefix "COL" -AllowClobber } Function RemoveCOL { $Session = Get-PSSession | where {$_.ComputerName -like “*compliance.protection.outlook.com”} Remove-PSSession $Session } ###############################Msonline######################### function LaunchMSOL { import-module msonline Write-Host "Enter MS Online Credentials" -ForegroundColor Green Connect-MsolService } Function RemoveMSOL { Write-host "Close Powershell Window - No disconnect available" -ForegroundColor yellow } ################################################################## |
No Comments