Skip to content

Instantly share code, notes, and snippets.

@miracles1315
Last active October 10, 2017 04:43
Show Gist options
  • Save miracles1315/0d82602c2cc6c50221e41472b55706b5 to your computer and use it in GitHub Desktop.
Save miracles1315/0d82602c2cc6c50221e41472b55706b5 to your computer and use it in GitHub Desktop.
The function creates a remote PowerShell session to the Office 365 'Security And Compliance' service.
Function Connect-O365SecurityAndCompliance
{
<#
.SYNOPSIS
The function creates a remote PowerShell session to the Office 365 'Security And Compliance' service.
.DESCRIPTION
The function creates a remote PowerShell session to the Office 365 'Security And Compliance' service. Valid Office 365 credentials must be supplied. The function has an alias of Connect-O365SAC.
.PARAMETER Credential
Valid Office 365 credentials.
.EXAMPLE
PS C:\>$Credential = Get-Credential
PS C:\>Connect-O365SecurityAndCompliance -Credential $Credential
OR
PS C:\>$Credential = Get-Credential
PS C:\>Connect-O365SAC -Credential $Credential
.EXAMPLE
PS C:\>Connect-O365SecurityAndCompliance -Credential administrator@contoso.com
OR
PS C:\>Connect-O365SAC -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-O365SecurityAndCompliance
OR
PS C:\>Connect-O365SAC
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
The exported commands from the 'Security And Compliance' service (Ex: Get-ComplianceSearch).
.NOTES
Last Updated: 9/11/17
.LINK
New-PSSession
https://technet.microsoft.com/en-us/library/mt587092(v=exchg.160).aspx
https://technet.microsoft.com/en-us/library/mt587093(v=exchg.160).aspx
#>
[CmdletBinding()]
Param
(
[PSCredential] $Credential = $Null
)
[String] $ServiceName = 'Security And Compliance'
[String] $FunctionName = 'Connect-O365SecurityAndCompliance'
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
{
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection -ErrorAction Stop
$Global:SACExportedCommands = Import-PSSession $Session -ErrorAction Stop
Return $SACExportedCommands
} #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 'Connect-O365SecurityAndCompliance' function
$FunctionAliasExists = Get-Alias -Name Connect-O365SAC -ErrorAction SilentlyContinue
If(!$FunctionAliasExists)
{
New-Alias -Name Connect-O365SAC -Value Connect-O365SecurityAndCompliance
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment