• No results found

The Package resource is used to install and uninstall setup.exe and Windows Installer packages on target nodes.

We cannot use it for installing Windows Update packages. Table 5-7 provides a list of properties associated with this resource and a brief description of each property.

Table 5-7. Properties Associated with the Package DSC Resource

Property Name Description Required?

Name Specifies the name of the package that has to configured Yes

ProductId Specifies a product ID that uniquely identifies the product being configured Yes Arguments Specifies a list of arguments required to configure the product No Credential Specifies the credentials required to access the network share containing

package installer

No

DependsOn Specifies the resource(s) that must be configured before configuring the group resource

No

Ensure Specifies whether the package needs to be present or absent No LogPath Specifies the full path of a log file to which install or uninstall information must be

written to

No

Path Specifies the full path to the package installer Yes

ReturnCode Specifies the expected return code from a successful install No

To understand how the Package resource can be used, we should look at a few examples. To start with, let us try to install a product that uses Windows Installer or MSI technology. I chose to install the 7-Zip package on my target node for this demo. You can download the 7-Zip MSI package from 7-zip.org. Once you have the package, copy it to the remote node at C:\Package folder. If you look at Table 5-7, the ProductId property is a required property. This is, in reality, a Globally Unique Identifier (GUID) associated with the product. Using any random GUID will result in a failure. The product code must be derived from the MSI package and requires Win32 API access. So, to make things easy, I will use a PowerShell module called Windows Installer PowerShell module. This has been developed by Heath Stewart at Microsoft and can be downloaded at psmsi.codeplex.com.

Once you have the module downloaded and installed, the Get-MSITable cmdlet can be used to retrieve the ProductID value.

Get-MSITable -Path .\7z920-x64.msi -Table Property | Where-Object { $_.Property -eq " ProductCode" }

We now have the product code for the 7-Zip MSI package. So, the configuration script for deploying this package on a target node can be written.

Configuration PackageDemo { Node WSR2-1 {

Package PackageDemo { Name = "7-Zip"

Path = "C:\Package\7z920-x64.msi"

ProductId = "23170F69-40C1-2702-0920-000001000000"

} } }

PackageDemo

The value of the Name property can be anything and does not have to match the package name while installing a package. The Path property is required and points to the full path of the MSI package. We have already discussed the ProductId property and understand its significance. When we apply this configuration, the MSI package gets installed on the target node, and the result of that configuration change is shown in Figure 5-15.

Figure 5-15. Package installation using DSC

What happens when we try to install an MSI package is simple. The Package resource verifies whether the package is locally available on the target node. If not, and the package is at a UNC path, a PS drive is mapped, using the credentials provided by the Credential property. Once this process is complete, the Package resource sets up the command line for installing the MSI package. This includes the full path to MSIEXEC.exe and the MSI package name with /i and /quiet command-line arguments.

You can also use an HTTP-based URL as the path to the MSI package. For example, it is completely valid to specify http://downloads.sourceforge.net/sevenzip/7z920-x64.msi as the value of Path property. In this case, the package resource will attempt to download a package to the target node, before starting the installation. If you have a target node connected to the Internet, try the preceding example with the URL just mentioned.

The next example in our discussion is about uninstalling the package (see Figure 5-16). The configuration script will be similar to that we have seen in the earlier example, except that the Ensure property will be used to specify that the package must be uninstalled.

Configuration PackageDemo { Node WSR2-1 {

Package PackageDemo { Name = "Test"

ProductId = "23170F69-40C1-2702-0920-000001000000"

Path = "C:\Package\7z920-x64.msi"

Ensure = "Absent"

} } }

PackageDemo

Figure 5-16. Uninstalling a package using DSC

There is a gotcha here. When uninstalling a package, the ProductId need not be specified. The property should still be present in the configuration script, but it can be an empty string value. In this case, you have to specify the value of the Name property as it appears in Programs and Features in the control panel. For example, the following configuration, when applied, will result in a message that there is no package with the name “Test” installed on the target node (see Figure 5-17).

Configuration PackageDemo { Node WSR2-1 {

Package PackageDemo { Name = "Test"

ProductId = ""

Path = "C:\Package\7z920-x64.msi"

Ensure = "Absent"

} } }

PackageDemo

Figure 5-17. Configuration failure with an invalid package name

To fix this, we have to use the same name for the package as in Programs and Feature under Control Panel. For the 7-Zip package we installed, the name of the package is 7-Zip 9.20 (x64 Edition).

Note