#@
#local created folders should be created in order to use the script.Pay attention on the savepath variables
$StartTime = (Get-Date)
#reference objects folder
$savepathR = ‘C:\’
#difference objects folder
$savepathD = ‘C:\’
#Take services snapshot from the servers
#It should run once
#as a Test i run it only for the Domain Controllers
#servers to check
$servername = Get-ADComputer -SearchBase “ou=domain controllers,dc=test,dc=com” -filter * | ForEach-Object { Get-ADDomainController -Identity $_.DNSHostname}
#uncomment the following 7 lines to take a new snapshot
<#
if (test-path “$savepathR\ref.csv”){Remove-Item “$savepathR\ref.csv”}
foreach ($server in $servername) {
if (Test-Connection $server -count 1 -quiet){
get-service -computername $server -name * | select machinename, name, status, StartType | export-csv “$savepathR\ref.csv” -delimiter ‘ ‘ -Append }
}
#>
#runs on demand to take a momentarilly snapshot of the services
#i run it once a day as part of my daily routine
if (test-Path “$savepathD\dif.csv”){Remove-Item “$savepathD\dif.csv”}
foreach ($server in $servername) {
if (Test-Connection $server -count 1 -quiet){
get-service -computername $server -name * | select machinename, name, status, StartType | Export-Csv “$savepathD\dif.csv” -delimiter ‘ ‘ -Append}
}
#read the 2 files in 2 variables
$f1 = “$savepathr\ref.csv”
$f2 = “$savepathd\dif.csv”
Compare-Object (Get-Content $f1) (Get-Content $f2) | ForEach-Object {
$i = 0; $ht = @{}; $vals = -split $_.InputObject
foreach($col in ‘machinename’, ‘name’, ‘Status’, ‘Starttype’) {
$ht.$col = $vals[$i++]
}
$ht.Before = $_.SideIndicator -eq ‘<=’
[pscustomobject] $ht
} | Group-Object machinename, name | ForEach-Object {
$ndxBefore, $ndxAfter = if ($_.Before) { 0, 1 } else { 1, 0 }
[pscustomobject] @{
machinename = $_.Group[0].machinename
name = $_.Group[0].name
‘Status Before’ = $_.Group[$ndxBefore].Status
‘StartType Before’ = $_.Group[$ndxBefore].starttype
‘Status After’ = $_.Group[$ndxAfter].status
‘StartType After’ = $_.Group[$ndxAfter].starttype
}
} | Sort-Object MachineName, name |
Out-GridView -Title ‘Service control’
#Format-Table
$([timespan]::fromseconds(((Get-Date)-$StartTime).Totalseconds).ToString(“mm\:ss”))
#It throws the window (gridview) only when there is a difference spotted.