Category Archives: Powershell

On my list I have a couple of PowerShell related subjects I want/need to learn more about. These include, but are not limited to parallelization, GitHub, DSC, PowerShell Core, JEA, Plaster, DBAtools and DBAchecks.

I have been playing around a bit already with parallelization and Github and the next thing on my list was Pester.

Pester in general

For those that do not know it, the Pester Wiki describes Pester as follows:
“Pester provides a framework for running Unit Tests to execute and validate PowerShell commands”.

So basically you use it to run scripts/functions/code with a specific input and tell Pester what the expected output should be. Then the result of the test either passes or fails. This way you can verify if your code works as expected.

Instead of using it to test your code, you can also use it to test if a configuration is the way it should be/you want it to be. By example if you’ve created an Operating System deployment or configured DSC, you can check if the configuration has been applied (correctly) or not.

Another advantage of having Pester tests is that you can easily re-run tests without spending much time.

Reasons to re-run these tests could be if you run the code under other circumstances like by example with PowerShell Core instead of Windows PowerShell or on Windows Server 2019 instead of Windows Server 2012 R2. A more common reason is when you change (parts of) your code that you can easily verify if your changes did not break anything. Keep in mind however that if you change your code, you might also need to change and/or add tests.

First, I started reading some articles about Pester, then I purchased ‘The Pester Book’ by Adam Bertram. I started reading the first couple of chapters, but didn’t finish it yet. It did provide me with the basics I needed to get started though. Instead of continuing to read the book, I decided to simply start using Pester with a simple real-life example and then build upon that.

The example I decided to start was a simple script I wrote in the past for a colleague to translate network addresses in the IP CIDR format to the IP subnet mask format. By example if the IP CIDR address was ‘192.168.1.1/24’ the output should be ‘192.168.1.1 255.255.255.0’. Basically it would take the input ‘192.168.1.1/24’ split it at the forward slash ( / ) and then use a switch statement to translate the CIDR notation (by example 24) to the subnet mask notation (by example 255.255.255.0).

To be honest I didn’t expect many advantages of using Pester for this specific script.

Actually getting started with Pester

Actually getting started with Pester consisted of the following steps that will be described in more detail:

Installing Pester

Creating a Pester tests file

Coming up with tests

Actually writing the PowerShell / Pester code for the tests you came up with

Running the tests

Analyzing the results, fixing code and re-running tests until all tests pass

to create a C:\Scripts\Convert-IPCIDRNotationtoIPSubnetMaskNotation.tests.ps1 file that is linked to the initial script and that will contain the Pester tests for my script. The linking looks as follows:

Sidenote: When using Test Driven Development (TDD) to create your code you first define your tests and then write your code. So if you create a new solution this way, you would also use New-Fixture and it would create both your empty script file and a linked Pester tests file.

3. Coming up with tests

Think about all possible scenarios and how you can test them. In my case the tests should at least test all possible input CIDR values ranging from 0 to 32 and if the subnet mask output would be what I expected.

4. Actually writing the PowerShell / Pester code for the tests you came up with

In this example in its simplest form you have a ‘Describe‘ block containing a single test or a logical group of tests contained in ‘It‘ statements where you define the code that should be run and what the output should be. By example you could have a logical group of tests for testing input validation and you could have a logical group of tests for testing if the output is as expected. In

Sidenote: In the example above I used testcases to define multiple inputs to test, but you could also use standard PowerShell code like getting input from a CSV and using a Foreach. This would look like this:

It“Ip and subnet mask result should be $($Test.ExpectedIpAndSubnetMask)“ {

$Result.IpAndSubNetMask |Should-Be $($Test.ExpectedIpAndSubnetMask)

}

}

}

5. Running the tests

Even though you can simply run the C:\Scripts\Convert-IPCIDRNotationtoIPSubnetMaskNotation.tests.ps1 file directly and run the tests, you should use Invoke-Pester because it gives your more options. In this simple example however, the differences aren’t that big but with Invoke-Pester it will show you how long it took to run all tests and how many tests had the result passed, failed, skipped, pending and inconclusive. So in this case you would run:

6. Analyzing the results, fixing code and re-running tests until all tests pass

Before I mentioned that I didn’t expect many advantages of using Pester for this specific script, but boy was I wrong. Apparently I made typos in the switch statement and without Pester I would have probably never noticed them. It could have caused major issues If this had been used in production for by example creating firewall rules.

By fixing issues and re-running the tests I finally got rid of the errors in my code. If you are interested in what mistakes I made and what I corrected, take a look at GitHub.

The code coverage of your tests basically describes to what degree your tests cover all possible scenarios. So if I had only tested only half of the options in the switch statement, my code coverage could never be 100%. For more info, see the Pester Wiki about code coverage.

The cool thing about Pester is that if you specify the -CodeCoverage parameter with Invoke-Item, it will create a code coverage report for you. So you would run by example:

Then it would tell you what percentage is covered by your tests and it even tells you what part(s) you not have tested. I have to admit that I don’t know exactly how it works and if it’s able to detect everything so you have to be critical yourself as well.

In my case I had defined the default keyword in the switch statement to return an error if the CIDR value was not between 0 and 32. I however forgot to write a test for it. Instead of writing a test for it, I decided in this case it would be better to modify the existing script to do input validation and then write tests to test if the input validation was working correctly. After this, the code coverage was 100%

Sidenote: You don’t always have to have 100% code coverage for a script from the start. Often you have limited time and it is not feasible to write tests for scenarios that are unlikely to ever happen. This is something you have to decide yourself. You can always add/improve tests later on and each test you have created is better than having none at all.

Closing thoughts

Pester is a great tool that helps to improve your code, even for simple code. We are all human and we all make mistakes. Pester helps me finding these issues and therefore improves my code quality. It also gives me a better feeling and more confidence about the scripts I’ve written.

What I also really like, is that making changes to an existing scripts takes less time and is less likely to create issues that will not be noticed. This is because you can easily re-run the tests that you have already created. You should still keep in mind that changes to your code might require changes to existing tests or might require you to add tests. But using the -codecoverage parameter of Invoke-Pester also helps you with that.

I purposely chose this simple example just to get started, but Pester can do much more. I plan to read more about it in ‘The Pester Book’ by Adam Bertram and when I get a good use case I will blog about it.

If you have any thoughts about this article, please leave a reply as this motivates me to create more blog posts.

I hope that they will add me so that it will also reach a bigger audience and other people can benefit from my blog posts. I also hope that I can benefit from more people in the community giving me pointers and advice to improve. I look forward to seeing the results 🙂

Thanks a lot to all people that make Planet PowerShell possible and also those who contribute to it with their blog posts. Without you I wouldn’t have been where I am now.

As a PowerShell person I’m personally not a big fan of creating a GUI for PowerShell scripts. This is however something that is asked a lot when PowerShell scripts are created for by example provisioning new users.

I’ve seen many different approaches to this. Most worked around it either by using Read-Host or creating a GUI inside their function. In some cases these approaches also meant that the function could not handle pipeline input, multiple inputs, etc. So basically they automated one thing, but prevented further automation by implementing the GUI in a stupid way.

Personally I prefer to keep my functions without a GUI. If it is desired or required, the GUI code will be written outside of the function and will result in the calling of the function with the values specified in the GUI. This way the GUI part will not pollute the function and users have more choice and flexibility to achieve their goal. You could compare it to how you normally handle output. You output objects and let users handle the formatting instead of providing them with by example the output in Format-Table.

Now the approach is clear, the GUI still needs to be created. There are multiple ways to go about this. You could by example create the GUI by looking up what exactly you need to code to get the desired result or you could use (paid) 3rd party tools / services. You could by example use the free web-based service POSHGUI to visually create your form and then have it output your needed code. There are however also (paid) editors like Visual Studio or PowerShell Studio. Another very simple method in some cases is using Out-Gridview with -PassThru. For more info, see also my previous blog post Summary of 2nd Dutch PowerShell User Group – DuPSUG meeting with additional resources

In my case I created my initial GUI form with POSHGUI. Even though you can do many more advanced GUI forms, I soon realized that I was always creating the same type of GUI form that consisted of a couple of text boxes and a submit button. Creating these manually become an annoyance very quickly. That’s why I decided to create a function to dynamically create such a GUI based on my input.

In the past years I’ve been creating scripts and storing them on my personal OneDrive that is connected to multiple desktops and laptops I use. Additionally, my Synology NAS syncs with OneDrive and backups the files to ChrashPlan.

Even though this works, it has some disadvantages:

OneDrive provides basic version history, but it is limited to just the file at a point in time. When modifying scripts I would need to create a new version and file to ensure I would be able to go back to a previously working version. In the past, especially when I just got started with PowerShell I screwed up code that was working before and that cost me a lot of time to get working again.

The file is only available to me and the people I explicitly share it with. Therefore a lot of general use scripts I’ve created will not be findable and usable by others and I cannot benefit from suggestions/improvements from others which would also help me create better code.

These are the things I plan to solve by using GitHub with Visual Studio Code.

Since we use a proxy at work, I quickly figured this would be the most probable cause. So first I determined the proxy server by checking our WPAD file using http://wpad.domain.local/wpad.dat. Then I went on to determine how to configure Visual Studio Code to use the proxy server and came across the articles “Network Connections in Visual Studio Code” and “User and Workspace Settings” , but both did not solve my problem even after restarting Visual Studio Code. Then it hit me that Git For Windows was being used to connect to GitHub. I then quickly found out that I had to run GIT Cmd and configure the proxy using: git config –global http.proxy http://proxyuser:proxypwd@proxy.server.com:8080 and after restarting Visual Studio Code it worked correctly again. When I’m working from home without VPN I simply unset the proxy configuration using GIT Cmd using: git config –global –unset http.proxy and restart Visual Studio Code.

I was able to succesfully logon to https://login.microsoftonline.com/ so the credentials were correct.
Also I had not enabled Multi-factor authentication because I knew this could be a problem.
I tried clearing credentials / cookies in browsers and credential manager.

Since this did not solve the issue, I started to search online for answers. The first result was : https://support.microsoft.com/en-gb/kb/2905767 which suggested I entered the wrong username/password or that I didn’t have the correct permissions (organization administrator).