Foreach和If链接问题

时间:2017-09-27 21:27:07

标签: powershell

在我遇到问题的代码之后:

#Delivery Groups Information
$dgroup = Invoke-RestMethod -Uri "https://${XMS}:4443/xenmobile/api/v1/deliverygroups/filter" -Body '{}' -Headers $headers -Method Post

$new = 0
$count = $dgroup.dglistdata.dglist.length
for ($v=0; $v -lt $count; $v++) {
    foreach ($dglistdata in $dgroup) {
        Write-Host $dglistdata.dglistdata.dglist[$new].name
        $new++
    }
}

$Host.UI.RawUI.ForegroundColor = "white"
$dgroup = Read-Host -Prompt "Please provide Delivery Group Name for which notification will be sent"
$message = Read-Host -Prompt "Please provide the message to be sent"

#Devices
$devices = Invoke-RestMethod -Uri "https://${XMS}:4443/xenmobile/api/v1/device/filter" -Body '{}' -Headers $headers -Method Post

foreach ($device in $devices.filteredDevicesDataList) {
    Write-Output $device.id >$null
    Write-Output $device.platform >$null
}

foreach ($device in $devices.filteredDevicesDataList) {
    $url = "https://${XMS}:4443/xenmobile/api/v1/device/" + $device.id + "/deliverygroups"
    $global:dg = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
    foreach($deliverygroups in $dg) {
        Write-Output $dg.deliveryGroups.name >$null
    }
}

foreach ($device in $devices.filteredDevicesDataList) {
    if ($dg.deliveryGroups.name -match $dgroup) {
        Write-Host $device.devicemodel
        Send-Notification
    } else {
        $dgroup = 0
    }
}

的信息:

代码的主要目标是根据设备所属的组设备向设备发送通知。

示例:

“DG 2”的iPad会员
“DG 1”的iPhone会员

结果:

Result

我想念那里的东西吗?

1 个答案:

答案 0 :(得分:0)

事实上,我认为我找到了解决方案,我需要在另一个循环中包含我的循环通知:

foreach($device in $devices.filteredDevicesDataList)
{
$url = "https://${XMS}:4443/xenmobile/api/v1/device/" + $device.id + "/deliverygroups"
$Global:dg=Invoke-RestMethod -Uri $url -Headers $headers -Method Get
foreach($deliverygroups in $dg)
{
write-output $dg.deliveryGroups.name >$Null
If($dg.deliveryGroups.name -match $dgroup)
{
write-host $device.devicemodel
Send-notification
}
}
}