Skip to content

Instantly share code, notes, and snippets.

@miracles1315
Created September 11, 2017 04:12
Show Gist options
  • Save miracles1315/d13343074b00cb87729be263abb1cf9a to your computer and use it in GitHub Desktop.
Save miracles1315/d13343074b00cb87729be263abb1cf9a to your computer and use it in GitHub Desktop.
The function creates a remote PowerShell session to the Office 365 'Azure Active Directory' (aka 'MS Online') service using the AzureAD module.
Function Connect-O365AzureActiveDirectory
{
<#
.SYNOPSIS
The function creates a remote PowerShell session to the Office 365 'Azure Active Directory' (aka 'MS Online') service using the AzureAD module.
.DESCRIPTION
The function creates a remote PowerShell session to the Office 365 'Azure Active Directory' (aka 'MS Online') service using the AzureAD module. Valid Office 365 credentials must be supplied. The function has an alias of Connect-O365AAD.
.PARAMETER Credential
Valid Office 365 credentials.
.EXAMPLE
PS C:\>$Credential = Get-Credential
PS C:\>Connect-O365AzureActiveDirectory -Credential $Credential
OR
PS C:\>$Credential = Get-Credential
PS C:\>Connect-O365AAD -Credential $Credential
.EXAMPLE
PS C:\>Connect-O365AzureActiveDirectory -Credential administrator@contoso.com
OR
PS C:\>Connect-O365AAD -Credential administrator@contoso.com
In the above examples, the user will be prompted for credentials, with administrator@contoso.com shown in the 'User name' field. A valid password will need to be supplied.
.EXAMPLE
PS C:\>Connect-O365AzureActiveDirectory
OR
PS C:\>Connect-O365AAD
In the above examples, the user will be prompted for credentials. Valid Office 365 credentials will need to be supplied.
.INPUTS
Valid Office 365 credentials.
.OUTPUTS
None
.NOTES
Last Updated: 9/10/17
.LINK
Connect-AzureAD
https://docs.microsoft.com/en-us/powershell/azure/active-directory/install-adv2?view=azureadps-2.0
https://www.powershellgallery.com/packages/AzureAD/2.0.0.131
#>
[CmdletBinding()]
Param
(
[PSCredential] $Credential = $Null
)
[String] $ModuleName = 'AzureAD'
[String] $ServiceName = 'Azure Active Directory'
[String] $FunctionName = 'Connect-O365AzureActiveDirectory'
[String] $ModuleDownloadURL = 'https://www.powershellgallery.com/packages/AzureAD/2.0.0.131'
Try
{
Write-Verbose "Importing the $ModuleName module."
Import-Module $ModuleName -ErrorAction Stop
[Boolean] $ModuleImported = $True
} #End 'Try' block
Catch
{
[Boolean] $ModuleImported = $False
Write-Warning "Unable to create a remote session to the Office 365 `'$ServiceName`' service because the `'$ModuleName`' module failed to successfully import (See the error, below, for details on the import failure.). Make sure the latest version of the module is installed (Last Known Download URL: $ModuleDownloadURL). If it is, try closing and relaunching Windows PowerShell/PowerShell ISE, and then running the $FunctionName function again.`n"
throw
} #End 'Catch' block
If($ModuleImported)
{
If(!$Credential)
{
Try
{
Write-Verbose "Prompting user for Office 365 credentials."
$Credential = Get-Credential -ErrorAction Stop
} #End 'Try' block
Catch
{
Write-Warning "User cancelled the request for credentials. Exiting the $FunctionName function."
} #End Catch block
} #End 'If' block
If($Credential)
{
Try
{
Connect-AzureAD -Credential $Credential -ErrorAction Stop
} #End 'Try' block
Catch
{
Write-Warning "Unable to create a remote session to the Office 365 `'$ServiceName`' service. See the error, below, for details on the failure.`n"
throw
} #End 'Catch' block
} #End 'If' block
} #End 'If' block
} #End 'Connect-O365AzureActiveDirectory' function
$FunctionAliasExists = Get-Alias -Name Connect-O365AAD -ErrorAction SilentlyContinue
If(!$FunctionAliasExists)
{
New-Alias -Name Connect-O365AAD -Value Connect-O365AzureActiveDirectory
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment