Monday, 18 September 2017

Create a PowerShell Module

Introduction

When you build up your own library of functions, it’s really useful to have them always available on your machine without you having to load script files etc. This is where creating your own PowerShell module comes in handy. You can have all your functions available in this module and just take it around wherever you go.

Today, I’m going to write a simple function and then create my own module which includes this function.

How to build a PowerShell module

So, this is really not as hard as it seems. The basic steps are below:

  1. Create your functions in a single .psm1 file
  2. Copy your .psm1 file into one of the PowerShell module folders
  3. Create a module manifest

1 - Create your functions and save the file as .psm1

I’ve created three simple functions below:

function Get-LoggedOnUser
    {
        ($env:USERDOMAIN).ToLower() + "\" + $env:USERNAME
    }

function List-Process 
    {
        Get-Process -IncludeUserName
    }

function List-MyProcesses
    {
        List-Process | ? {$_.UserName -eq (Get-LoggedOnUser)}
    }

List-MyProcesses

Get-LoggedOnUser just gets the user name. List-Process gets a list of all processes and List-MyProcesses filters the processes using the logged on user details.

We only want to have the function List-MyProcesses available to users who use the module and we’ll configure this when we create our module manifest in step 3.

2 - Copy your .psm1 file

You can store your psm1 file in one of three locations by default in Server 2016:

  • C:\Users\Administrator.contoso\Documents\WindowsPowerShell\Modules
  • C:\Program Files\WindowsPowerShell\Modules
  • C:\Windows\system32\WindowsPowerShell\v1.0\Modules

…..and you can confirm these are the locations by running the command below:

$env:PSModulePath -split ";"

image

We now need to make a folder for our module and give it a name. In this case, we will call our module ProcessTroubleshooting and we’ll save it in a module folder.

Make a new folder: C:\Program Files\WindowsPowerShell\Modules\ProcessTroubleshooting

Save your PowerShell file as C:\Program Files\WindowsPowerShell\Modules\ProcessTroubleshooting\ProcessTroubleshooting.psm1

Note the psm1 extension. This is the convention for creating modules.

3 - Create a module manifest

We now need a module manifest so that our module can be loaded automatically and so that PowerShell knows which functions to provide to the user. To create a module manifest, you need to use the New-ModuleManifest cmdlet. The module manifest file is a .psd1 file.

New-ModuleManifest -Path 'C:\Program Files\WindowsPowerShell\Modules\ProcessTroubleshooting\ProcessTroubleshooting.psd1' `
-RootModule 'C:\Program Files\WindowsPowerShell\Modules\ProcessTroubleshooting\ProcessTroubleshooting.psm1' `
-Author "Mark Gossa" `
-FunctionsToExport List-MyProcesses

We now have a module manifest which includes information about our module and very importantly which functions to export and allow the user to access. The FunctionsToExport parameter lists which functions we want to provide to users, i.e. which functions we want to export. It’s all nice and simple really.

You should now be able to import your module and use all the functions you’ve exported. In Server 2012 and later, you don’t need to even import it - Windows Server just imports it as you call one of the functions. See below for our function in action:

image

So, there we have it - that's how you create PowerShell modules but manual steps I hear you say? Well, you can just write your own function that will package up your module for you and create the manifest and you can even save that function in another module.

Happy scripting!

Monday, 4 September 2017

Learn PowerShell DSC - Part 4

Introduction

Sometimes you can’t find the DSC resource you need built into Windows. For example, let’s say you want to deploy a certificate from your CA - the DSC resource just doesn’t exist but then there’s GitHub. I’ll demo how you can search for and install the xCertificate DSC resource then how to use it in your configuration.

Other parts in this series:

Find DSC Resources

GitHub is a great repository for DSC resources which are not yet included in Windows. You can go to GitHub and download the additional DSC resources you need or you can just search for them using PowerShell:

Find-Module -Tag dsc

image

You can also filter by name e.g.

Find-Module -Tag dsc -Name *cert*

image

Our module is called xCertificate. Now, that was quite easy. Let’s move on.

Install DSC Resource

The DSC resource is actually part of a module which we need to install. To do this, we simply run the command below. It will prompt you to install a NuGet module if you don’t already have it - this is used to get modules from GitHub:

Install-Module xCertificate -Force

image

We can check our module is installed too:

Get-Module xCertificate -ListAvailable

image

You need to install the module on the server you’re writing your configuration on and also on the target server (the one that will receive the configuration). In upcoming articles, I’ll explain how to pull configurations using SMB or HTTPS. Using a pull server, you not only have a central repository for configurations but also have a central repository for the modules which are pulled as well. How good is that!?

To save time and install the module on a remote computer (contchidsc01), we can use remote PowerShell to install the NuGet Package Provider and xCertificate module:

icm contchidsc01 {Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
     Install-Module xCertificate -Force
     }

image

Deploy a certificate with DSC

Okay, so we now have all the pre-requisites we need. We have the DSC resource on our development machine and our target server. Let’s go ahead and write a DSC configuration. If you’re not familiar with this then go back and review part 1. You’ll see I’m using parameters in my configuration - if you’re not familiar with this, go and review part 3.

The plan is to deploy a new certificate to our target server, contchidsc01. The details are below:

  • Subject: contchidsc01.contoso.com
  • Subject alternative names: contchidsc01.contoso.com, contshidsc01
  • Exportable: true
  • Certificate Template: Server

configuration HTTPSPullServerCertificate
     {
         Param (
             [Parameter(Mandatory = $true)]
             [string] $ComputerName
         )

        # Modules must exist on target pull server
         Import-DSCResource -ModuleName PSDesiredStateConfiguration
         Import-DscResource -ModuleName xCertificate

        Node $ComputerName
             {
                 $ComputerFqdn = $ComputerName + ".contoso.com"

                xCertReq Certificate
                     {
                         Subject = $ComputerFqdn
                         SubjectAltName = "dns=$ComputerFqdn&dns=$ComputerName"
                         Exportable = $true
                         CertificateTemplate = "Server"
                     }
             }
     }

image

You’ll see we’re using our new xCertificate module and calling a new resource called xCertReq. The xCertReq DSC resource is what we need to request a certificate from the CA. There are other DSC resources available in the module and you can find all the documentation on GitHub here.

This line below simply gets the FQDN of the computer and saves it as $ComputerFqdn. We use it to add it to the certificate names:

$ComputerFqdn = $ComputerName + ".contoso.com”

Now, let’s create our MOF files:

HTTPSPullServerCertificate -ComputerName contchidsc01 -OutputPath C:\DSC\Certificate

…..and push our configuration:

Start-DscConfiguration -Path C:\DSC\Certificate -Verbose -Wait -Force

then test our configuration was deployed successfully:

Test-DscConfiguration -ComputerName contchidsc01 -Path C:\DSC\Certificate

The entire script is below:

configuration HTTPSPullServerCertificate
     {
         Param (
             [Parameter(Mandatory = $true)]
             [string] $ComputerName
         )

        # Modules must exist on target pull server
         Import-DSCResource -ModuleName PSDesiredStateConfiguration
         Import-DscResource -ModuleName xCertificate

        Node $ComputerName
             {
                 $ComputerFqdn = $ComputerName + ".contoso.com"

                xCertReq Certificate
                     {
                         Subject = $ComputerFqdn
                         SubjectAltName = "dns=$ComputerFqdn&dns=$ComputerName"
                         Exportable = $true
                         CertificateTemplate = "Server"
                     }
             }
     }

#Create MOF file
HTTPSPullServerCertificate -ComputerName contchidsc01 -OutputPath C:\DSC\Certificate
#Push the configuration
Start-DscConfiguration -Path C:\DSC\Certificate -Verbose -Wait -Force
#Test the configuration
Test-DscConfiguration -ComputerName contchidsc01 -Path C:\DSC\Certificate

image

Let’s go ahead and run it:

image

Now, that’s a lot of info because we’re using the -Verbose switch on the Start-DscConfiguration cmdlet. If you look closely at the blue section, you’ll see DSC doing this:

  • Locate the CA
  • Call certutil to ping the CA to check it’s online
  • Check for a certificate which matches the names we’ve requested
  • Create a certificate request
  • Install the certificate

At the end, in white, Test-DscConfiguration runs and states that our target machine is in the desired state so let’s check we have the correct certificate installed on our target machine:

Connect to remote PowerShell on the target computer:

Enter-PSSession contchidsc01

Open up the local computer certificate store:

cd Cert:\LocalMachine\My\

List the certificates:

dir

image

Now we have our certificate installed!

Conclusion

We’re learning quite a lot about DSC. We’re now able to find the DSC resources we need to do almost anything we need to do and we know how to find the documentation, download the modules and use the included DSC resources. So far, we’ve been pushing configurations using Start-DscConfiguration but in the upcoming posts, we’ll look at configuring SMB or HTTPS pull servers.

Sunday, 3 September 2017

Learn PowerShell DSC - Part 3

Introduction

So, we’ve learnt a bit about the basics of DSC with a simple configuration using the File DSC resource in part 1 and then moved on to discuss other DSC resources you can find built into Windows in part 2.

In this post, I’ll show you how you can integrate parameters when creating DSC configurations and also how you can set up dependencies between different DSC resources in your configuration.

Other parts in the series can be found below:

PowerShell DSC parameters

Why are we doing this? Well, it allows you to specify variables which are used to generate the configuration which is then pushed out to the target machines.

In this example which uses the Registry DSC resource to create registry values, I have four parameters that I want to pass to the configuration:

  • ComputerName - this determines which target machines the configuration will be pushed out to
  • Key - this is the registry key
  • ValueName - this is the name of the value
  • ValueData - this is the data that goes in the registry value
  • ValueType - this is the type of value, e.g. string, binary, dword

You specify the parameters as you would normally when writing a script or a function so this section looks like this:

param
     (
         [Parameter(Mandatory=$true)]
         [string]$ComputerName,
         [Parameter(Mandatory=$true)]
         [string]$Key,
         [Parameter(Mandatory=$true)]
         [string]$ValueName,
         [Parameter(Mandatory=$false)]
         [string]$ValueData,
         [Parameter(Mandatory=$false)]
         [string]$ValueType
     )
    

Nothing earth shattering here so stay with me. Let’s now make a configuration using the Registry DSC resource but instead of specifying the key, value and other parameters, we’ll use variables:

configuration RegistryConfig
{
     param
     (
         [Parameter(Mandatory=$true)]
         [string]$ComputerName,
         [Parameter(Mandatory=$true)]
         [string]$Key,
         [Parameter(Mandatory=$true)]
         [string]$ValueName,
         [Parameter(Mandatory=$false)]
         [string]$ValueData,
         [Parameter(Mandatory=$false)]
         [string]$ValueType
     )         

    Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node $ComputerName
     {       
         Registry CreateReg
         {
             Key = $Key
             ValueName = $ValueName
             ValueType = $ValueType
             ValueData = $ValueData
             Ensure = 'Present'
         }
     }
}

We can then generate the MOF file and specify what we need the key, value, value data and type to be and also which target machine to create the configuration for:

RegistryConfig -OutputPath C:\DSC\Registry -ComputerName contchidsc01 `
-Key HKEY_Local_Machine\Software\DSCTest -ValueName DSCTestGood -ValueData True -ValueType string

Now we have our MOF file, we can push it to our target machine contchidsc01:

Start-DscConfiguration -Computername contchidsc01 -Path C:\DSC\Registry -Wait -Verbose -Force

The full script is below:

configuration RegistryConfig
{
     param
     (
         [Parameter(Mandatory=$true)]
         [string]$ComputerName,
         [Parameter(Mandatory=$true)]
         [string]$Key,
         [Parameter(Mandatory=$true)]
         [string]$ValueName,
         [Parameter(Mandatory=$false)]
         [string]$ValueData,
         [Parameter(Mandatory=$false)]
         [string]$ValueType
     )         

    Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node $ComputerName
     {       
         Registry CreateReg
         {
             Key = $Key
             ValueName = $ValueName
             ValueType = $ValueType
             ValueData = $ValueData
             Ensure = 'Present'
         }
     }
}

RegistryConfig -OutputPath C:\DSC\Registry -ComputerName contchidsc01 `
-Key HKEY_Local_Machine\Software\DSCTest -ValueName DSCTestGood -ValueData True -ValueType string
Start-DscConfiguration -Computername contchidsc01 -Path C:\DSC\Registry -Wait -Verbose -Force

image

When we run this, we can see that the HKEY_Local_Machine\Software\DSCTest key is created then the DSCTestGood value is created and set to True and the type is set to REG_SZ, (string).

image

So, there we have it. PowerShell parameters. Now, let’s say you need to deploy a number of different registry keys and values to a number of machines and you have it all in a CSV. Well, now you can do that quite easily - all using the same configuration.

PowerShell DSC DependsOn

Let’s say we need to copy a zip file to a target machine and then extract that zip file to a folder. Now, clearly we don’t want DSC to try extract a zip file before it’s been copied over because that would be rather dumb. Here’s where we can specify the order that DSC Resources are processed in a configuration - by using the DependsOn parameter which is built into DSC resources.

We’ll use the File DSC resource to copy the zip file over then we’ll use the Archive DSC resource to extract it but we’ll specify that the Archive DSC resource depends on the File DSC resource:

configuration Archive
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node contchidsc01
     {       
         File CopyArchive
         {
             SourcePath = '\\contchisql01\Software\Docker.zip'
             DestinationPath = 'C:\Software\Docker.zip'
             Type = 'File'
             Ensure = 'Present'
         }

        Archive ExtractArchive
         {
             Path = 'C:\Software\Docker.zip'
             Destination = 'C:\Software\Docker'
             DependsOn = "[File]CopyArchive"
         }
     }
}

Archive -OutputPath C:\DSC\Archive
Start-DscConfiguration -Computername contchidsc01 -Path C:\DSC\Archive -Wait -Verbose -Force

image

When we run this configuration and push it out, we see that our zip file is copied then extracted:

image

We can also look at the output from Start-DscConfiguration and we’ll see that the File DSC resource is run before the Archive DSC resource:

image

So, now you can get your configuration to apply DSC resources in order and ensure dependencies are met.

Conclusion

That’s DSC working with parameters and dependencies. I hope you’re starting to get into PowerShell DSC as much as I am! Next up, we’ll look at how to find, install and use DSC modules that are not build into Windows.

Saturday, 2 September 2017

Learn PowerShell DSC - Part 2

Introduction

In part 1, we went through how to make a simple configuration using the File DSC resource and then how to push it out to a single computer. In this part, we’ll go through how to learn about the available DSC resources and demo a few of them.

To go to other parts of this series, see below:

List DSC Resources

Windows has a number of DSC resources built in and you can view the list by using Get-DscResource:

Get-DscResource -Module PSDesiredStateConfiguration

image

Get help for a DSC Resource

Now, let’s say you think you need to use the Service DSC resource but you don’t know the syntax, you can use Get-DscResource:

Get-DscResource Service -Syntax

image

You can also use Get-DscResource to get information about what properties you can set:

Get-DscResource Service | % Properties

image

Service DSC Resource Example

As we’ve just seen the syntax of how to use this DSC resource, let’s start with this.

My target machine has the Windows Audio service stopped and the service is set to manual start:

Get-Service Audiosrv | fl *

image

I want to set this to start automatically and to also start the service so the configuration I’d use is below. (Remember to import the correct module that has your DSC resource - in our case it’s the PSDesiredStateConfiguration module):

configuration Service
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node contchidsc01
     {
         Service WindowsAudio
         {
             Name = "audiosrv"
             StartupType = "Automatic"
             State = "Running"
         }
     }
}

image

Run your configuration - you should see no output from this.

As in part 1, we create a MOF file:

Service -OutputPath C:\DSC\Service

image

…….then push the configuration to our target machine contchidsc01:

Start-DscConfiguration -Computername contchidsc01 -Path C:\DSC\Service -Wait -Verbose -Force

image

……and then we confirm that our target machine matches the configuration:

Test-DscConfiguration -ComputerName contchidsc01 -Path C:\DSC\Service

image

We can also check the service to make sure it’s started and start up is automatic:

Get-Service Audiosrv | fl *

image

Archive DSC Resource Example

This allows you to extract a zip file from a location into a location on the target machine. See the example below:

configuration Archive
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node contchidsc01
     {       
         Archive Unzip
         {
             Destination = 'C:\Software\Docker'
             Path = '\\contchisql01\Software\Docker.zip'
             Checksum = 'SHA-256'
             Validate = $true
             Force = $true
             Ensure = 'Present'
         }
     }
}

Archive -OutputPath C:\DSC\Archive
Start-DscConfiguration -Computername contchidsc01 -Path C:\DSC\Archive -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:\DSC\Archive

image

Environment DSC Resource Example

This DSC resource adds environment variables. In this case, it creates a new variable called NewVar and gives it a value of ‘Value to store’.

configuration Environment
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node contchidsc01
     {       
         Environment NewVar
         {
             Name = 'MYNEWVAR'
             Value = 'Value to store'
             Ensure = 'Present'
         }
     }
}

Environment -OutputPath C:\DSC\Environment
Start-DscConfiguration -Computername contchidsc01 -Path C:\DSC\Environment -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:\DSC\Environment

image

Group DSC Resource Example

This DSC resource creates a local group and can set the members for it too. This example creates a group called TestGroup and makes Administrator a member of it.

configuration GroupConfig
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node contchidsc01
     {       
         Group TestGroup
         {
             Ensure = 'Present'
             GroupName = 'TestGroup'
             Description = 'This is a DSC test group'
             Members = 'administrator'
         }
     }
}

GroupConfig -OutputPath C:\DSC\Group
Start-DscConfiguration -Computername contchidsc01 -Path C:\DSC\Group -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:\DSC\Group

image

Process DSC Resource Example

This DSC resource starts or stops a particular process. You can also specify the arguments for the process too. This example ensures that notepad.exe is running.

configuration ProcessConfig
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node contchidsc01
     {       
         WindowsProcess MSPaint
         {
             Path = 'notepad.exe'
             Arguments = ''
         }
     }
}

ProcessConfig -OutputPath C:\DSC\Process
Start-DscConfiguration -Computername contchidsc01 -Path C:\DSC\Process -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:\DSC\Process

image

Registry DSC Resource Example

You can use this DSC resource to create, delete or set registry keys and values. This example creates a registry key 'HKEY_Local_Machine\Software\DSCTest' with the below value:

  • Name: DSCTestGood
  • Value: True
  • Type: REG_SZ (string)

configuration RegistryConfig
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration
     Node contchidsc01
     {       
         Registry CreateReg
         {
             Key = 'HKEY_Local_Machine\Software\DSCTest'
             ValueName = 'DSCTestGood'
             ValueType = 'string'
             ValueData = 'True'
         }
     }
}

RegistryConfig -OutputPath C:\DSC\Registry
Start-DscConfiguration -Computername contchidsc01
-Path C:\DSC\Registry -Wait -Verbose -Force
Test-DscConfiguration -ComputerName contchidsc01 -Path C:\DSC\Registry

image

Conclusion

As you can see, you can do a lot with PowerShell DSC and these are just a few of the built in DSC resources you can use. There’s a lot more on GitHub and we’ll come to this in part 4. Stay tuned for part 3 where we’ll start using parameters and a setting called DependsOn.

Monday, 28 August 2017

Learn PowerShell DSC - Part 1

Introduction

PowerShell DSC. What’s that? DSC stands for Dynamic State Configuration. It sounds like it could get a little complicated but if you’re just starting out using PowerShell to deploy configurations to machines then you’ll probably be lost in long scripts which check whether your configuration is correct then takes the steps to correct it.

To go to other parts of this series, see below:

You would have to write out the check statements and then write out all the logic to install the feature or copy the file etc.

With PowerShell DSC, you just state what you want your configuration to look like and DSC just “makes it so” and that’s the beauty of DSC.

image

Advantages of PowerShell DSC

As explained above, you need to write less code. That’s great but there’s more.

1) You can read through your code easier

This is called a configuration document. Rather than having a long script with if statements and lots of logic, you now just have a simpler script which states what you want the configuration to look like.

2) Look for configuration drift

DSC allows you to check a server against a configuration document to make sure there hasn’t been any changes.

3) Auto-remediation

DSC periodically checks a server’s configuration against the configuration document and then can be set to automatically restore the configuration to what it should be

4) Remote execution

You can deploy a configuration to remote machines and you can apply the same configuration to many machines

5) Centralised repository

You can configure your servers to pull their configuration from a central repository. This saves you having to copy scripts around and helps you manage versioning.

6) Works with workgroup servers

You’re not tied down to Kerberos authentication! You can deploy configurations to remote machines which are not on the domain.

7) Different teams can manage different configurations

This is called partial configurations. For example it allows your DBAs to manage the SQL related configuration while your systems team can manage the networking and your developers manage IIS.

8) One to many deployment

You can deploy the same configuration to more than one server at a time.

As you can see, DSC is really the best thing since sliced bread! It saves so much time and is a very elegant and efficient way to deploy configurations.

PowerShell DSC Example

Let’s take a look at a quick example. Let’s say we just want to make sure that an install file is copied to C:\Software on our server.

  • Server to configure: contlonsql01
  • Source file: \\contchisql01\Software\Installer.msi
  • Destination file: C:\Software\Installer.msi

Here’s what we’d write out to make this happen. Things to note:

  • configuration. This specifies that this is a PowerShell DSC configuration and the name is Configuration1.
  • Import-DscResource -ModuleName PSDesiredStateConfiguration. Perhaps you guessed it already but this imports the DSC resources from the DSC module. We’ll go into resources and modules another time so don’t worry about this line for now.
  • node is an array of servers we want to configure.
  • File. This specifies that we will be asking DSC to do a file or folder operation and we tell it what we want - i.e. we want to ensure that installer.msi is present which means it needs to be copied from the source location to the destination location.


image

configuration Configuration1 #Configuration1 is the name of the configuration
{
     Import-DscResource -ModuleName PSDesiredStateConfiguration #Imports the DSC module

    node ("contlonsql01") #List the servers you'll be targeting. It'll deploy the settings within node {}
     {
         File InstallerFile #File is a DSC Resource
         {
            Ensure = "Present"
            SourcePath = "\\contchisql01\Software\Installer.msi"
            DestinationPath = "C:\Software\Installer.msi"          
         }
     }
}

There is no output from this. It’s like when you define a function:


image

Push a DSC configuration

Now we need to push the configuration to the machine. To do this we need to create a Management Object File or MOF file and we simply run the configuration and specify an output path for the MOF file:

Configuration1 -OutputPath C:\DS

image

Let’s take a look at the contents of C:\DSC

image

Note that the MOF file is named after the node specified in the configuration. If you have specified more than one node then you’ll get one MOF file per node.
We then deploy the configuration by using Start-DscConfiguration

Start-DscConfiguration -Path C:\DSC

image

Note that you don’t really get much output here. You just see that a job’s been started. if you want to get the output of the job, you can run use Get-Job:

Get-Job 11

image

So, it’s completed and we can confirm our install.msi file has been copied over:

image

We can also use Test-DscConfiguration to check our target machine configuration against the configuration we deployed to it:

Test-DscConfiguration -Path C:\DSC

image

Conclusion

So, that’s just a quick intro into PowerShell DSC. It’s such a great way to configure your servers and definitely the way of the future. In the next post, I’ll talk through DSC resources - these define what types of configuration settings you can make e.g. file, windows features, registry changes etc. Click here for part 2 to continue the DSC journey.

Saturday, 26 August 2017

PowerShell functions with -Verbose and -Debug

Introduction

So, another rather standard day but made a little more interesting with some new tips on getting your PowerShell functions to provide some more feedback. We’re going to look at how to get your script to use the -Verbose and -Debug parameters.

Simple function

Okay, so our simple function is below. It just takes a Message parameter of and outputs Welcome <message> in green.

image

function Welcome
     {
         param(
             [string]$Message
             )

        Write-Host Welcome $message -ForegroundColor Green
     }

We can run the function as below:

Welcome -Message "to my blog!"

image

Nothing really that interesting here. Let’s move on.

Add -Verbose and -Debug to a PowerShell function

Here I’m adding another part to the script - [cmdletbinding()]. This automatically adds the -Verbose and -Debug parameters to the script and does all the logic behind it. By default, Write-Verbose doesn’t actually produce an output.

image

function Welcome
     {
         [cmdletbinding()] #This provides the function with the -Verbose and -Debug parameters
         param(
             [string]$Message
             )

        #Verbose - this is only shown if the -Verbose switch is used
         Write-Verbose -Message "This is verbose output"

        #Debug - this causes the script to halt at this point
         Write-Debug "This is debugging information"

        #Actual function
         Write-Host Welcome $message -ForegroundColor Green
     }

We can now run the function and specify the -Verbose parameter and we can see we’re now getting the output from Write-Verbose:

Welcome -Message "to my blog!" -Verbose

image

Let’s test out -Debug now. Debug is useful for when you’re debugging your script (who would have thought). Basically, it outputs Write-Debug, pauses the script and asks for you to either continue running it or to halt it. Write-Debug could contain more than just text. For example, if you want to check the value of a variable before it’s used, you can use Write-Debug to output the variable and then prompt you to either continue or halt the script.

Welcome -Message "to my blog!" -Debug

image

I selected Y and so it continued running the rest of the script:

image

So, there you have it. How to add -Verbose and -Debug into your functions.

Tuesday, 22 August 2017

VMware to Hyper-V migration with Veeam

Problem

Okay, so here’s the problem. You want to migrate VMs from VMware vSphere 5 or 6 or whatever to Hyper-V 2016 but you can’t find a tool to use because you want near-zero downtime. You search around and find some options:

Sysinternals Disk2Vhd

Great tool and free! The problem is that you need a lot of downtime because it converts the entire disk and you cannot sync changes after the conversion is done so you need downtime from the time you start the conversion.

Microsoft Virtual Machine Converter

Another good tool but no longer supports the later versions of vSphere or Hyper-V. In fact, the tool itself is not supported as of June 3rd 2017. See more here. However, even if you do use it, you still cannot do the incremental sync that you need.

Third party tools to migrate VMware to Hyper-V

There’s some very useful tools that you can use but these come at a cost but will literally do near-zero downtime conversions. Have a look at Quest, Double-Take or PlateSpin.

Solution

So, I found a neat little workaround for this. Basically, use Veeam. Now, you need both Veeam Agent for Windows and Veeam Backup and Replication (and you can get free trials for both). The steps are below:
  1. Install Veeam Agent for Windows on your VM
  2. Install Veeam Backup and Replication on a backup server
  3. Add your host into Veeam Backup and Replication
  4. Create a backup repository in Veeam Backup and Replication
  5. Back up your VM to a Veeam Repository on the backup server using Veeam Agent for Windows
  6. Prevent users accessing the server to make changes
  7. Do an incremental backup of your VM and shut it down
  8. Use Veeam Backup and Replication to rescan the repository
  9. Use Veeam Backup and Replication to do an Instant Restore of your VM onto a Hyper-V host and select to power on the VM
  10. Re-enable user access
  11. Use Veeam Backup and Replication to migrate the VM onto production storage (using the Instant Restore wizard)
The advantages of doing this are that you minimize downtime by doing incremental backups and then doing an instant restore. If you’re not familiar with this, Veeam Backup and Replication creates dummy VHD and VM configuration files on the Hyper-V storage which actually reference the backup server storage and the VM runs off the backup server storage. To improve performance, you may want to add faster disks and use 10Gb networking on your backup servers.
If you are a hosting provider then you really don’t want tenant VMs with access to the backup server so you can use the Veeam Cloud Connect Gateway (part of the Veeam Cloud Connect suite). This only requires a single port to be open from the tenant network - it’s generally used to back up VMs over the internet so it was designed with that security in mind.
I hope this helps people out as it looks like MS aren’t really providing a solution to do this just yet.

Saturday, 4 March 2017

Enable key archival in Server 2012 R2

Overview

So, you get an escalated call from the helpdesk saying someone’s lost their private key. So, we only had one copy of that. Now what?
Well, here’s where key archival comes into play. You configure your CA to enable key archival and then you specify that your certificate templates have key archival enabled and now your private keys are copied to your CA so you can recover them when needed!

How to enable key archival

Identify a user to serve as the key recovery agent. In this case, we'll use the account LITWARE\Administrator.

Open your Certification Authority snap-in, right click Certificate Templates and click Manage. You now see a list of certificate templates:

clip_image001[6]

Duplicate the Key Recovery Agent certificate template and give it a name: Key Recovery Agent 2

clip_image002[6]

Configure the key recovery agent certificate template with Read and Enroll permissions for the key recovery agent (LITWARE\Administrator). You do this on the Security tab:

clip_image003[6]

Now we need to configure the CA to use issue the new certificate template. Right click Certificate Templates, click New then click Certificate Templates to Issue

clip_image004[6]

Select your new Key Recovery Agent 2 certificate and click OK

clip_image005[6]

Now we need to enroll the Administrator account for the Key Recovery Agent 2 certificate. To do this, open up certmgr.msc and click on Personal

Click on Action > All Tasks > Request New Certificate

clip_image006[6]

Click next

clip_image007[6]

Click to select the Key Recovery Agent 2 certificate and then click Enroll to finish the wizard:

clip_image008[6]

clip_image009[6]

Note that it didn't issue the certificate - the status is Enrollment pending. Now, go back to your CA snap-in and click on Pending Requests. You should see a pending request for the certificate you just enrolled.

clip_image010[6]

Right click the certificate, click on All Tasks and then Issue. The certificate is now issued.
Now, right click the CA and go to Properties and select the Recovery Agents tab. Select Archive the key, select the Number of recovery agents to use (one in our case):

clip_image011[6]

Click Add and select the certificate which was issued to your chosen user:

clip_image012[6]

Click OK twice and you're then prompted to restart the AD CS services so go ahead and click Yes

clip_image013[6]

So, we've now created our Key Recovery Agent certificate template, issued it to our Key Recovery Agent and configured the CA to use a Key Recovery Agent. We're not protected against key loss just yet because the certificate templates that are issued out need to have key archival enabled.
Right click on a certificate template which you need to enable key archival for, duplicate it, give it a name, go to Properties and then to the Request Handling tab. Tick Archive subject's encryption private key:

clip_image014[6]

On the Superseded Templates tab, add all the certificate templates that you want to be replaced by your new one then click OK:

clip_image015[6]

This doesn't protect against loss of private keys for certificates which have already been issued so in this case, you need to get the clients to reenroll these. Right click on your original certificate and select Reenroll All Certificate Holders:

clip_image016[6]

Go for an 8hr coffee break or just sit and stare at the screen…….

Go to Issued Certificates in the CA snap-in and add the Archived Key column. Eventually, you should start to see new certificates issued and you can see that the key is archived:

clip_image017[6]

So, there you have it. That’s how you enable key archival in AD CS!

If you need to recover a key then see here.