Microsoft GRAPH 查询使用端点 /deviceManagement/deviceHealthScripts 列出了 detectionscripcontent 的不熟悉格式
Microsoft GRAPH query lists an unfamiliar format for detectionscripcontent using endpoint /deviceManagement/deviceHealthScripts
我将创建一个 JWT 令牌并将其放在 GET 请求的 header 中以验证我的租户。然后我用
Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/deviceManagement/deviceHealthScripts" -Method GET -Headers $headers -ContentType 'application/json' -ErrorAction "continue"
这会检索主动修复脚本 object。只是我不知道实际代码是什么格式。我应该查看我的 PowerShell 脚本,但没有,只是很多随机字符。这是 JSON 文件的一部分。
"detectionScriptContent": "JGVudmlyb25lbW50YWxSZWdwYXRoID0gJ1JlZ2lzdHJ5OjpIS0VZX0xPQ0FMX01BQ0hJTkVcU3lzdGVtXEN1cnJlbnRDb250cm9sU2V0XENvbnRyb2xcU2Vzc2lvbiBNYW5hZ2VyXEVudmlyb25tZW50Jw0KDQokZGVzaXJlZFByb3BlcnR5ID0gIkFSQ0dJU19MSUNFTlNFX0ZJTEUiDQokdmFsID0gIjI3MDA5QGF6ci1saWMtMDEiDQoNCiMgJGRlc2lyZWRQcm9wZXJ0eSA9ICJUTVAiDQojICR2YWwgPSAiQzpcV0lORE9XU1xURU1QIg0KDQoNCiMgVGVzdC1QYXRoIC1QYXRoICdIS0xNOlxTeXN0ZW1cQ3VycmVudENvbnRyb2xTZXRcQ29udHJvbFxTZXNzaW9uIE1hbmFnZXJcRW52aXJvbm1lbnRcUGF0aCcNCiRkZXNpcmVkUHJvcGVydHlFeGlzdHMgPSAoJG51bGwgLW5lIChHZXQtSXRlbVByb3BlcnR5IC1QYXRoICRlbnZpcm9uZW1udGFsUmVncGF0aCAtTmFtZSAkZGVzaXJlZFByb3BlcnR5IC1FcnJvckFjdGlvbiBTaWxlbnRseUNvbnRpbnVlKSkNCg0KV3JpdGUtT3V0cHV0ICRkZXNpcmVkUHJvcGVydHlFeGlzdHMNCg0KaWYoJGRlc2lyZWRQcm9wZXJ0eUV4a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IHRoZW4gcHJvcEV4aXN0cyBtdXN0IGJlIGZhbHNlLg0KICAgIHJldHVybiAkcHJvcEV4aXN0cw0KfQ0KDQppZigoUmVnaXN0cnlJdGVtUGFzc2VzKSAtZXEgJGZhbHNlKXsNCg0KICAgIFdyaXRlLU91dHB1dCAiQ3JlYXRpbmcgdGhlIHByb3BlcnR5ICRkZXNpcmVkUHJvcGVydHkiDQogICAgY3JlYXRlUmVnaXN0cnlQcm9wZXJ0eQ0KfQ0KDQppZihSZWdpc3RyeUl0ZW1QYXNzZXMpew0KICAgIA0KICAgIFdyaXRlLU91dHB1dCAiUmVnaXN0ZXkgaXRlbSBhbiB2YWx1ZSBjaGVja3Mgb3V0LiBFeGl0IDAiOyBFeGl0IDA7DQoNCn1lbHNleyBXcml0ZS1PdXRwdXQgIlNvbWV0aGluZyB3ZW50IHdyb25nLkV4aXQgMSI7IGV4aXQgMTsgfQ0KDQoNCg==",
Microsoft 文档说它是二进制的,但事实并非如此
https://docs.microsoft.com/en-us/graph/api/intune-devices-devicehealthscript-update?view=graph-rest-beta
您的 属性 值是 Base64 编码的字节,表示 UTF-8 编码的字符串。
如果一个给定的字符串是由看似随机的字符组成的,主要由数字和大小写字母组成,可选地后跟一两个=
,那么它很可能代表Base64 编码数据。
Base64 能够编码 any 二进制数据(字节数组),所以在摘要中没有说明 what 正在编码:
- 但是,鉴于您的案例中的属性名称包含“ScriptContent”,可以合理地假设 text 正在被编码。
- 这就留下了问题 使用什么字符编码 来创建 Base64 编码的二进制数据。 UTF-8是一种常见的字符编码,确实是你的情况。
您可以按如下方式解码它们(进入plain-text .NET 字符串)(使用简单的示例输入字符串):
$bytes = [Convert]::FromBase64String('SGkgdGhlcmUu')
[Text.Encoding]::Utf8.GetString($bytes) # -> 'Hi there.'
到编码:
$bytes = [Text.Encoding]::Utf8.GetBytes('Hi there.')
[Convert]::ToBase64String($bytes) # -> 'SGkgdGhlcmUu'
我将创建一个 JWT 令牌并将其放在 GET 请求的 header 中以验证我的租户。然后我用
Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/deviceManagement/deviceHealthScripts" -Method GET -Headers $headers -ContentType 'application/json' -ErrorAction "continue"
这会检索主动修复脚本 object。只是我不知道实际代码是什么格式。我应该查看我的 PowerShell 脚本,但没有,只是很多随机字符。这是 JSON 文件的一部分。
"detectionScriptContent": "JGVudmlyb25lbW50YWxSZWdwYXRoID0gJ1JlZ2lzdHJ5OjpIS0VZX0xPQ0FMX01BQ0hJTkVcU3lzdGVtXEN1cnJlbnRDb250cm9sU2V0XENvbnRyb2xcU2Vzc2lvbiBNYW5hZ2VyXEVudmlyb25tZW50Jw0KDQokZGVzaXJlZFByb3BlcnR5ID0gIkFSQ0dJU19MSUNFTlNFX0ZJTEUiDQokdmFsID0gIjI3MDA5QGF6ci1saWMtMDEiDQoNCiMgJGRlc2lyZWRQcm9wZXJ0eSA9ICJUTVAiDQojICR2YWwgPSAiQzpcV0lORE9XU1xURU1QIg0KDQoNCiMgVGVzdC1QYXRoIC1QYXRoICdIS0xNOlxTeXN0ZW1cQ3VycmVudENvbnRyb2xTZXRcQ29udHJvbFxTZXNzaW9uIE1hbmFnZXJcRW52aXJvbm1lbnRcUGF0aCcNCiRkZXNpcmVkUHJvcGVydHlFeGlzdHMgPSAoJG51bGwgLW5lIChHZXQtSXRlbVByb3BlcnR5IC1QYXRoICRlbnZpcm9uZW1udGFsUmVncGF0aCAtTmFtZSAkZGVzaXJlZFByb3BlcnR5IC1FcnJvckFjdGlvbiBTaWxlbnRseUNvbnRpbnVlKSkNCg0KV3JpdGUtT3V0cHV0ICRkZXNpcmVkUHJvcGVydHlFeGlzdHMNCg0KaWYoJGRlc2lyZWRQcm9wZXJ0eUV4aXN0cyl7DQoNCiAgICBXcml0ZS1PdXRwdXQgIkRldGVjdGVkIFByb3BlcnR5ICRkZXNpcmVkUHJvcGVydHkgZXhpc3RzLiINCg0KfWVsc2V7IFdyaXRlLU91dHB1dCAiJGRlc2lyZWRQcm9wZXJ0eSBkb2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B1dCAiQ3JlYXRpbmcgdGhlIHByb3BlcnR5ICRkZXNpcmVkUHJvcGVydHkiDQogICAgY3JlYXRlUmVnaXN0cnlQcm9wZXJ0eQ0KfQ0KDQppZihSZWdpc3RyeUl0ZW1QYXNzZXMpew0KICAgIA0KICAgIFdyaXRlLU91dHB1dCAiUmVnaXN0ZXkgaXRlbSBhbiB2YWx1ZSBjaGVja3Mgb3V0LiBFeGl0IDAiOyBFeGl0IDA7DQoNCn1lbHNleyBXcml0ZS1PdXRwdXQgIlNvbWV0aGluZyB3ZW50IHdyb25nLkV4aXQgMSI7IGV4aXQgMTsgfQ0KDQoNCg==",
Microsoft 文档说它是二进制的,但事实并非如此 https://docs.microsoft.com/en-us/graph/api/intune-devices-devicehealthscript-update?view=graph-rest-beta
您的 属性 值是 Base64 编码的字节,表示 UTF-8 编码的字符串。
如果一个给定的字符串是由看似随机的字符组成的,主要由数字和大小写字母组成,可选地后跟一两个
=
,那么它很可能代表Base64 编码数据。Base64 能够编码 any 二进制数据(字节数组),所以在摘要中没有说明 what 正在编码:
- 但是,鉴于您的案例中的属性名称包含“ScriptContent”,可以合理地假设 text 正在被编码。
- 这就留下了问题 使用什么字符编码 来创建 Base64 编码的二进制数据。 UTF-8是一种常见的字符编码,确实是你的情况。
您可以按如下方式解码它们(进入plain-text .NET 字符串)(使用简单的示例输入字符串):
$bytes = [Convert]::FromBase64String('SGkgdGhlcmUu')
[Text.Encoding]::Utf8.GetString($bytes) # -> 'Hi there.'
到编码:
$bytes = [Text.Encoding]::Utf8.GetBytes('Hi there.')
[Convert]::ToBase64String($bytes) # -> 'SGkgdGhlcmUu'