适用于以下产品或服务:
Office 365 管理员的日常管理中。使用 PowerShell 是除了Office365 管理中心外,最为高效的工具。这里将会介绍 PowerShell 连接到 Office365 服务的方法。
本篇摘要:
前提条件
连接到 Exchange Online
连接到 SharePoint Online
连接到 Office 365 管理服务
连接到 Skype for Business Online
前提条件
为了能确保电脑可以正常使用 PowerShell,请进行下面的前期准备:
支持 PowerShell 连接Windows系统有(以下均为64 位系统):
Windows 10、Windows 8.1、Windows 8 或 Windows 7 Service Pack 1 (SP1)
Windows Server 2019、Windows Server 2016、WindowsServer 2012 R2、Windows Server 2012 或 Windows Server 2008 R2 SP1
初次使用PowerShell连接Office 365的各项服务时,需要先运行以下命令:
Set-ExecutionPolicy RemoteSigned
连接到 Exchange online
1. Exchange Online 的 Powershell Module 并不需要单独下载,具体的连接步骤如下:
$UserCredential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://partner.outlook.cn/PowerShell-LiveID/ -Credential $UserCredential -Authentication Basic -AllowRedirection Import-PSSession $Session
2. 此时会弹出对话框提示您输入用户名和密码,这时您需要有效的、具备 Exchange Online 管理员权限的帐号,之后 PowerShell 会话会自动下载所需的 Exchange 模块。
连接到 SharePoint Online
要通过 PowShell 连接 SharePoint Online 服务,您首先需要安装 SharePoint Online Management Shell,安装方式有两种:
方法一:直接下载安装:
http://www.microsoft.com/en-us/download/details.aspx?id=35588
方法二:我们可以通过 Nuget 在 PowerShell 中直接使用 Install-Module 命令来安装,命令如下:
Install-Module Microsoft.Online.SharePoint.PowerShell
此时您可能会在 PowerShell 上收到如下提示:
或者:
无论哪种提示,都输入“Y”并按下回车即可
然后用以下命令连接到 SharePoint 管理中心:
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameCheckingConnect-SPOService -Url https://youro365domain-admin.sharepoint.cn -Credential 管理员账号
注意:youro365domain 为您 Office 365 租户的默认域名 如office365lab.partner.onmschina.cn,则输入 -Url https://office365lab-admin.sharepoint.cn;
管理员账号部分为拥有 SharePoint 管理员权限的帐号,在下一步弹窗中输入该帐号的密码。
在一些情况下,您可能还需要连接到 SharePoint PnP PowerShell,SharePoint PnP PowerShell 主要用户连接具体的SharePoint网站,而非整个 SharePoint 服务。安装 SharePointPnP PowerShell 模块的命令如下:
Install-Module SharePointPnPPowerShellOnline
同样的情况,如果PowerShell中出现任何提示,均输入“Y”,之后回车以继续。
之后您便可以通过以下命令连接到PowerShell PnP:
Import-Module SharePointPnPPowerShellOnlineConnect-PnPOnline –Url https://siteUrl –Credentials (Get-Credential)
其中 https://siteUrl 是您要连接的 SharePoint 站点的完整 URL,如以下示例:
https://office365lab.sharepoint.cn
https://office365lab-admin.sharepoint.cn
https://office365lab.sharepoint.cn/sites/demo
在弹出的窗口中输入该网站管理员的用户名和密码,即可连接。
有关 SharePoint Online PnP 的相关连接,您可参考:
https://docs.microsoft.com/zh-cn/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets?view=sharepoint-ps
连接到 Office 365 管理服务
在使用 Office 365 管理服务的PowerShell之前,也需要安装其模块,安装方法也是两种:
方法一:安装 Microsoft Online Services 登陆小助手
http://www.microsoft.com/en-us/download/details.aspx?id=41950
方法二:在PowerShell中运行此指令:Install-Module MSOnline
在运行时,如果Powershell 出现任何提示,均输入“Y”并按下回车以继续。
导入并连接Office 365服务
Import-Module msonlineConnect-MsolService -AzureEnvironment AzureChinaCloud
此时您会看到熟悉的 Office 365 现代模式凭据验证界面,直接输入您的管理员帐号密码即可。
连接到 Skype for Business Online
1. 下载安装以下 Skype for Business Online 最新模块
Skype for Business Online, Windows PowerShell Module:
https://www.microsoft.com/en-sg/download/details.aspx?id=39366&751be11f-ede8-5a0c-058c-2ee190a24fa6=True
2. 执行以下命令,连接到 Skype for Business Online,需要输入管理员账号和密码
$cred = Get-CredentialImport-Module LyncOnlineConnector$cssess = New-CsOnlineSession -Credential $cred -Verbose Import-PSSession $cssess
参考文档:
https://docs.microsoft.com/en-us/office365/enterprise/powershell/connect-to-office-365-powershell
欢迎大家关注我们的公众号,我们将持续进行技术文章的更新以及对 Office 365 技术的推广和普及。