Can't answer your specific question but you have a logic error in your code.
$iOSDevices = $devices | where-Object{ $_.SerialNumber -eq "$serial"}
...
if ($iOSDevices.Count -eq 0){
$Context.LogMessage("No Device found for this serial number $($Serial)", "warning")
}
If there are no devices matching the serial number, $iOSDevices
will be null
and therefore won't have a .Count
property. Instead, change your if
statement to
if ($null -eq $iOSDevices) { ... }