将 Microsoft Graph API 与 Powershell 结合使用
Microsoft Graph API 使您能够通过单个端点与不断发展的 Azure 服务进行交互:https://graph.microsoft.com。我们将使用 Powershell、OAuth 2.0 和 REST 连接到 Graph。
注册您的应用程序
要与 Graph 交互,我们需要注册我们的应用程序。请访问 https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade 开始使用。使用与您的租户关联的帐户登录。添加新应用程序并输入以下详细信息:
名称 > 只需给它一个名称
应用程序类型 > Web 应用程序/-API
登录 Url > 没关系,所以我们选择:http://localhost:8000
然后选择您新创建的应用程序并转到所需权限,这里我们将添加Graph API权限。您需要根据您想要访问的内容选择不同的权限。您可以稍后更改此设置,因此现在我们单击顶部的“添加”,选择 Microsoft Graph,在步骤 2 中,我们只需选择 对用户配置文件的读写访问权限。
最后一步是创建密钥,转到 Keys 并输入 Key Description 并将持续时间设置为永不过期。当您点击“保存”时,该值(ClientSecret)将仅显示一次,因此在显示时写入完成。
成为一名有道德的黑客,可以像黑帽黑客一样入侵计算机系统,并像安全专家一样保护它们。
认证与授权
要访问 Graph API,我们需要获取访问代码。如果您不熟悉 Rest 和 Graph API,那么需要记住以下几点:
要获取访问令牌,您需要授权码。该代码的有效期只有 1 小时,但只要您的刷新令牌有效,您只需每 90 天更新一次。
每次请求中使用的访问令牌的有效期仅为 1 小时。您可以使用刷新令牌进行更新。
刷新令牌的有效期为 14 天。
所以第一步是获取身份验证码。为此,我们使用下面的脚本。使用以下命令运行脚本:
GraphAPIGetAuthCode.ps1 -ClientId <clientId> -ClientSecret <clientSecret> -RedirectUrl <redirectUrl>它将把身份验证代码存储在脚本根位置的文本文件中。这样我们以后就可以轻松访问它。
[CmdletBinding()]
PARAM(
[parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Mandatory=$true)]
[string]$ClientId,
[parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Mandatory=$true)]
[string]$ClientSecret,
[parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Mandatory=$true)]
[string]$RedirectUrl
)
BEGIN
{
$ResourceUrl = "https://graph.microsoft.com"
}
PROCESS
{
Function Get-AuthCode {
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object -TypeName System.Windows.Forms.Form -Property @{Width=440;Height=640}
$web = New-Object -TypeName System.Windows.Forms.WebBrowser -Property @{Width=420;Height=600;Url=($url -f ($Scope -join "%20")) }
$DocComp = {
$Global:uri = $web.Url.AbsoluteUri
if ($Global:uri -match "error=[^&]*|code=[^&]*") {$form.Close() }
}
$web.ScriptErrorsSuppressed = $true
$web.Add_DocumentCompleted($DocComp)
$form.Controls.Add($web)
$form.Add_Shown({$form.Activate()})
$form.ShowDialog() | Out-Null
$queryOutput = [System.Web.HttpUtility]::ParseQueryString($web.Url.Query)
$output = @{}
foreach($key in $queryOutput.Keys){
$output["$key"] = $queryOutput[$key]
}
$output
}
# UrlEncode the ClientID and ClientSecret and URL's for special characters
$clientIDEncoded = [System.Web.HttpUtility]::UrlEncode($ClientId)
$clientSecretEncoded = [System.Web.HttpUtility]::UrlEncode($ClientSecret)
$redirectUrlEncoded = [System.Web.HttpUtility]::UrlEncode($RedirectUrl)
$resourceUrlEncoded = [System.Web.HttpUtility]::UrlEncode($ResourceUrl)
$scopeEncoded = [System.Web.HttpUtility]::UrlEncode("https://outlook.office.com/user.readwrite.all")
# Get AuthCode
$url = "https://login.microsoftonline.com/common/oauth2/authorize?response_type=code&redirect_uri=$redirectUrlEncoded&client_id=$clientID&resource=$resourceUrlEncoded&prompt=admin_consent&scope=$scopeEncoded"
Get-AuthCode
# Extract Access token from the returned URI
$regex = '(?<=code=)(.*)(?=&)'
$authCode = ($uri | Select-string -pattern $regex).Matches[0].Value
# Store AuthCode
Set-Content "$PSScriptRoot\AuthCode.txt" $authCode
}感谢达伦·罗宾逊:https://gist.github.com/darrenjrobinson/b74211f98c507c4acb3cdd81ce205b4f#file-ps2graphapi-ps1
获取访问和刷新令牌
通过身份验证代码,我们可以获得所有重要的访问令牌和急需的刷新令牌。由于访问令牌的有效期只有 1 小时,因此我们需要检查它的使用期限,如果过期,则使用刷新令牌请求新的访问令牌。
下面的脚本将访问令牌和刷新令牌存储在脚本根位置的文本文件中。您始终可以将其更改为仅将其存储在变量中,但请记住,当您关闭 shell 时,所有内容都消失了,您需要使用授权代码重新开始。
脚本检查 accesstoken.txt 的最后修改时间,如果早于 1 小时,它将使用刷新令牌请求新的访问令牌。如果失败,它将请求新的授权码并重新开始。
我使用 config.json 文件来存储我的 ClientId、ClientSecret 和 RedirectUrl,这样您就可以轻松更改它。
配置.json
{
"AppId": {
"ClientId": "fa9d9d34-7a2a-****-****-************",
"ClientSecret": "J/WDSKLjE+XC9G0+ASDHUen31/***********=",
"RedirectUrl": "https://localhost:8000",
"ResourceUrl": "https://graph.microsoft.com"
}
}连接图
#----------------------------------------------------------[Declarations]----------------------------------------------------------
#Get the config file
$config = Get-Content $PSScriptRoot"\config.json" -Raw | ConvertFrom-Json
# Add System Web Assembly to encode ClientSecret
Add-Type -AssemblyName System.Web
# Encode ClientSecret
$clientSecretEncoded = [System.Web.HttpUtility]::UrlEncode($config.AppId.ClientSecret)
# Get the accessToken
If ((Test-Path -Path $PSScriptRoot"\accessToken.txt") -ne $false) {
$accessToken = Get-Content $PSScriptRoot"\accessToken.txt"
}
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
# Check if the AccessToken is not older then 1 hour
If (($accessToken -eq $null) -or ((get-date) - (get-item $PSScriptRoot"\accessToken.txt").LastWriteTime).TotalHours -gt 1) {
# Get the refreshToken
$refreshToken = Get-Content $PSScriptRoot"\refreshToken.txt"
$clientId = $config.AppId.ClientId
$clientSecret = $config.AppId.clientSecret
$redirectUrl = $config.AppId.RedirectUrl
$resourceUrl = $config.AppId.ResourceUrl
Try {
$refreshBody = "grant_type=refresh_token&redirect_uri=$redirectUrl&client_id=$clientId&client_secret=$clientSecretEncoded&refresh_token=$refreshToken&resource=$resourceUrl"
$Authorization = Invoke-RestMethod https://login.microsoftonline.com/common/oauth2/token `
-Method Post -ContentType "application/x-www-form-urlencoded" `
-Body $refreshBody `
-UseBasicParsing
}
Catch {
$webResponse = $_.Exception.Response
}
If ($webResponse -ne $null) {
# Get Authorization code
GraphAPIGetAuthCode.ps1 -ClientId $clientId -ClientSecret $clientSecret -RedirectUrl $redirectUrl
$authCode = get-content $PSScriptRoot"\authCode.txt"
$body = "grant_type=authorization_code&redirect_uri=$redirectUrl&client_id=$clientId&client_secret=$clientSecretEncoded&code=$authCode&resource=$resourceUrl"
$Authorization = Invoke-RestMethod https://login.microsoftonline.com/common/oauth2/token `
-Method Post -ContentType "application/x-www-form-urlencoded" `
-Body $body `
-UseBasicParsing
}
# Store refreshToken
Set-Content $PSScriptRoot"\refreshToken.txt" $Authorization.refresh_token
# Store accessToken
$accessToken = $Authorization.access_token
Set-Content $PSScriptRoot"\accessToken.txt" $accessToken
}
使用 Invoke-RestMethod 从 Graph 获取实际日期
完成所有操作后,我们可以与 Graph 进行交互,如下所示:
Invoke-RestMethod -Headers @{Authorization = "Bearer $accessToken"} `
-Uri https://graph.microsoft.com/v1.0/me `
-Method Get
您可以在此处下载脚本:https://github.com/ruudmens/SysAdminScripts/tree/master/Graph


