
close
close
Chance to win $250 in Petri 2023 Audience Survey
In an previous article, I showed you several different techniques for displaying critical service statuses from your domain controllers. Although you could easily apply the technique to any server or service, I prefer PowerShell commands and tools that work with objects in the pipeline. This is especially true if you think you might need to sort, filter, export, or convert the results. But there are probably just as many situations where all you want to do is read some information on the screen. PowerShell has some tricks up its sleeve that you can take advantage of to make this kind of reporting easy and meaningful. All I ask is that you realize that what I’m about to demonstrate should be considered the exception and not the rule.
Let’s use the same domain controllers and services that I used last time.
$dcs = "chi-dc01","chi-dc02","chi-dc04" $svcs = "adws","dns","kdc","netlogon"
Next, let’s look at a typical command you might run.
Get-Service -name $svcs -ComputerName $dcs | format-table @{Name="Computername";Expression={$_.Machinename.toUpper()}},Name,Displayname,Status -AutoSize
Checking domain controller services (Image Credit: Jeff Hicks)
$t = Get-Service -name $svcs -ComputerName $dcs | format-table @{Name="Computername";Expression={$_.Machinename.toUpper()}},Name,Displayname,Status -AutoSize | out-string
This looks the same.
A text version of the table (Image Credit: Jeff Hicks)
$split = $t.split("`n")
See how I now have an array of strings?
Viewing the split string (Image Credit: Jeff Hicks)
[enum]::GetNames([System.ServiceProcess.ServiceControllerStatus])
Service status enumerations (Image Credit: Jeff Hicks)
$other = ([enum]::GetNames([System.ServiceProcess.ServiceControllerStatus])).where({$_ -notmatch "running|stopped"}) -join "|"
Note that I am using the Where() method, introduced in PowerShell 4.0. You can also use the Where-Object
cmdlet. The variable looks like this:
A regular expression pattern (Image Credit: Jeff Hicks)
foreach ($line in $split) { #hashtable of parameters for Write-Host $params=@{Object=$line} if ($line -match "Stopped") { $params.Add("BackgroundColor","red") } elseif ($line -match $other) { $params.Add("BackgroundColor","yellow") } Write-Host @params }
I am using the ForEach enumerator, which makes this a little easier to follow. For every item ($line) in the collection $split, I am doing several things. First, I create a hashtable of parameter values that I intend to splat to Write-Host. I like this approach because I can dynamically adjust parameters based on my needs.
In this case, if the line has the word “Stopped” in it, I’ll set the Backgroundcolor parameter to Red. Or if the string matches anything in my regular expression, I’ll set it to yellow. If there is no match, then the BackgroundColor parameter is undefined when I splat the hashtable to Write-Host.
Here’s my result.
Highlighting stopped services (Image Credit: Jeff Hicks)
foreach ($line in $split) { #append some space after the line Write-Host "$line " -NoNewline if ($line -match "Stopped") { Write-Host " " -BackgroundColor Red -NoNewline } Write-Host " " }
In this example, if the line matches Stopped, I’ll write a blank space with red background color, which gives me this result:
Red light for stopped services (Image Credit: Jeff Hicks)
$marker = [char]31 foreach ($line in $split) { #only process lines with text if ($line -notmatch "^\s+$") { #replace line return with a space Write-Host $($line.Replace("`r"," ")) -NoNewline if ($line -match "Stopped") { Write-Host $marker -foregroundColor Red } else { Write-Host "`r" } } else { #should be blank lines Write-Host $line } }
A PowerShell console variation (Image Credit: Jeff Hicks)
More in PowerShell
Most popular on petri