Book Image

Microsoft Exchange Server PowerShell Essentials

By : Biswanath Banerjee
Book Image

Microsoft Exchange Server PowerShell Essentials

By: Biswanath Banerjee

Overview of this book

PowerShell has become one of the most important skills in an Exchange administrator's armory. PowerShell has proved its mettle so widely that, if you're not already starting to learn PowerShell, then you're falling behind the industry. It isn't difficult to learn PowerShell at all. In fact, if you've ever run commands from a CMD prompt, then you'll be able to start using PowerShell straightaway. This book will walk you through the essentials of PowerShell in Microsoft Exchange Server and make sure you understand its nitty gritty effectively. You will first walk through the core concepts of PowerShell and their applications. This book discusses ways to automate tasks and activities that are performed by Exchange administrators and that otherwise take a lot of manual effort. Microsoft Exchange PowerShell Essentials will provide all the required details for Active Directory, System, and Exchange administrators to help them understand Windows PowerShell and build the required scripts to manage the Exchange Infrastructure.
Table of Contents (17 chapters)
Microsoft Exchange Server PowerShell Essentials
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Using If statements


In this section, we will use the if conditional statement If to execute statements based on a specific condition test to be true. We can also use the If command to do tests on more than one condition or even if all the other conditions are evaluated to be false.

The following example shows the If statement syntax:

        if (<Condition1>) 
                {<Command Block 1>}
        [elseif (<Condition2>)
                {<Command Block 2>}]
        [else
                {<Command Block 3>}]

We will use the if command to check the status of the services running on the computer, and if the status is stopped, it will be displayed in red and the running services will be displayed in green. We will use a combination of pipeline, the foreach-object cmdlet, and the if statement.

PS C:\> get-service | foreach-object {if ($_.status -eq "stopped") {write-host -foregroundcolor red $_.Displayname}` else {write-host -foregroundcolor green $_.Displayname }} 

Let's take a look at another example where you would like to find whether a particular folder is created. We will use a Test-Path cmdlet, which will return true if it finds the folder and false if it does not. As I am using the statement on my Active Directory domain controller, the folder is not present under Program Files, and this is what you will see in the output:

PS C:\> $ExchangeFolder = "C:\Program Files\Microsoft\Exchange Server"
PS C:\> $FolderPresent = Test-Path $ExchangeFolder
PS C:\> if ($FolderPresent -eq $True) {Write-Host "Exchange installation Folder found on this Server"} else {Write-Host "Exchange installation folder not present"}
Exchange installation folder not present