Do Scripters Dream of Magenta-Colored Text?
To tell you the truth we don’t know what scripters dream about and we’re not so sure that we wantto know. We’re willing to bet, however, that any time scripters dream they dream in color.
How do we know that? That’s easy: after all, about the only time scripters cansee colors is in their dreams. To paraphrase Henry Ford, when it comes to using Wscript.Echo to echo data back to the command window, scripters can choose any color they want … as long as it’s white text on a black background.
Note. Yes, or whatever colors you’ve set as your command window defaults. But you know what we mean.
In other words, it’s a big, bright, beautiful, and multi-colored world … as long as you’re not writing system administration scripts. Here’s a riddle for you: what’s black and white with no red (or any other color) all over? You got it: data echoed back to the command window.
Note. OK, fine: we’ll also accept a zebra. And a panda bear. But that wasn’t really what we were looking for.
One thing that isn’tblack and white with no red all over is the new Windows PowerShell . Have you ever fantasized about reporting back data using magenta-colored text? (Don’t feel bad: we wouldn’t want to admit to that, either.) Well, don’t just dream about it, do it:
Before you ask, no,
this isn’t trick photography or Scripting Guy special effects:
that’s magenta-colored script output. And here’s how we did it:
$strComputer = "."
Note. In Windows PowerShell the grave accent character (`)
is used to indicate a line break. In that respect it is equivalent
to the underscore character (_) in WSH and VBScript. We won’t discuss
most of the code shown in this article; if you’d like to know more
about accessing WMI data using Windows PowerShell take a peek at
Accessing WMI from Windows PowerShell,
which explains the ins and outs of using Windows PowerShell to get
to WMI data. For now we’re going to focus exclusively on
write-host, the Cmdlet used to write data to the command
window. Write-host is similar to Wscript.Echo: you simply call
write-host followed by the data you want to echo back. For example,
here’s a very simple script that echoes back the results of the
equation 2 + 2:
write-host (2 + 2)
Note. We won’t keep you in suspense: the answer is 4. Granted, that’s
probably not the most exciting thing you’ve ever seen. But would
you still feel that way if the result of that equation was
displayed in magenta text? Impossible, you say? Well, then fire up
your copy of Windows PowerShell and type in the following:
write-host (2 + 2)
–foreground "magenta"
Told you.
As it turns out, the
–foregroundcolorparameter enables you to change the color of
the text being output by write-host. As you saw in the first script
we showed you, we were able to get magenta-colored text just by
adding the –foregroundcolor parameter and specifying “magenta” as
the color:
write-host $objItem.Name,
$objItem.WorkingSetSize -foregroundcolor "magenta"
Nice, huh? And don’t
worry: if you don’t like magenta you have other colors at your
disposal. In fact, you can set the foreground color and/or the
background color to any of the following:
·
Black
·
Blue
·
Cyan
·
DarkBlue
·
DarkCyan
·
DarkGray
·
DarkGreen
·
DarkMagenta
·
DarkRed
·
DarkYellow
·
Gray
·
Green
·
Magenta
·
Red
·
White
·
Yellow
What’s that? Did we
say “and/or the background color?” As a matter of fact, we did: you
can use the
–backgroundcolorparameter to set the background color of the
output. For example, here’s a Windows PowerShell script that
displays output using magenta-colored text on a yellow
background:
$strComputer = "."
And here’s what the
command window looks like after you run the script:
OK, so now you know
why none of the Scripting Guys made it into art school. But you get
the idea.
Here’s a better use
of color. This script retrieves a list of processes and then
reports back the name and working set size for each one. The added
twist? If a process has a working set size greater than 3,000,000
bytes the information for that process is displayed using magenta
text; otherwise the process information is displayed using white
text. Here’s the script:
$strComputer = "."
And this is what the
output looks like:
Needless to say,
this makes it easy for you to just glance at the command window and
pick out the processes with the largest working set sizes.
And no, you’re
notdreaming: it’s all real. Henry Ford would be so
jealous.
$colItems = get-wmiobject
-class "Win32_Process" -namespace "root\CIMV2" `
-computername $strComputer
| write-output
foreach ($objItem in
$colItems) {
write-host $objItem.Name,
$objItem.WorkingSetSize -foregroundcolor "magenta"
}
$colItems = get-wmiobject -class "Win32_Process" -namespace
"root\CIMV2" `
-computername $strComputer | write-output
foreach ($objItem in $colItems) {
write-host $objItem.Name,
$objItem.WorkingSetSize `
-foregroundcolor "magenta"
–backgroundcolor "yellow"
}
$colItems = get-wmiobject -class "Win32_Process" -namespace
"root\CIMV2" `
-computername $strComputer | write-output
foreach ($objItem in $colItems) {
if ($objItem.WorkingSetSize -gt
3000000) {
write-host $objItem.Name,
$objItem.WorkingSetSize -foregroundcolor "magenta" }
else {write-host $objItem.Name,
$objItem.WorkingSetSize}
}