我编写了一个简单的脚本(或至少试图编写),以获得分配给特定Azure AD组的所有MicrosoftIn调优防御策略。
脚本获取所有防御策略,找到链接到这些策略的组ID,然后使用group获取实际名称。
现在,我需要找到一种只显示与特定组名匹配的策略(硬编码)的方法。在脚本的顶部,我有一个带有组名的变量。我只是找不到一种方法来过滤所有的$intent(s),只显示链接到组名称变量的那些。
Connect-MSGraph -ForceInteractive
Update-MSGraphEnvironment -SchemaVersion beta
Connect-MSGraph
Connect-AzureAD
$groupname = "group-name-here"
$intents = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceManagement/intents" | Get-MSGraphAllPages
foreach ($intent in $intents) {
$PolicyID = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceManagement/intents/$($intent.Id)/assignments"
$AssignmentGroupIDs = $PolicyID.value.target.groupID
foreach ($AssignmentGroupID in $AssignmentGroupIDs) {
$AssignmentGroupName = Get-AzureADGroup -ObjectId $AssignmentGroupID
}
}
Write-Host "Number of policies found: $($intents.Id.Count)" -ForegroundColor cyan
Write-Host $AssignmentGroupName.DisplayName
Write-Host $intent.displayName发布于 2021-05-31 08:44:12
您创建并没有分配给变量的任何值都将是脚本块输出的一部分。这意味着你可以这样做:
$result = foreach (...) {
if (condition) {
$value
}
}$result将包含循环中的所有$value。
在上下文中(未经测试,但您有此想法):
$matchingIntents = foreach ($intent in $intents) {
$PolicyID = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceManagement/intents/$($intent.Id)/assignments"
$AssignmentGroupIDs = $PolicyID.value.target.groupID
foreach ($AssignmentGroupID in $AssignmentGroupIDs) {
$AssignmentGroupName = Get-AzureADGroup -ObjectId $AssignmentGroupID
if ($AssignmentGroupName -eq $groupname) {
$intent
break
}
}
}https://stackoverflow.com/questions/67770325
复制相似问题