当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何安装 Microsoft Graph PowerShell 模块

作者:精品下载站 日期:2024-12-14 06:28:02 浏览:12 分类:玩电脑

如何安装 Microsoft Graph PowerShell 模块


Microsoft Graph 模块是一个 PowerShell 模块,提供用于通过 PowerShell 管理 Microsoft 365 和 Azure Active Directory 资源的 cmdlet。

Microsoft Graph API 提供对 Microsoft 365 数据和服务(例如用户、组、日历和文件)的访问。

在本文中,我们将讨论如何安装 Microsoft Graph PowerShell 模块。

先决条件

Microsoft Graph PowerShell SDK 需要满足以下先决条件:

  • 升级到 PowerShell 5.1 或更高版本
  • 安装 .NET Framework 4.7.2 或更高版本
  • 使用 Install-Module PowerShell 将 PowerShellGet 更新到最新版本
  • PowerShell 脚本执行策略必须设置为远程签名或限制较少。要设置执行策略,请运行以下命令
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

在 PowerShell 中安装 Microsoft Graph 模块

要在 PowerShell Core 或 Windows PowerShell 中安装 Microsoft Graph v1.0 模块,请运行以下步骤。

1打开 PowerShell 终端

使用管理员权限启动 PowerShell 或 PowerShell Core。为此,请右键单击 PowerShell 图标并选择“以管理员身份运行”。

2安装 Microsoft Graph PowerShell SDK

在 PowerShell 中运行以下命令来安装 Graph 模块。

Install-Module Microsoft.Graph -Scope CurrentUser

此命令从 PowerShell 库获取模块并将其安装在您的系统上。

3. 接受不受信任的存储库

键入 Y 从不受信任的存储库安装模块并等待安装完成。

[玩转系统] 如何安装 Microsoft Graph PowerShell 模块

如何验证 PowerShell 中已安装的 Microsoft Graph 模块

要验证 PowerShell 中的 Microsoft Graph 模块是否已成功安装,请运行以下命令。

Get-InstalledModule Microsoft.Graph 

此命令返回 Microsoft Graph 模块的已安装版本、名称和说明(如果您的系统上安装了该模块)。

[玩转系统] 如何安装 Microsoft Graph PowerShell 模块

如何在 PowerShell 中导入 Microsoft Graph 模块

导入 Microsoft Graph 模块。运行以下命令。

Import-Module Microsoft.Graph

此命令使 Microsoft Graph 模块 cmdlet 可供在 PowerShell 会话中使用。

如何在 PowerShell 中连接到 MgGraph

要使用 PowerShell 中的范围连接到 MgGraph,请运行以下命令。

Connect-MgGraph -Scopes "User.Read.All","Group.ReadWrite.All" 

此命令将提示登录对话框以验证用户身份。

如果启用了 MFA,将为您的帐户生成验证码。您需要使用您使用工作或学校帐户配置的正版应用程序来验证它。

[玩转系统] 如何安装 Microsoft Graph PowerShell 模块

如果您的帐户没有启用 MFA,您可以使用用户名和密码对用户进行身份验证。

[玩转系统] 如何安装 Microsoft Graph PowerShell 模块

单击“下一步”按钮或登录以继续操作。它将代表您的组织请求您授予连接到 Microsoft Graph PowerShell 的权限和同意。

启用“代表您的组织同意”复选框,然后单击接受按钮。

[玩转系统] 如何安装 Microsoft Graph PowerShell 模块

验证成功后,将返回PowerShell终端并显示如下欢迎消息。

PS C:\> Connect-MgGraph -Scopes "User.Read.All","Group.ReadWrite.All"                                                   Welcome to Microsoft Graph!

Connected via delegated access using 14d82eec-204b-4c2f-b7e8-296a70dab67e
Readme: https://aka.ms/graph/sdk/powershell
SDK Docs: https://aka.ms/graph/sdk/powershell/docs
API Docs: https://aka.ms/graph/docs

NOTE: You can use the -NoWelcome parameter to suppress this message.

您现在可以使用 Microsoft Graph API 来管理 Microsoft 365 数据和服务,例如用户、组等......

您可以使用 Microsoft Graph 模块中的 Get-MgUser cmdlet 获取 Microsoft 365 中可用的用户。

 Get-MgUser -All  

此命令获取所有用户的列表。

如何更新 Microsoft Graph PowerShell 模块

要更新 PowerShell 中的 Microsoft Graph 模块以获得最新功能,请运行以下命令。

Update-Module Microsoft.Graph

此命令将更新 Microsoft Graph SDK 及其所有依赖项。

如何卸载 Microsoft Graph PowerShell 模块

要在 PowerShell 中卸载 Microsoft Graph 模块,请运行以下命令来卸载主模块。

Uninstall-Module Microsoft.Graph -AllVersions

该命令将删除主模块。

然后,通过运行以下命令删除所有依赖模块。

Get-InstalledModule Microsoft.Graph.* | ? Name -ne "Microsoft.Graph.Authentication" | Uninstall-Module
Uninstall-Module Microsoft.Graph.Authentication

结论

希望以上关于如何安装Microsoft Graph PowerShell模块的文章对您有所帮助。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯