question

YaroC-2432 avatar image
0 Votes"
YaroC-2432 asked YaroC-2432 answered

Finding diferences in objects from two lists

I have 2 lists of Windows services as txt files with json content. Not sure why it's json but it's needed for some other operations in this format. This is for pre and post reboot. I saw similar threads but not addressing what I'm trying to achieve here. So basically I'm loading the lists to variables like $pre = get-content pre/post.txt | convertfrom-json and starting with loops foreach($name in $pre.name){
foreach($name in $pre.name){
foreach($aft_name in $aft.name){
if($pre.name -eq $aft.name){

... and here should be some code that would find same name of a service in $pre and match it with corresponding one in $aft checking for status and starttype to see if they match and if so do nothing but if not print out that the status was this and now is that or startup type was this and now is that.
Not sure however how to get powershell to find the corresponding object (if exists!) and make it to compare status and starttype. So far I just ended looping myself in a loop :)
I'd appreciate if someone more familiar with powershell could help with filling the gap.



windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

How about providing a small sample of the JSON from both lists? The Compare-Object cmdlet may be all you need to do the comparison.

0 Votes 0 ·
YaroC-2432 avatar image
0 Votes"
YaroC-2432 answered YaroC-2432 edited

It's just get-service converted to json with | ConvertTo-Json | Tee-Object services.txt
Somebody who wrote that line is using the resultant file in other scripts. I could possibly save the same as plain string but I can't think how it would help. Here I know as far as I can convert it back to json and address each line separately through the foreach loop. So maybe then one loop is sufficient like

 foreach($name in $pre.name){
       if(($name -eq $aft.name){ 
          if(($name.status-eq $aft.status) -and ($name.starttype -eq $aft.starttype)){ then all good...

however how do I make sure I'm comparing the right service status and starttype bound to a given $aft.name not just some random one









5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YaroC-2432 avatar image
0 Votes"
YaroC-2432 answered

OK I think I nailed it. Might not be the best way but here is what I came with
foreach($bfsvc -in $pre){
$ref = $bfsvc
$dif = $aft | ?{$_.name -eq $pre.name}
if(($ref.status -eq $dif.status) -and ($ref.startType -eq $dif.startType)){ ... all good

From my tests it looks like this is working. Only thing that bothers me is that I get status and starttype as numerical values rather than actual names but that's how these values end up in the json file for some reason instead of corresponding names.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.