我尝试通过对 GraphAPI Beta 的 1 次调用来获取所有移动应用程序及其检测规则。使用 PostMan,发送 1 个 GET 请求
https://graph.microsoft.com/beta/deviceAppManagement/mobileApps?$filter=isof('microsoft.graph.win32LobApp')&$top=1000
将返回 JSON 格式的前 100 个移动应用程序列表。每个对象都有一个检测规则,其中包含各种数据,但在使用 Graph Beta 库在 C# 中执行相同操作时情况并非如此(至少据我所知)。
这是我编写的一个方法,用于尝试获取分配 ID、检测规则和createdOn:
public async Task<List<GraphResponseModel>> GetAllWin32LobAppWithDetectionRules()
{
try
{
Win32LobAppRegistryDetection detectionRule = null;
List<GraphResponseModel> graphResponseModelList = new();
List<string?> idsFromAssignment = [];
// Get all Win32LobApps
var mobileApps = await _graphClient.DeviceAppManagement.MobileApps.GetAsync(context =>
{
context.QueryParameters.Filter = "isof('microsoft.graph.win32LobApp')";
context.QueryParameters.Expand = new string[] {"assignments"};
});
foreach (var mobileApp in mobileApps.Value)
{
mobileApp.Assignments.ForEach(assignment =>
{
idsFromAssignment.Add(assignment.Id);
});
// Cast the mobileApp to a Win32LobApp
var win32LobApp = (Win32LobApp)mobileApp;
_detectionRulesModel = null;
foreach (var detection in win32LobApp.DetectionRules)
{
if (detection.OdataType == "#microsoft.graph.win32LobAppRegistryDetection")
{
detectionRule = (Win32LobAppRegistryDetection)detection;
_detectionRulesModel = new DetectionRulesModel(detectionRule.OdataType, (bool)detectionRule.Check32BitOn64System, detectionRule.KeyPath, detectionRule.ValueName,detectionRule.DetectionType.Value.ToString(), detectionRule.Operator.ToString(), detectionRule.DetectionValue);
}
else
{
continue;
}
_createdOn = mobileApp.CreatedDateTime.Value.DateTime;
graphResponseModelList.Add(new GraphResponseModel(idsFromAssignment, _detectionRulesModel, _createdOn));
}
}
return graphResponseModelList;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
不幸的是,我收到回复,其中包含包含我拥有的实际软件数量的列表,但仅填充了
_createdOn
字段,其他字段为空。
我收到回复,其中包含包含我拥有的实际软件数量的列表,但仅填充了
字段,其他字段为空。_createdOn
首先,检查他们是否正确访问检测规则和分配。然后,在循环内将值正确添加到模型中。
代码:
public async Task<List<GraphResponseModel>> GetAllWin32LobAppWithDetectionRules()
{
try
{
List<GraphResponseModel> graphResponseModelList = new List<GraphResponseModel>();
// Get all Win32LobApps
var mobileApps = await _graphClient.DeviceAppManagement.MobileApps.GetAsync(context =>
{
context.QueryParameters.Filter = "isof('microsoft.graph.win32LobApp')";
context.QueryParameters.Expand = new string[] { "assignments", "detectionRules" };
});
foreach (var mobileApp in mobileApps.Value)
{
List<string> assignmentIds = mobileApp.Assignments.Select(assignment => assignment.Id).ToList();
DateTime createdOn = mobileApp.CreatedDateTime.Value.DateTime;
List<DetectionRulesModel> detectionRulesModels = new List<DetectionRulesModel>();
// Check if the mobileApp has detection rules
if (mobileApp.DetectionRules != null)
{
foreach (var detectionRule in mobileApp.DetectionRules)
{
if (detectionRule.ODataType == "#microsoft.graph.win32LobAppRegistryDetection")
{
var win32LobAppRegistryDetection = (Win32LobAppRegistryDetection)detectionRule;
DetectionRulesModel detectionRulesModel = new DetectionRulesModel(
detectionRule.ODataType,
win32LobAppRegistryDetection.Check32BitOn64System,
win32LobAppRegistryDetection.KeyPath,
win32LobAppRegistryDetection.ValueName,
win32LobAppRegistryDetection.DetectionType?.ToString(),
win32LobAppRegistryDetection.Operator?.ToString(),
win32LobAppRegistryDetection.DetectionValue
);
detectionRulesModels.Add(detectionRulesModel);
}
}
}
graphResponseModelList.Add(new GraphResponseModel(assignmentIds, detectionRulesModels, createdOn));
}
return graphResponseModelList;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
在上面,我们直接访问
mobileApp.Assignments
和 mobileApp.DetectionRules
并在循环中正确填充 DetectionRulesModel
列表和 assignmentIds
列表。
然后将填充的模型添加到循环外部的
graphResponseModelList
列表中。
下面表示移动应用程序的创建时间。
{
"assignmentIds": ["assignment1", "assignment2"],
"detectionRules": {
"OdataType": "#microsoft.graph.win32LobAppRegistryDetection",
"Check32BitOn64System": true,
"KeyPath": "HKEY_LOCAL_MACHINE\\Software\\MyApp",
"ValueName": "Version",
"DetectionType": "RegistryValueExists",
"Operator": "Equals",
"DetectionValue": "1.0"
},
"createdOn": "2024-05-10T15:30:00Z"
}
参考: