Instantly share code, notes, and snippets.
Last active
September 11, 2017 04:52
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save miracles1315/129f522e5f4fee0a71fdcd9223145f58 to your computer and use it in GitHub Desktop.
The function creates a remote PowerShell session to the Office 365 'SharePoint Online' service.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Connect-O365SharePointOnline | |
{ | |
<# | |
.SYNOPSIS | |
The function creates a remote PowerShell session to the Office 365 'SharePoint Online' service. | |
.DESCRIPTION | |
The function creates a remote PowerShell session to the Office 365 'SharePoint Online' service, which is where getting information from and making changes for both 'SharePoint Online' & 'OneDrive For Business' are done. Valid Office 365 credentials must be supplied. The function has an alias of Connect-O365SPO. | |
.PARAMETER OrganizationName | |
The name of the Office 365 organization (Ex: Contoso). This is a required parameter. | |
.PARAMETER Credential | |
Valid Office 365 credentials. | |
.EXAMPLE | |
PS C:\>$Credential = Get-Credential | |
PS C:\>Connect-O365SharePointOnline -OrganizationName Contoso -Credential $Credential | |
OR | |
PS C:\>$Credential = Get-Credential | |
PS C:\>Connect-O365SPO -OrganizationName Contoso -Credential $Credential | |
.EXAMPLE | |
PS C:\>Connect-O365SharePointOnline -OrganizationName Contoso -Credential administrator@contoso.com | |
OR | |
PS C:\>Connect-O365SPO -OrganizationName Contoso -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-O365SharePointOnline -OrganizationName Contoso | |
OR | |
PS C:\>Connect-O365SPO -OrganizationName Contoso | |
In the above examples, the user will be prompted for credentials. Valid Office 365 credentials will need to be supplied. | |
.EXAMPLE | |
PS C:\>Connect-O365SharePointOnline Contoso administrator@contoso.com | |
OR | |
PS C:\>Connect-O365SPO Contoso 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. | |
.INPUTS | |
Valid Office 365 credentials. | |
.OUTPUTS | |
None | |
.NOTES | |
Last Updated: 9/10/17 | |
.LINK | |
Connect-SPOService | |
https://technet.microsoft.com/en-us/library/fp161372.aspx | |
https://technet.microsoft.com/en-us/library/fp161364.aspx | |
https://www.microsoft.com/en-us/download/details.aspx?id=35588 | |
#> | |
[CmdletBinding()] | |
Param | |
( | |
[Parameter(Mandatory = $True, | |
HelpMessage = "Specify the name of your Office 365 organization (Ex: Contoso).")] | |
[String] $OrganizationName, | |
[PSCredential] $Credential = $Null | |
) | |
[String] $ModuleName = 'Microsoft.Online.SharePoint.PowerShell' | |
[String] $ServiceName = 'SharePoint Online' | |
[String] $FunctionName = 'Connect-O365SharePointOnline' | |
[String] $ModuleDownloadURL = 'https://www.microsoft.com/en-us/download/details.aspx?id=35588' | |
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-SPOService -Url https://$OrganizationName-admin.sharepoint.com -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-O365SkypeForBusiness' function | |
$FunctionAliasExists = Get-Alias -Name Connect-O365SPO -ErrorAction SilentlyContinue | |
If(!$FunctionAliasExists) | |
{ | |
New-Alias -Name Connect-O365SPO -Value Connect-O365SharePointOnline | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment