Share via


Script: - PowerShell Script to Connect to all Microsoft Online Services using one function!

Feedback that we hear every now and again,Is how much it can be a pain for IT Admins whom need to administer multiple Microsoft Online Services via PowerShell. Each service require you to use a different module or dynamic script endpoint in order for you to be able to administer that service, and for some administrators this can be a real inconvenience. This particular script (which you should ideally add to your $profile will allow you to connect to all services using 1 command and you don't even have to connect to all services each time!).

Download PS1 File {zipped}

The most efficient way to manage this is to paste the following code in to a notepad, and then save it as profile.ps1 in your windows powershell directory "C:Users%username%DocumentsWindowsPowerShell" this will ensure that each time you open a powershell console, the following code is loaded and you will be able to use the functions that have been made available.

Functions that can be called are as follows:

Connect-Service [-All Services -AzureAD -Exchange -Lync -SharePoint]

 Function Connect-Service{
param(
[Parameter(ParameterSetName='IndividualService')][switch]$AzureAD,
[Parameter(ParameterSetName='IndividualService')][switch]$Exchange,
[Parameter(ParameterSetName='IndividualService')][switch]$Lync,
[Parameter(ParameterSetName='IndividualService')][switch]$SharePoint,
[Parameter(ParameterSetName='AllServices')][switch]$AllServices
)

switch ($psCmdlet.ParameterSetName){
'IndividualService' {
if (!($AzureAD -or $Exchange -or $Lync -or $SharePoint)){
throw("You must choose at least one service to connect to. Run Get-Help Connect-Service to see available parameters")
}
}

'AllServices'{}

default {throw("You must choose at least one service to connect to. Run Get-Help Connect-Service to see available parameters")}
}

#Gather credentials
#==================
Write-Host "Gathering installed modules. Please wait...." -ForegroundColor Cyan
$Modules = Get-Module -ListAvailable -Refresh
Write-Host ""
Write-Host "Please Enter your Azure Active Directory credentials into the popup window" -ForegroundColor Yellow
$UserCredentials = Get-Credential -Message "Please enter your Azure AD credentials"

#Validate username is of UPN format
#==================================
if ($UserCredentials.UserName -notmatch '.*@.*'){
throw("Username must be entered in UserPrincipalName format (i.e. admin@contoso.onmicrosoft.com). Please re-run this function and try again")
}

#Connect to SharePoint
#=====================
if ($SharePoint -or $AllServices){
if (($Modules | Select-Object -ExpandProperty Name) -contains 'Microsoft.Online.SharePoint.PowerShell'){
$SPOTenant = Read-Host -Prompt "Please enter the SharePoint admin url (i.e. https://contoso-admin.sharepoint.com)"
Write-Host "Logging into SharePoint Online" -ForegroundColor Cyan
Import-Module Microsoft.Online.SharePoint.PowerShell
Connect-SPOService -Url $SPOTenant -Credential $UserCredentials
} else {
Write-Warning "Could not connect to SharePoint. Microsoft.Online.SharePoint.PowerShell module is not installed"
}
}

#Connect to Azure AD
#===================
if ($AzureAD -or $AllServices){
if (($Modules | Select-Object -ExpandProperty Name) -contains 'MSOnline'){
Write-Host "Logging into Azure AD" -ForegroundColor Cyan
Import-Module MSOnline
Connect-MSOLService -Credential $UserCredentials
} else {
Write-Warning "Could not connect to Azure AD. MSOnline module is not installed"
}
}

#Connect to Exchange
#===================
if ($Exchange -or $AllServices){
Write-Host "Logging into Exchange Online" -ForegroundColor Cyan
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredentials -Authentication Basic -AllowRedirection
if ($Session -ne $null){
Import-PSSession $Session
} else {
Write-Warning "Unable to connect to Exchange Online. Please check credentials, and that you have admin privileges to Exchange service"
}
}

#Connect to Lync
#===============
if ($Lync -or $AllServices){
if (($Modules | Select-Object -ExpandProperty Name) -contains 'LyncOnlineConnector'){
Write-Host "Logging into Lync Online" -ForegroundColor Cyan
Import-Module LyncOnlineConnector
$Session = New-CsOnlineSession -Credential $UserCredentials
if ($Session -ne $null){
Import-PSSession $Session
} else {
Write-Warning "Unable to connect to Lync Online. Please check credentials, and that you have admin privileges to Exchange service"
}
} else {
Write-Warning "Could not connect to Lync. LyncOnlineConnector module is not installed"
}
}

}

I hope that helps,

James / Ashley
@Microsoft Cloud Identity Escalations

Comments

  • Anonymous
    March 04, 2015
    Hi Guys,Just wanted to say thanks for this!!! a BIG helpRikin