简体   繁体   English

如何使用powshell调用Graph API,例如获取用户数据?

[英]How to use the powshell to call the Graph API, such as get the user's data?

I learn from the stack overflow, that show we can using the PowerShell to call some URL. 我从堆栈溢出中了解到,这表明我们可以使用PowerShell调用某些URL。 So I want to use the PowerShell to call the Graph API for testing. 因此,我想使用PowerShell来调用Graph API进行测试。 So anyone can provide a sample code to show how to use it. 因此,任何人都可以提供示例代码来演示如何使用它。 Thanks. 谢谢。

You have to set up your environment to use the MSGraph API first for your needs... 您必须设置您的环境才能首先使用MSGraph API来满足您的需求...

There are several steps to get this done as documented on the MS Docs and TechNet sites 如MS Docs和TechNet网站上所述,有几个步骤可以完成此操作

... then use the PSMSGraph module to interact with it. ...然后使用PSMSGraph模块与之交互。

There are many more docs like the above on the topic . 还有更多类似上述主题的文档 Once you get all setup, then you can move forward as noted. 完成所有设置后,即可按照说明进行操作。

Connect to the Graph API 连接到Graph API

Each time you want to connect to the Microsoft Graph API you will need to export the AccessToken. 每次您想连接到Microsoft Graph API时,都需要导出AccessToken。

$GraphAccessToken = Import-GraphOAuthAccessToken -Path 'c:\MyGraphApp\AccessToken.XML'
$GraphAccessToken | Update-GraphOAuthAccessToken -Force

When you disconnect you will want to export the AccessToken. 断开连接时,您将要导出AccessToken。 This MUST be done each time. 每次都必须这样做。

$GraphAccessToken | Export-GraphOAuthAccessToken -Path 'c:\MyGraphApp\AccessToken.XML'

Connect to the Graph API and Get All Users 连接到Graph API并获取所有用户

In this example we will use our token to connect to the Microsoft Graph API and get a list of our users. 在此示例中,我们将使用令牌连接到Microsoft Graph API,并获取用户列表。 First, import the token 首先,导入令牌

$GraphAccessToken = Import-GraphOAuthAccessToken -Path 'c:\MyGraphApp\AccessToken.XML'
$GraphAccessToken | Update-GraphOAuthAccessToken -Force

Next, we can get all of our users by running the following command: 接下来,我们可以通过运行以下命令来获取所有用户:

Get-AADUserAll -AccessToken $GraphAccessToken

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM