
close
close
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.
advertisment
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)
advertisment
$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 [email protected]{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)
advertisment
More from Jeff Hicks
advertisment
Petri Newsletters
Whether it’s Security or Cloud Computing, we have the know-how for you. Sign up for our newsletters here.
advertisment
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri
Log in to save content to your profile.
Article saved!
Access saved content from your profile page. View Saved
Join The Conversation
Create a free account today to participate in forum conversations, comment on posts and more.
Copyright ©2019 BWW Media Group