PART III
BUILDING YOUR OWN MODULE

By now, you should have a firm grasp on what makes PowerShell PowerShell. We’ve covered the syntax of the language, as well as a few specific modules you may use in your day-to-day automation work. But up until the preceding chapter, we’ve been doing things only in pieces: a little syntax here, a little syntax there, nothing major. In Chapter 14, with the server inventory script, you got your first taste of working on a prolonged PowerShell project. In Part III, we’re going to go bigger: you’re going to build your own PowerShell module.

PowerLab

PowerLab is a single PowerShell module that contains the functions you need to provision Windows servers from scratch. You’ll build PowerLab brick by brick; if you want to see the final result, you can find it in this GitHub repository: https://github.com/adbertram/PowerLab.

The process of provisioning a Windows server from scratch will look something like this:

  • Create a virtual machine.

  • Install a Windows operating system.

  • Install a server service (Active Directory, SQL Server, or IIS).

This means you’ll need your PowerLab module to do five things:

  • Create Hyper-V virtual machines

  • Install a Windows server

  • Create an Active Directory forest

  • Provision SQL servers

  • Provision IIS web servers

To accomplish these tasks, you’ll use three primary commands:

  • New-PowerLabActiveDirectoryForest

  • New-PowerLabSqlServer

  • New-PowerLabWebServer

Of course, you’re going to use more than three commands. You’ll build each of these commands with multiple helper commands that will take care of behind-the-scenes functionality, including creating the virtual machine and installing the operating system. But we’ll go through all of that in the chapters ahead.

Prerequisites

You’ll need a few things to build PowerLab:

  • A Windows 10 Professional client computer in a workgroup. A Windows 10 machine joined to a domain may work but was not tested.

  • A Hyper-V host in a workgroup running Windows Server 2012 R2 (at least) on the same network as the client—although the host could be joined to a domain as well, but this scenario was not tested.

  • ISO files for Windows Server 2016, located on your Hyper-V host. Windows Server 2019 was not tested. You can download evaluation versions of Windows Server from https://www.microsoft.com/en-us/evalcenter/evaluate-windows-server-2016?filetype=ISO.

  • Remote Server Administration Tools (RSAT) enabled on the client computer (download from https://www.microsoft.com/en-us/download/details.aspx?id=45520).

  • The latest version of the Pester PowerShell module installed on your client computer.

You also need to be logged in as a member of the local administrators group on the client computer, and have the PowerShell execution policy set to unrestricted. (You can run Set-ExecutionPolicy Unrestricted to change the execution policy, but I recommend changing this back to AllSigned or RemoteSigned when the lab setup is complete.)

Setting Up PowerLab

When providing something like PowerLab to consumers, you want to make setup as painless as possible. One way to do this is by providing a script that handles the installation and configuration of your module with minimal user input.

I’ve already written the installation script for PowerLab. It can be found in the PowerLab GitHub repository: https://raw.githubusercontent.com/adbertram/PowerLab/master/Install-PowerLab.ps1. That link will provide the raw source code for the script. You could copy and paste it into a new text file and save it as Install-PowerLab.ps1, but this is a PowerShell book, so let’s try running the following command:

PS> Invoke-WebRequest -Uri 'http://bit.ly/powerlabinstaller' -OutFile 'C:Install-PowerLab.ps1'

Be warned: when you run the script, you’ll have to answer some questions. You’ll need the hostname of the Hyper-V host, the IP address of the Hyper-V host, the local administrator username and password for the Hyper-V host, and the product keys (if not using a Windows Server evaluation copy) for each operating system you want to install.

Once you have all the information on hand, run the install script with the following command:

PS> C:Install-PowerLab.ps1

Name of your HYPERV host: HYPERVSRV
IP address of your HYPERV host: 192.168.0.200
Enabling PS remoting on local computer...
Adding server to trusted computers...
PS remoting is already enabled on [HYPERVSRV]
Setting firewall rules on Hyper-V host...
Adding the ANONYMOUS LOGON user to the local machine and host server
Distributed COM Users group for Hyper-V manager
Enabling applicable firewall rules on local machine...
Adding saved credential on local computer for Hyper-V host...
Ensure all values in the PowerLab configuration file are valid and close the
ISE when complete.
Enabling the Microsoft-Hyper-V-Tools-All features...
Lab setup is now complete.

If you’d like to inspect what this script does, you can always download it via the book’s resources and check it out. However, know that it’s meant to get us both to the same infrastructure, not necessarily show you what the script is doing; it may be over your head at this time. This script is meant to enable you to follow along with me.

Demo Code

All the code you will write in the following chapters can be found at https://github.com/adbertram/PowerShellForSysadmins/tree/master/Part%20III. In addition to all the PowerLab code, you’ll find necessary data files and the Pester scripts to test the module and verify that your environment meets all the expected prerequisites. Before starting each chapter, I strongly suggest that you use the Invoke-Pester command to run the Prerequisites.Tests.ps1 Pester script found in each chapter’s files. Doing so will save you from many headache-inducing bugs down the line.

Summary

You should have everything you need to start building PowerLab. We’ll cover a lot of ground in the following chapters, and draw on many areas of PowerShell, so don’t be surprised if you see something you don’t recognize. Plenty of online resources can help you through thorny syntax, and if you don’t understand something, you can always reach out to me on Twitter at @adbertram or reach out to others on the internet.

With that, let’s get started!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.20.224.107