title: Foreach and If linked issue
body: <p>Here after my code for which I have an issue:</p>
  
  <pre><code>#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 &gt;$null
      Write-Output $device.platform &gt;$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 &gt;$null
      }
  }
  
  foreach ($device in $devices.filteredDevicesDataList) {
      if ($dg.deliveryGroups.name -match $dgroup) {
          Write-Host $device.devicemodel
          Send-Notification
      } else {
          $dgroup = 0
      }
  }
  </code></pre>
  
  <p>Info:</p>
  
  <p>The main object of the code is to send notification to devices based on which group devices are member of.</p>
  
  <p>Example:</p>
  
  <p>iPad member of "DG 2"<br>
  iPhone member of "DG 1"  </p>
  
  <p>Result:</p>
  
  <p><a href="https://i.stack.imgur.com/rsXL9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rsXL9.png" alt="Result"></a></p>
  
  <p>Do I miss something there?</p>