Install a cab file with the given name from the given path

Description

This example shows how you can use the WindowsPackageCab resource user-provided values to ensure a package is installed.

You must specify the name of the package with the Name parameter, which sets the Name property of the resource.

You must specify the path to the .cab file the package can be installed from with the SourcePath parameter, which sets the SourcePath property of the resource.

You must specify the path to a log file with the LogPath parameter, which sets the LogPath property of the resource.

With Ensure set to Present, the Name property set to the user-provided value from the Name parameter, and SourcePath set to the user-provided value from the SourcePath parameter, the resource installs the named package from the specified .cab file if it isn't already installed.

With LogPath set to the user-provided value from the LogPath parameter, the resource writes the logs for installing the package to that file instead of %WINDIR%\Logs\Dism\dism.log.

With Invoke-DscResource

This script shows how you can use the WindowsPackageCab resource with the Invoke-DscResource cmdlet to ensure a user-specified package is installed.

[CmdletBinding()]
param(
    [Parameter (Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [String]
    $Name,

    [Parameter (Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [String]
    $SourcePath,

    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [String]
    $LogPath
)

begin {
    $SharedParameters = @{
        Name       = 'WindowsPackageCab'
        ModuleName = 'PSDscResource'
        Properties = @{
            Name       = $Name
            Ensure     = 'Present'
            SourcePath = $SourcePath
            LogPath    = $LogPath
        }
    }

    $NonGetProperties = @(
        'Ensure'
        'SourcePath'
        'LogPath'
    )
}

process {
    $TestResult = Invoke-DscResource -Method Test @SharedParameters

    if ($TestResult.InDesiredState) {
        $QueryParameters = $SharedParameters.Clone()

        foreach ($Property in $NonGetProperties) {
            $QueryParameters.Properties.Remove($Property)
        }

        Invoke-DscResource -Method Get @QueryParameters
    } else {
        Invoke-DscResource -Method Set @SharedParameters
    }
}

With a Configuration

This snippet shows how you can define a Configuration with a WindowsPackageCab resource block to ensure a user-specified package is installed.

Important

There's a limitation in machine configuration that prevents a DSC Resource from using any PowerShell cmdlets not included in PowerShell itself or in a module on the PowerShell Gallery. This example is provided for demonstrative purposes, but because the DSC Resource uses cmdlets from the DISM module, which ships as one of the Windows modules, it won't work in machine configuration.

Configuration Install {
    param(
        [Parameter (Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Name,

        [Parameter (Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $SourcePath,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $LogPath
    )

    Import-DscResource -ModuleName 'PSDscResources'

    Node Localhost {
        WindowsPackageCab ExampleWindowsPackageCab {
            Name       = $Name
            Ensure     = 'Present'
            SourcePath = $SourcePath
            LogPath    = $LogPath
        }
    }
}