0

this is my code


$Packets = @()

$Packets += Test-PXESever -ProcessorArchitecture 0 -Verbose

$Packets += Test-PXESever -ProcessorArchitecture 7 -Verbose

The output of the array $Packets looks like this:

Op          : BootResponse
HType       : Ethernet
HLen        : 6
Hops        : 0
XID         : E72347EB
Secs        : 0
Flags       : {Broadcast}
CIAddr      : 0.0.0.0
SIAddr      : 10.164.28.3
SName       : 
File        : 


Op          : BootResponse
HType       : Ethernet
HLen        : 6
Hops        : 0
XID         : E72347EB
Secs        : 0
Flags       : {Broadcast}
CIAddr      : 10.164.28.74
SIAddr      : 164.3.15.61
File        : smsboot\Ps02\x86\pxe.com

I want to check if $Packets contains the string "smsboot" and if $Packets contains the string "164.3.15.61"

In this case it does. So I tried to do:

if($Packets -contains "smsboot"){
Write-Host "Contains smsboot"}
else {Write-Host "Does not contain smsboot"}

and same with the IP address. But it always says it does not contain the strings, although it does. What am I doing wrong?

thanks!

user18209625
  • 139
  • 2
  • 15
  • 1
    You need to filter the required objects on their property using [`Where-Object`](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/where-object): `$packets |Where-Object File -Like smsboot*` – iRon May 17 '23 at 09:34

1 Answers1

0

I would go for something like that even if your output expectation is unclear

$Packets = @()

$Packets += Test-PXESever -ProcessorArchitecture 0 -Verbose
$Packets += Test-PXESever -ProcessorArchitecture 7 -Verbose

$Packets | Where-Object { $_.name -like "*smsboot*" -or $_.CIAddr -like "10.164.28.74" }
Civette
  • 386
  • 2
  • 10
  • 2
    [try avoid using the increase assignment operator (`+=`) to create a collection](https://stackoverflow.com/a/60708579/1701026) as it is exponentially expensive. – iRon May 17 '23 at 17:09