使用 PowerShell 连接 Microsoft 365
此文章适用于 Microsoft 365 企业版和 Office 365 企业版。
PowerShell for Microsoft 365 可让你从命令行管理您的 Microsoft 365 设置。 要连接到 PowerShell,只需安装所需的软件,然后连接到 Microsoft 365 组织。
可以使用两种版本的 PowerShell 模块连接至 Microsoft 365 和管理员用户帐户、组和许可证:
- Azure Active Directory PowerShell for Graph(其 cmdlet 在其名称中包含 AzureAD)
- 用于 Windows PowerShell 的 Microsoft Azure Active Directory 模块(其 cmdlet 在其名称中包含 Msol)
目前,对于用户、组和许可证管理,Azure Active Directory PowerShell for Graph 模块不能完全替代用于 Windows PowerShell 模块的 Microsoft Azure Active Directory 模块的功能。 在某些情况下,需要同时使用两种版本。 可以在同一计算机上安全地安装两种版本。
注意
还可以从 Microsoft 365 管理中心连接 Azure Cloud Shell。
开始前,需要知道什么?
注意
Azure Active Directory 模块将被替换为 Microsoft Graph PowerShell SDK。 可以使用 Microsoft Graph PowerShell SDK 访问所有 Microsoft Graph API。 有关详细信息,请参阅 Microsoft Graph PowerShell SDK 入门。
操作系统
必须使用 64 位版 Windows。 2014 年已终止对 32 位版的用于 Windows PowerShell 的 Microsoft Azure Active Directory 模块的支持。
可以使用下列 Windows 版本:
Windows 10、Windows 8.1、Windows 8 或 Windows 7 Service Pack 1 (SP1)
Windows Server 2019、Windows Server 2016、Windows Server 2012 R2、Windows Server 2012 或 Windows Server 2008 R2 SP1
注意
对于 Windows 8.1、Windows 8、Windows 7 Service Pack 1 (SP1)、Windows Server 2012 R2、Windows Server 2012 和 Windows Server 2008 R2 SP1,请下载并安装 Windows Management Framework 5.1。
PowerShell
对于 Azure Active Directory PowerShell for Graph 模块,必须使用 PowerShell 版本 5.1。
对于用于 Windows PowerShell 的 Microsoft Azure Active Directory 模块,必须使用 PowerShell 版本 5.1 或以上版本(最高版本 6)。 你无法使用 PowerShell 版本 7。
注意
这些步骤适合于属于 Microsoft 365 管理员角色成员的用户。 有关详细信息,请参阅关于管理员角色。
连接到 Azure Active Directory PowerShell Graph 模块
Azure Active Directory PowerShell Graph 模块中的命令在其 cmdlet 名称中包含 AzureAD。 可安装Azure Active Directory PowerShell for Graph 模块或 Azure PowerShell。
有关在 Azure Active Directory PowerShell Graph 模块中需要新 cmdlet 的过程,请按照以下步骤安装该模块并连接到 Microsoft 365 订阅。
注意
有关不同版本的 Microsoft Windows 的支持信息,请参阅 Azure Active Directory PowerShell Graph 模块。
步骤 1:安装所需软件
这些步骤只需在计算机上执行一次。 但可能需要定期更新软件。
打开Windows PowerShell命令提示符窗口。
运行此命令:
Install-Module -Name AzureAD
默认情况下,PowerShell 库 (PSGallery) 不会配置为 PowerShellGet 受信任的存储库。 首次使用 PSGallery 时,你将看到以下消息:
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the `Set-PSRepository` cmdlet.
Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"):
回答“是”或“全部是”以继续安装。
运行此命令以导入模块:
Import-Module AzureAD
步骤 2:连接到 Microsoft 365 订阅的 Azure AD
若要使用帐户名称和密码或者多重身份验证连接到 Microsoft 365 订阅的 Azure Active Directory (Azure AD),请在 Windows PowerShell 命令提示符中运行这些命令之一。 (无需提升。)
Office 365 云 | 命令 |
---|---|
Office 365 全球 (+GCC) | Connect-AzureAD |
由世纪互联运营的 Office 365 | Connect-AzureAD -AzureEnvironmentName AzureChinaCloud |
Office 365 德国 | Connect-AzureAD -AzureEnvironmentName AzureGermanyCloud |
Office 365 美国政府版 DoD 和 Office 365 美国政府版 GCC 高 | Connect-AzureAD -AzureEnvironmentName AzureUSGovernment |
在“登录到你的帐户”对话框中,键入 Microsoft 365 工作或学校帐户用户名和密码,再选择“确定”。
如果使用多重身份验证,请按照说明提供其他身份验证信息,例如验证码。
连接后,可对 Azure Active Directory PowerShell Graph 模块使用这些 cmdlet。
与用于 Windows PowerShell 的 Microsoft Azure Active Directory 模块连接
注意
用于 Windows PowerShell 的 Microsoft Azure Active Directory 模块中的 cmdlet 名称中具有 Msol。
PowerShell 版本 7 不支持用于 Windows PowerShell 和 cmdlet 的其名称中包含 Msol 的 Microsoft Azure Active Directory 模块。 对于 PowerShell 版本 7 及更高版本,必须使用 Microsoft Graph PowerShell SDK。
PowerShell Core 不支持用于 Windows PowerShell 和 cmdlet 的其名称中包含 Msol 的 Microsoft Azure Active Directory 模块。 从 Windows PowerShell 运行这些 cmdlet。
步骤 1:安装所需软件
这些步骤只需在计算机上执行一次。 但可能需要定期更新软件。
如果您运行的不是 Windows 10,则请安装 32 位版 Microsoft Online Services 登录助手:适用于 IT 专业人员 RTW 的 Microsoft Online Services 登录助手。
请按照以下步骤,安装用于 Windows PowerShell 的 Microsoft Azure Active Directory 模块:
- 打开提升的 Windows PowerShell 命令提示符(以管理员身份运行 Windows PowerShell)。
- 运行 Install-Module MSOnline 命令。
- 如果系统提示安装 NuGet 提供程序,请键入 Y,然后按 Enter 键。
- 如果系统提示从 PSGallery 安装模块,请键入 Y,然后按 Enter 键。
步骤 2:连接到 Microsoft 365 订阅的 Azure AD
若要使用帐户名称和密码或者多重身份验证连接到 Microsoft 365 订阅的 Azure AD,请在 Windows PowerShell 命令提示符中运行这些命令之一。 (无需提升。)
Office 365 云 | 命令 |
---|---|
Office 365 全球 (+GCC) | Connect-MsolService |
由世纪互联运营的 Office 365 | Connect-MsolService -AzureEnvironment AzureChinaCloud |
Office 365 德国 | Connect-MsolService -AzureEnvironment AzureGermanyCloud |
Office 365 美国政府版 DoD 和 Office 365 美国政府版 GCC 高 | Connect-MsolService -AzureEnvironment USGovernment |
在“登录到你的帐户”对话框中,键入 Microsoft 365 工作或学校帐户用户名和密码,再选择“确定”。
如果使用多重身份验证,请按照说明提供其他身份验证信息,例如验证码。
如何知道它是正常工作的?
如果你没有收到错误消息,则表明你已成功连接。 有关快速测试,请运行 Microsoft 365 cmdlet(如 Get-MsolUser)并查看结果。
如果收到错误消息,请检查以下问题:
常见问题是密码错误。 再次运行步骤 2,并密切注意您输入的用户名和密码。
Microsoft Azure Active Directory模块Windows PowerShell要求 Microsoft .NET Framework 3.5。x 已在计算机上启用。 计算机可能安装了较新版本 (例如 4 或 4.5。x) 。 但可以启用或禁用与 .NET Framework 的早期版本的向后兼容性。 有关详细信息,请参阅以下文章:
对于 Windows Server 2012 或 Windows Server 2012 R2,请参阅使用“添加角色和功能”向导启用 .NET Framework 3.5。
对于 Windows 7 或 Windows Server 2008 R2,请参阅不能打开用于 Windows PowerShell 的 Azure Active Directory 模块。
对于 Windows 10、Windows 8.1 和 Windows 8,请参阅在 Windows 10、Windows 8.1 和 Windows 8 上安装 .NET Framework 3.5。
你的用于 Windows PowerShell 的 Microsoft Azure Active Directory 模块版本可能已过期。 若要进行检查,请在 PowerShell for Microsoft 365 或用于 Windows PowerShell 的 Microsoft Azure Active Directory 模块中运行以下命令:
(Get-Item C:\Windows\System32\WindowsPowerShell\v1.0\Modules\MSOnline\Microsoft.Online.Administration.Automation.PSModule.dll).VersionInfo.FileVersion
如果返回的版本号低于 1.0.8070.2,请卸载用于 Windows PowerShell 的 Microsoft Azure Active Directory 模块,并通过上述第 1 步进行安装。
如果收到连接错误,请参阅“Connect-MsolService:抛出类型异常”错误。
如果收到“Get-Item:找不到路径”错误消息,请运行以下命令:
(dir "C:\Program Files\WindowsPowerShell\Modules\MSOnline").Name
与 Azure Cloud Shell 连接
若要连接并使用 Microsoft 365 管理中心中的 Azure Cloud Shell,请从任务栏右上角选择 PowerShell 窗口图标。 在 欢迎使用 Azure Cloud Shell 窗格中,选择 PowerShell。
将需要组织有活跃的 Azure 订阅,并与 Microsoft 365 订阅绑定。 如果还没有,可以创建一个。 一旦拥有了 Azure 订阅,则会打开 PowerShell 窗口,可以从中运行 PowerShell 命令和脚本。
有关详细信息,请参阅 Azure Cloud Shell。