Comparing two variable properties

davesnb
davesnb used Ask the Experts™
on
I have 2 variables with the same property values ( computername) . I wish to find the value that does not match . So below , I have my list of computers in var pageresponse in a invoke-webrequest stored variable .

$pageresponse.pscomputername

iasc9-web04
iasc9-web05
iasc9-web06
iasc9-web07
iasc9-web15
iasc9-web16
iasc9-web25
iasc9-web26
iasc9-web27
iasc9-web28
iasc9-web29
iasc9-web30
iasc9-web31
iasc9-web32
iasc9-web33

Open in new window


And , i also have a list of "started" computers in a stored variable of var publicstarted of property $publicstarted.pscomputername

$publicstarted.pscomputername
iasc9-web04
iasc9-web05
iasc9-web06
iasc9-web07
iasc9-web15
iasc9-web16
iasc9-web25
iasc9-web26
iasc9-web27
iasc9-web28
iasc9-web29
iasc9-web30
iasc9-web31
iasc9-web32
iasc9-web33
iasc9-web34

Open in new window


So I just wish to compare the 2 and return the value that is not matching , so in this example, iasc9-web34 should be returned and stored in a new var.

Your help is appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
$diff = $pageresponse.pscomputername | ? { $_ -notin $publicstarted.pscomputername }

Open in new window

Top Expert 2014
Commented:
I'm going to assume that $pageresponse and $publicstarted are object arrays that each have a pscomputername property.
$diff = Compare-Object -ReferenceObject $pageresponse -DifferenceObject $publicstarted -Property pscomputername | Select -ExpandProperty PsComputerName

Open in new window

This will work whether there are extra items in either array.

However, if $pageresponse and $publicstarted aren't object arrays, then the approach would have to be changed.  For example, if $pageresponse is a single object, and its pscomputername property is a multiline string, or a string array.  To check, try providing the result of the following:
$pageresponse.GetType()
$pageresponse | gm

Open in new window

Author

Commented:
Yes, thank you the first worked !

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial