我有一个使用 Powershell core 6 环境创建的 azure 函数,
想要运行一些 MS graph powershell 模块,例如“Get-IntuneManagedDevice | Get-MSGraphAllPages” 但这需要我尝试使用“Connect-MSGraph”的令牌,但执行时出现以下错误 错误: 无法从程序集“System.Core,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”加载类型“System.Security.Cryptography.SHA256Cng”。):无法加载类型“System.Security.Cryptography.SHA256Cng”来自程序集“System.Core,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089”。
如果我在任何时候做错了,任何人都可以帮助我修复或纠正我。
请问您是否已经创建了访问 Graph API 之前需要创建的应用程序、密钥等?我创建了一个博客,解释“如何使用 Graph API 和 PowerShell 在 Exchange Online 邮箱中创建邮箱文件夹”。在第一部分中,我将解释在 Azure 租户中创建应用程序的过程。请检查一下。希望对你有帮助
使用 Windows PowerShell 使用 Graph API 在 Exchange Online 邮箱中创建自定义文件夹
谢谢, 马努
以下方式怎么样:
从以下 GitHub 存储库下载最新版本的 Intune Power Shell SDK https://github.com/Microsoft/Intune-PowerShell-SDK/releases
在PowerShell中导入模块
导入模块 .\Microsoft.Graph.Intune.psd1导入模块 .\Microsoft.Graph.Intune.psd1
连接 MS Grpah
连接-MSGraph-AdminConsent
尝试 In-Tune 托管设备连接 (Get-IntuneManagedDevice | Get-MSGraphAllPages)
谢谢, 马努
对于这个问题,我不知道如何在azure powershell函数中使用令牌运行
Get-IntuneManagedDevice
。但我可以在下面提供一个解决方法供您参考(使用rest api在azure powershell函数中获得与您期望的相同的结果)。
1. 打开fildder并在powershell中运行命令
Get-IntuneManagedDevice
,我们可以看到该命令在后端请求microsoft graph api。 api是https://graph.microsoft.com/v1.0/deviceManagement/managedDevices
,这个页面是它的文档(你不需要执行这一步)。
2. 我们需要为您在azure ad中注册的应用程序添加权限。
3. 根据文档我们可以发现graph api只支持Delegate权限类型,不支持Application权限类型。 所以我们不能使用客户端凭据作为授予类型来获取访问令牌(我们不能只使用密钥来获取访问令牌),正如您在上面的评论中提到的。我们需要通过密码授予类型请求访问令牌,因此请使用以下命令来获取访问令牌:
$AppId = 'xxx'
$AppSecret = 'xxx'
$Scope = "https://graph.microsoft.com/.default"
$TenantName = "xxx"
$username = "xxx"
$password = "xxx"
$Url = "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token"
Add-Type -AssemblyName System.Web
$Body = @{
client_id = $AppId
client_secret = $AppSecret
scope = $Scope
username = $username
password = $password
grant_type = 'password'
}
$PostSplat = @{
ContentType = 'application/x-www-form-urlencoded'
Method = 'POST'
Body = $Body
Uri = $Url
}
$Request = Invoke-RestMethod @PostSplat
$Request.access_token
4. 在步骤1中我们知道命令
Get-IntuneManagedDevice
在后端请求graph api,所以我们只需要请求graph api就可以得到结果。
$Uri = "https://graph.microsoft.com/v1.0/deviceManagement/managedDevices"
$Header = @{
Authorization = "$($Request.token_type) $($Request.access_token)"
}
$result = Invoke-RestMethod -Uri $Uri -Headers $Header -Method Get -ContentType "application/json"
$result.value
希望有帮助~