PowerShell 101 for Messaging Administrator and IT Pros (Part 2)

If you would like to read the other parts in this article series please go to:

Before going to the script we are going to do a short detour and see a few concepts required for our future scripts, such as: Variables, Operators and If Statement.

Using Variables…

Using PowerShell we can create variables to receive content and there are several types of variables but for the purposes of this article series we can just use the syntax $VariableName = <Value> which can be a numerical, string and any other thing that you want to add there. An example of using a string and numerical value:

$vName =”Andy”

$vUsers = 10

The good thing is that we can manage these variables after definition, in the example (Figure 01) below we defined the variables, then we print and after adding more content to them, we finally print again (we added string “ Patricio” to the $vName, and added 5 to the vUsers). The result can be seen on the right side where the string was added and the numerical value was added.


Figure 01

Managing Operators…

PowerShell provides a universe of operators and you will get used to them as you go, but to build scripts the arithmetic operators (+-*/) and comparison operators are the most important. We saw in the previous sections an example of the arithmetic operator. Here is a list of a couple of useful operators.

  • -eq : if the both values are equal
  • -ne: If the both values are not equal to
  • -gt: if one of the values is greater than the other value
  • -lt: If one of the values is less than the other value

If Statement…

Now that we have an idea about comparison operators, we can check the If statement. Basically, the If Statement will check one expression against another expression using one comparison operator, and then we can define action if the result is true or another action if it is false. Simple as that! The following syntax shows how the If statement is organized.

If ( Variable <operator> Variable) {

                [Commands]

} Else {

                [Commands]

}

Note:
There is also ElseIf that we can use but for the sake of simplicity we can start just with If and Else.

We are not going over examples on this section because in the following section we are going to use If statement.

Starting more complex scripts…

In some situations the script must be dynamic and the IT Pro must be able to pass information to the script to perform a task. In a more automated scenario the script can receive just a file containing several objects to perform its tasks. In this section we will check how we use parameters with a script and use switches to manage script actions.

In the first example, let’s say we want a script that is given the mailbox name, and the script returns the Display Name, Mailbox Database, and Exchange Server. We do know that Get-Mailbox retrieves all information related to a mailbox, we can run Get-Mailbox <mailbox> | FL and the output will result all attributes and its values. Let’s find what we are looking for – Name, ServerName and Database. Let’s run the cmdlet again but now selecting just the output that we need for our script and the result will be similar to the one in figure 02 below.


Figure 02

Now, that we have tested with a single user and we are sure that the cmdlet is working properly we can create a new file called mbxinfo.ps1, insert the first line write-host “Hello world” like we have done in the previous article, and then run .\mbxinfo.ps1. We can pass 9 parameters on a second attempt and the results are the same, as shown in figure 03.


Figure 03

The issue here is that we are not working with the parameters in our script and that is the reason why the information entered is not being used. Each parameter is identified by a space after the script name and they always start with 0. In the script file we can use $args[X] where X is the position of the parameter entered in the command line. Let’s change our simple script to write also the second and seventh position as shown in figure 04.


Figure 04

Now if we run the same script we can see that the results of write-host (Figure 05) showing the content at positions 2nd and 7th which are possible parameters for our script.


Figure 05

Cool, isn’t it? So, now it becomes easier to put pieces together and have the results that we expect from our script.

Let’s remove all lines from our current script and add the cmdlet that we tested before, however let’s replace administrator by $args[0], as shown in Figure 06. Also, let’s give more information to the user that is running the script by using the first line Write-Host Retrieving info of $args[0].


Figure 06

Now if you need a helpdesk to retrieve that information you just need to run .\mbxinfo.ps1 <mailbox-name> and the results will be similar to the one show in the Figure 07.


Figure 07

The Helpdesk loved your script and now they want some improvements where they require a switch to list the same information but for the entire server. So, we defined that if the user add –server <ServerName> we are going to do just that.

First of all, let’s evaluate the two possible entries that we can receive, the first one is –Server <Server-name> and for that we need to change our cmdlet to list all mailboxes of that specific server and it can be easily done using the following cmdlet. Bear in mind that we cannot use static values in the script because we want the user to enter which servers he wants to retrieve information.

Get-Mailbox –Server <ServerName> | Select Name,ServerName, Database

Now, that we have the cmdlet tested we can start updating our script and use the IF statement. Basically, we are going to use the following logic: If the user enters –Server we are going to list by server, if he doesn’t we are going to list by individual, let’s review the previous section about IF statement and build our script. The deal here is that the first parameter can be either a mailbox or a server, so our first task is to evaluate what the end-user entered and it can be easily done by checking if the –server was entered using If ( $args[0] –eq ‘-server’) and if that statement is true we know that the second parameter is going to be the server name. Also, if the user hasn’t entered the server he probably entered the mailbox (individual) and then we will use the Else portion of the script, as shown in Figure 08.


Figure 08

Using the script above, we can start testing. Start by using .\mbxinfo.ps1 –Server POAEX01 (where POAEX01 is my Exchange Server) and the result will be all mailboxes of that server; secondly we can use .\mbxinfo.ps1 anderson.patricio and we have the expected results on both tries (Figure 09). The beauty of PowerShell is that we are not reinventing the wheel to bring the information. Just play with some parameters from existent cmdlets and in a really easy way we are able to get the results. From there we can take some actions and add intelligence to our scripts.


Figure 09

We are using static positioning here, first to exercise the If Statement. In the next article we will be looking at a more dynamic way to get similar results.

Conclusion

In this second article we saw some action where we have created a script from scratch. The first task was to list some mailbox information, then an entire server but we will continue working on that in the next article of this series. The idea behind this article series is to demonstrate how a script can be used to automate processes that you or your team have been doing manually and how you can start building up based on your requirements.

If you would like to read the other parts in this article series please go to:

    About The Author

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

    Scroll to Top