Use PowerShell to Download and Install DSC Resource Kit


Summary: Microsoft Scripting Guy, Ed Wilson, writes a Windows PowerShell script to download and install the Desired State Configuration Resource Kit.

Microsoft Scripting Guy, Ed Wilson, is here. Today I want to talk a little bit about downloading and installing the latest wave of the Windows PowerShell Desired State Configuration (DSC) Resource Kit. It is pretty simple. I go to the Script Center Repository, search for DSC Resource Kit, click the link for the latest build, and download it to my local computer. The associated page is shown here:

Image of webpage

I then have to extract the .zip files, copy the files to the Program Files > Windows PowerShell > Modules folder—and groovy, everything is done. But dude, that is a lot of clicking around. And this blog is about automating stuff, not about clicking stuff.

So I decided I would write a Windows PowerShell script to copy the files from the Script Center Repository to a temp folder, and then extract and copy the files to the Program Files > Windows PowerShell > Modules folder. Keep in mind that the file name changes between waves of the Windows PowerShell DSC Resource Kit, so the download line is one that you need to change between versions.

To extract and to copy the files into the modules location on my laptop requires Admin rights, so I use #Requires -RunAsAdministrator. This is a feature in Windows PowerShell 4.0, and because DSC is also a feature in Windows PowerShell 4.0 there is not a problem using this. In the past, I used my Test-ISAdministrator function, which I keep in my Windows PowerShell ISE profile. But I do not use that anymore. Here is the appropriate line of code:

#Requires -RunAsAdministrator

The next line is the source path. It is long and ugly, but it was easy to get. All I had to do was go to the  Script Center Repository page that I showed previously, right-click the name of the .zip file, and select Copy shortcut from the action menu. This gives me the following path to the .zip file:

$source = "http://gallery.technet.microsoft.com/scriptcenter/DSC-Resource-Kit-All-c449312d/file/126120/1/DSC%20Resource%20Kit%20Wave%207%2009292014.zip"

I would have used a shorter URL for the line, but because it will change with every release of the DSC Resource Kit, I decided not to do that. Keep in mind, it is a single line.

I decided to use the temp folder for the download location. There are lots of ways to get the temp folder location. I decided to use the $env:Temp method because I was going to use $env:ProgramFiles a little later, and I like to keep the same methodology in my scripts. The temp folder is always available, so it is an easy choice. I use the Join-Path cmdlet to build my location, and I append the DSCResKit.zip for my file name. This is shown here:

$destination = Join-Path -Path $env:TEMP -ChildPath "DSCResKit.zip"

The installation directory (according to the notes) needs to be the \WindowsPowerShell\Modules directory, so I have that hardcoded with the path to the environmental Program Files directory. Here is that line of code:

$dscHome = "$env:ProgramFiles\WindowsPowerShell\Modules"

Now I create a webclient object, and I call the DownloadFile method. This method takes two options: the source and the destination. Here are those lines of code:

$wc = New-Object system.net.webclient

$wc.downloadFile($source,$destination)

At this point, I have the DSC Resourse Kit downloaded as a .zip file to my temp directory. Now, I need to extract the files, and copy them to the installation location. There are a lot of ways to do this, but perhaps the easiest is to use the Shell.Application object—at least it is easy for me because I have been using this for nearly one and a half decades (seriously).

So I create the Shell.Application object, then I call the NameSpace object and pass it a folder location. This is a compressed folder, our .zip file. I then use the Items method to retrieve all of the items in the destination. Here are those lines of code:

$shell = New-Object -ComObject shell.application

$files = $shell.namespace($destination).items()

I use the NameSpace object again, and this time I connect to the installation location. I use the CopyHere method to copy all of the files from the .zip file. This extracts them. Here is the line of code:

$shell.NameSpace($dscHome).copyHere($files)

If the folders exist, a dialog box will appear and prompt you to replace it. I say Yes. I could use Test-Path to cycle through everything and delete any folders that existed prior to the operation, but this is easier for me.

I clean up by removing the downloaded .zip file from the temp folder. This is one of my pet peeves—I hate applications that leave stuff lying around in the temp folder.

Remove-Item $destination

At this point, I can use Get-DSCResource to ensure that all of my files were copied correctly:

Get-DscResource

Here is the complete script:

# DownloadAndInstallDSCResKit.ps1

#  ed wilson, msft

# Requires Admin Rights

# HSG-10-13-2014

# ————————————————————————————————-

#Requires -RunAsAdministrator

$source = "http://gallery.technet.microsoft.com/scriptcenter/DSC-Resource-Kit-All-c449312d/file/126120/1/DSC%20Resource%20Kit%20Wave%207%2009292014.zip"

$destination = Join-Path -Path $env:TEMP -ChildPath "DSCResKit.zip"

$dscHome = "$env:ProgramFiles\WindowsPowerShell\Modules"

$wc = New-Object system.net.webclient

$wc.downloadFile($source,$destination)

$shell = New-Object -ComObject shell.application

$files = $shell.namespace($destination).items()

$shell.NameSpace($dscHome).copyHere($files)

Remove-Item $destination

That is all there is to using Windows PowerShell to download and to install the files for the DSC Resource Kit. DSC Resource Kit Week will continue tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Comments (4)

  1. Anonymous says:

    Thanks for a very useful article. Is there a way to execute this script to install the resource kit locally from a DSC .ps1 file before it attempts to import any of the resource kit modules? When I execute the .ps1 file, I get errors on my Import-DSCResource
    lines of the script before the resource kit can be installed. Thanks!

  2. I will dig deeper into this DCS, seems to have some good stuff.

  3. Derek M says:

    This didn’t work. Threw the error "You cannot call a method on a null-valued expression" for the line $shell.NameSpace($dscHome).copyHere($files)

    I needed to create the destination directory first then it worked like a charm.

Skip to main content