Uncategorized · September 10, 2020 0

Comparing Services from two computers with Powershell

Hello there,

if you might want to compare services between two servers and want to have it done automatically and quickly; : ) I am sharing with you this code I created long ago… it helps me and save my time

use cases would be when I have a server that behave unordinary in compare to other servers that are totally identical and still functioning properly.

I also use this script to compare between services status before and after restarting a server.

To compare services status I take an Image stored in .csv file listing services and thier status. something looks like this

so first of all, we need to run the code on both the computers that we want to compare between. For that I use the code

$computerName = $env:computername
$logPath = “C:\temp\$computerName” + “_log.csv”
Get-Service | Select DisplayName, Name, Status | sort -Property DisplayName | Export-Csv -path $logPath

as you see below, running the above code creates csv file under C drive


Now to the comparison part… the below code will do the job

$logPath1 = “C:\temp\1.csv”
$S1 = Import-Csv -Path $logPath1
$logPath2 = “C:\temp\2.csv”
$S2 = Import-Csv -Path $logPath2

for ($i = 0; $i -lt $S1.DisplayName.length; $i++) {
         for ($j = 0; $j -lt $S2.DisplayName.length; $j++) {
            if ($S1.DisplayName[$i] -eq $S2.DisplayName[$j]) {
                if ($S1.status[$i] -ne $S2.status[$j]) {
                     Write-Host $S1.DisplayName[$i] “is Not matching”
                   }
            }
        }  
}

Make sure you put both csv files in the right folder where the code can read from; in my case I put them under c:\temp and name them 1and 2 accordingly

So basically, If you run the above code before restarting a computer and again after restarting a computer then you have two csv files that represent two different states of the computer.

the result will show you which services that does not match between the provides csv. something like this

Feel free to use this tool\ method and have fun!

Cheers

Jaber

Close Bitnami banner
Bitnami