筛选Powershell会导致For Each语句



我已经编写(或试图至少编写(一个简单的脚本来获取分配给特定Azure AD组的所有Microsoft Intune Defender策略。

脚本获取所有Defender策略,找到链接到这些策略的组ID,然后使用Get-AzureADGroup获取实际名称。

现在,我需要找到一种方法,只显示与特定组名称(硬编码(匹配的策略。在脚本的顶部,我有一个具有组名称的变量。我只是找不到一种方法来过滤所有的$intent,只显示链接到组名变量的那些。

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

您创建且分配给变量的任何值都将是脚本块输出的一部分。这意味着你可以做这样的事情:

$result = foreach (...) {
if (condition) {
$value
}
}

并且CCD_ 1将包含来自循环的所有CCD_。

在上下文中(未经测试,但你明白了(:

$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
}
}
}

最新更新