Finding diferences in objects from two lists

YaroC 311 Reputation points
2021-10-22T10:11:58.697+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,364 questions
{count} votes

2 answers

Sort by: Most helpful
  1. YaroC 311 Reputation points
    2021-10-25T09:25:51.69+00:00

    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

    0 comments No comments

  2. YaroC 311 Reputation points
    2021-10-25T10:37:58.257+00:00

    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.

    0 comments No comments