Skip to content

Commit

Permalink
Add Windows Package Cab
Browse files Browse the repository at this point in the history
  • Loading branch information
kwirkykat committed Oct 8, 2016
1 parent a87c3b2 commit 4f1faae
Show file tree
Hide file tree
Showing 9 changed files with 644 additions and 1 deletion.
225 changes: 225 additions & 0 deletions DSCResources/MSFT_xWindowsPackageCab/MSFT_xWindowsPackageCab.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
Import-Module -Name (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'CommonResourceHelper.psm1')
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_xWindowsPackageCab'

Import-Module -Name 'Dism'

<#
.SYNOPSIS
Retrieves the current state of a package from a windows cabinet (cab) file.
.PARAMETER Name
The name of the package to retrieve the state of.
.PARAMETER Ensure
Not used in Get-TargetResource.
Provided here to follow DSC design convention of including all mandatory parameters
in Get, Set, and Test.
.PARAMETER SourcePath
The path to the cab file the package should be installed or uninstalled from.
Returned from Get-TargetResource as it is passed in.
.PARAMETER LogPath
The path to a file to log this operation to.
There is no default value, but if not set, the log will appear at %WINDIR%\Logs\Dism\dism.log.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
$Name,

[Parameter(Mandatory = $true)]
[ValidateSet('Present', 'Absent')]
[String]
$Ensure,

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

[ValidateNotNullOrEmpty()]
[String]
$LogPath
)

$windowsPackageCab = @{
Name = $Name
Ensure = 'Present'
SourcePath = $SourcePath
LogPath = $LogPath
}

$getWindowsPackageParams = @{
PackageName = $Name
Online = $true
}

if ($PSBoundParameters.ContainsKey('LogPath'))
{
$getWindowsPackageParams['LogPath'] = $LogPath
}

Write-Verbose -Message ($script:localizedData.RetrievingPackage -f $Name)

try
{
$windowsPackageInfo = Dism\Get-WindowsPackage @getWindowsPackageParams
}
catch
{
$windowsPackageInfo = $null
}

if ($null -eq $windowsPackageInfo -or -not ($windowsPackageInfo.PackageState -in @( 'Installed', 'InstallPending' )))
{
$windowsPackageCab.Ensure = 'Absent'
}

Write-Verbose -Message ($script:localizedData.PackageEnsureState -f $Name, $windowsPackageCab.Ensure)

return $windowsPackageCab
}

<#
.SYNOPSIS
Installs or uninstalls a package from a windows cabinet (cab) file.
.PARAMETER Name
The name of the package to install or uninstall.
.PARAMETER Ensure
Specifies whether the package should be installed or uninstalled.
To install the package, set this property to Present.
To uninstall the package, set the property to Absent.
.PARAMETER SourcePath
The path to the cab file to install or uninstall the package from.
.PARAMETER LogPath
The path to a file to log this operation to.
There is no default value, but if not set, the log will appear at %WINDIR%\Logs\Dism\dism.log.
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
$Name,

[Parameter(Mandatory = $true)]
[ValidateSet('Present', 'Absent')]
[String]
$Ensure,

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

[ValidateNotNullOrEmpty()]
[String]
$LogPath
)

Write-Verbose -Message ($script:localizedData.SetTargetResourceStarting -f $Name)

if (-not (Test-Path -Path $SourcePath))
{
New-InvalidArgumentException -ArgumentName 'SourcePath' -Message ($script:localizedData.SourcePathDoesNotExist -f $SourcePath)
}

if ($Ensure -ieq 'Present')
{
Write-Verbose -Message ($script:localizedData.AddingPackage -f $SourcePath)
Dism\Add-WindowsPackage -PackagePath $SourcePath -LogPath $LogPath -Online
}
else
{
Write-Verbose -Message ($script:localizedData.RemovingPackage -f $SourcePath)
Dism\Remove-WindowsPackage -PackagePath $SourcePath -LogPath $LogPath -Online
}

Write-Verbose -Message ($script:localizedData.SetTargetResourceFinished -f $Name)
}

<#
.SYNOPSIS
Tests whether a package in a windows cabinet (cab) file is installed or uninstalled.
.PARAMETER Name
The name of the cab package to test for installation.
.PARAMETER Ensure
Specifies whether to test if the package is installed or uninstalled.
To test if the package is installed, set this property to Present.
To test if the package is uninstalled, set the property to Absent.
.PARAMETER SourcePath
Not used in Test-TargetResource.
.PARAMETER LogPath
The path to a file to log this operation to.
There is no default value, but if not set, the log will appear at %WINDIR%\Logs\Dism\dism.log.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([Boolean])]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
$Name,

[Parameter(Mandatory = $true)]
[ValidateSet('Present', 'Absent')]
[String]
$Ensure,

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

[ValidateNotNullOrEmpty()]
[String]
$LogPath
)

$getTargetResourceParams = @{
Name = $Name
Ensure = $Ensure
SourcePath = $SourcePath
}

if ($PSBoundParameters.ContainsKey('LogPath'))
{
$getTargetResourceParams['LogPath'] = $LogPath
}

$windowsPackageCab = Get-TargetResource @getTargetResourceParams

if ($windowsPackageCab.Ensure -ieq $Ensure)
{
Write-Verbose -Message ($script:localizedData.EnsureStatesMatch -f $Name)
return $true
}
else
{
Write-Verbose -Message ($script:localizedData.EnsureStatesDoNotMatch -f $Name)
return $false
}
}

Export-ModuleMember -Function '*-TargetResource'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

[ClassVersion("1.0.0.0"), FriendlyName("xWindowsPackageCab")]
class MSFT_xWindowsPackageCab : OMI_BaseResource
{
[Key, Description("The name of the package to install or uninstall.")] String Name;
[Required, Description("Specifies whether the package should be installed or uninstalled. To install the package, set this property to Present. To uninstall the package, set the property to Absent."), ValueMap{"Present", "Absent"}, Values{"Present", "Absent"}] String Ensure;
[Required, Description("The path to the cab file to install or uninstall the package from.")] String SourcePath;
[Write, Description("The path to a file to log the operation to.")] String LogPath;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Description("This resource is used to install or uninstall a package from a windows cabinet (cab) file.") : Amended,AMENDMENT, LOCALE("MS_409")]
class MSFT_xWindowsPackageCab : OMI_BaseResource
{
[Key, Description("The name of the package to install or uninstall.") : Amended] String Name;
[Description("Specifies whether the package should be installed or uninstalled. To install the package, set this property to Present. To uninstall the package, set the property to Absent.") : Amended] String Ensure;
[Description("The path to the cab file to install or uninstall the package from.") : Amended] String SourcePath;
[Description("The path to a file to log the operation to.") : Amended] String LogPath;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Localized resources for xWindowsPackageCab

ConvertFrom-StringData @'
RetrievingPackage = Retrieving information for the package {0}
PackageEnsureState = The package {0} is currently {1}
SourcePathDoesNotExist = Could not find the source file at path {0}
SetTargetResourceStarting = Starting configuration of the WindowsPackageCab resource {0}
SetTargetResourceFinished = Finished configuration of WindowsPackageCab resource {0}
AddingPackage = Adding a package from the source at path {0}
RemovingPackage = Removing package from the source at path {0}
EnsureStatesMatch = Ensure states match for package {0}
EnsureStatesDoNotMatch = Ensure states do not match for package {0}
'@
49 changes: 49 additions & 0 deletions Examples/Sample_xWindowsPackageCab.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<#
.SYNOPSIS
Installs a package from the cab file with the specified name from the specified source path
and outputs a log to the specified log path.
.PARAMETER Name
The name of the package to install.
.PARAMETER SourcePath
The path to the cab file to install the package from.
.PARAMETER LogPath
The path to a file to log the install operation to.
.NOTES
The DISM PowerShell module must be available on the target machine.
#>
Configuration Sample_xWindowsPackageCab
{
param
(
[Parameter (Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
$Name,

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

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

Import-DscResource -ModuleName 'xPSDesiredStateConfiguration'

xWindowsPackageCab WindowsPackageCab1
{
Name = $Name
Ensure = 'Present'
SourcePath = $SourcePath
LogPath = $LogPath
}
}

Sample_xWindowsPackageCab
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ Please check out common DSC Resources [contributing guidelines](https://github.c
* **xWindowsFeatureSet** allows installation and uninstallation of a group of Windows features and their subfeatures.
* **xWindowsOptionalFeature** provides a mechanism to enable or disable optional features on a target node.
* **xWindowsOptionalFeatureSet** allows installation and uninstallation of a group of optional Windows features.
* **xWindowsPackageCab** provides a mechanism to install or uninstall a package from a windows cabinet (cab) file on a target node.

Resources that work on Nano Server:

* xWindowsOptionalFeature
* xUser
* xWindowsOptionalFeature
* xWindowsPackageCab

### xArchive

Expand Down Expand Up @@ -360,6 +362,30 @@ These parameters will be the same for each Windows optional feature in the set.
- Suported values: ErrorsOnly, ErrorsAndWarning, ErrorsAndWarningAndInformation.
- Default value: ErrorsOnly.

### xWindowsPackageCab
Provides a mechanism to install or uninstall a package from a windows cabinet (cab) file on a target node.
This resource works on Nano Server.

#### Requirements

* Target machine must have access to the DISM PowerShell module

#### Parameters

* **[String] Name** _(Key)_: The name of the package to install or uninstall.
* **[String] Ensure** _(Required)_: Specifies whether the package should be installed or uninstalled. To install the package, set this property to Present. To uninstall the package, set the property to Absent. { *Present* | Absent }.
* **[String] SourcePath** _(Required)_: The path to the cab file to install or uninstall the package from.
* **[String] LogPath** _(Write)_: The path to a file to log the operation to. There is no default value, but if not set, the log will appear at %WINDIR%\Logs\Dism\dism.log.

#### Read-Only Properties from Get-TargetResource

None

#### Examples

* [Install a cab file with the given name from the given path](https://github.com/PowerShell/xPSDesiredStateConfiguration/blob/dev/Examples/Sample_xWindowsPackageCab.ps1)


## Functions

### Publish-ModuleToPullServer
Expand All @@ -383,6 +409,7 @@ These parameters will be the same for each Windows optional feature in the set.
* xUser:
* Fixed PSSA/Style violations
* Added/Updated Tests and Examples
* Added xWindowsPackageCab

### 4.0.0.0

Expand Down
Loading

0 comments on commit 4f1faae

Please sign in to comment.