次の方法で共有


Getting IDs to use with the Package DSC resource

One of the questions I get from customers who are looking at using the Package DSC resource to install MSI's and executables is "What is the ProductID for the installer I want to run?". If you look at the documentation the ProductID is meant to be a unique identified for the product you are installing, but how can you figure out what the correct value for any given installation is? Lucky for us you can actually use a little bit of PowerShell to look this up after you manually install the product once. Let me give you an example, I recently had to write a DSC script which would install SQL Server Management Studio 17.1 with DSC, so here is the approach I took.

Step one: Manually install the product somewhere

This is pretty straightforward - but we need to manually install it once so that we can look up the product ID from the registry after the fact. We can uninstall it after we are done if you like - I tend to provision a quick virtual machine in Azure for this to get the ID and then I can delete the VM after I'm done with it.

Step two: Find the ID with PowerShell

Since all the IDs of products that are installed on a machine are stored in the registry, it's actually quite straightforward to interrogate the registry to get a list of products with their corresponding ID is. Here is the script I use to output the list of installed products:

 $x86Path = "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
$installedItemsX86 = Get-ItemProperty -Path $x86Path | Select-Object -Property DisplayName, PSChildName
    
$x64Path = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
$installedItemsX64 = Get-ItemProperty -Path $x64Path | Select-Object -Property DisplayName, PSChildName

$installedItems = $installedItemsX86 + $installedItemsX64
$installedItems | Where-Object -FilterScript { $null -ne $_.DisplayName } | Sort-Object -Property DisplayName | Out-GridView

This will output you a GridView in PowerShell that looks a little like this

And once we have it in a GridView we can actually add filters pretty easily as well

Now we can see what the GUID is for SQL Management Studio 17.1 and I can now use this to make a DSC resource

Step three: Create the DSC resource

Now that I have the ID I can craft my resource

 Package InstallSSMS
{
    Ensure = "Present"
    Name = "SSMS-Setup-ENU"
    Path = "C:\Software\ssms2016\SSMS-Setup-ENU.exe"
    Arguments = "/install /passive /norestart"
    ProductId = "b636c6f4-2183-4b76-b5a0-c8d6422df9f4"
}

Here you can see I've used the ID we retrieved in step 2 as the ProductId attribute, and that I'm also passing arguments to make sure the installer won't need any user input (remember, DSC runs in the background so just running the installer on it's own without those arguments won't help).

 

But that's all there is to it - a nice simple script to help you figure out the ProductID values to use in your package resource. Happy DSC'ing!