Let us get started by studying PowerShell's 'If' logic.
The secret is to identify the two parts of the construction: (test
condition) and {block command}.
It helps to keep one eye on PowerShell's different types of bracket.
(The parenthesis style of brackets are used for the first part, namely the condition), while {braces
denote the block command
payload}.
If (condition) {Block Command}
Here is a different explanation of the same 'If' flow control:
If
(test) {execute when true}
Summary: The PowerShell 'If' conditionally executes a
block statement,
providing the test expression is true.
Think of this script as a preamble which checks whether the Alerter
service is installed on your machine, and introduces the basic 'If'
statement. There is no -not comparison here, that comes in
Example 2.
# PowerShell script to check if a service is installed $SrvName
= "Alerter" $Service = Get-Service -display $SrvName -ErrorAction
SilentlyContinue If ($Service) {$SrvName + " is installed on this
computer."} Else {"No Alerter service indicates Windows 7 or
Server 2008"}
Learning Points
Note 1: The If (test) is simply this: can
Get-Service find the $SrvName. Try changing "Alerter" to "Print
Spooler"
Note 2: Avoid over-think; there is no 'Then' in
a PowerShell 'If'
statement. Furthermore, there is no endif in PowerShell as would be in VBScript.
Guy
Recommends: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, SolarWinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your PowerShell scripts. Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory or Exchange Server.
The purpose of this script is to check for the Alerter service, the
reason being Windows 7 machines no longer install this service.
Observe how -not reverses the 'If' logic compared with
Example 1.
# PowerShell script to check if a service is -NOT installed
$SrvName = "Alerter" $Service = Get-Service -display $SrvName
-ErrorAction SilentlyContinue If (-not $Service)
{$SrvName + " is not installed on this computer."} Else {"You
probably have $SrvName, thus machine is Vista or W2K3"}
Note 3: Other examples of If logic benefit
from chaning -not to -ne meaning not equal.
Here is a variation which employs -ne (not equal) rather than -not.
# PowerShell script to check if a service is not equal to ""
$SrvName = "Alerter" $Service = Get-Service -display $SrvName
-ErrorAction SilentlyContinue If ($Service -ne "") {$SrvName + "
is not installed on this computer."} Else {"You probably have
$SrvName, thus machine is Vista or W2K3"}
Note 4: This is a crude and forced example of
-ne, a better example for 'not equal' would be testing numeric data.
Example 3a: PowerShell If -ne Numeric Data
If (Test numbers not equal) {Script block}
$GuyNum = "12" if($GuyNum -ne '7'){"Guy's number is not seven"}
Else {"Guy's number is $GuyNum "}
Note 5: My biggest problem with not equal
is wanting to write -neq, when I should be typing: -ne. So
often it's overthink that is my downfall!
Note 6: While this a pretty crude example, it
does plant the idea that you could use other comparators such as greater
than -gt, or less than -lt.
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
SolarWinds'
Orion performance monitor
will help you discover what's happening on your network. This
utility will also guide you through troubleshooting; the dashboard will
indicate whether the root cause is a broken link, faulty equipment or
resource overload.
Perhaps the NPM's best feature is the way it suggests solutions to network
problems. Its
second best feature is the ability to monitor the health of individual VMware
virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you take advantage of SolarWinds' offer.
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
For More information refer to the built-in About_IF file
Clear-Host Get-Help about_Comparison_Operators
Summary of PowerShell's If -Not Construction
When it comes to filtering output, one of the oldest and best statements
is the 'If' clause. As usual, the secret of understanding the syntax
is to pay close attention to the style bracket. If (parenthesis for
the test) and {braces for the action}. Once you have mastered the
basic 'If' statement, then extend your capabilities by researching -Not,
-ne, and also -or.
If you like this page then please share it with your friends
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, SolarWinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.