[玩转系统] 将 Microsoft Graph API 与 Powershell 结合使用
作者:精品下载站 日期:2024-12-14 18:45:50 浏览:11 分类:玩电脑
将 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
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag