-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
644 additions
and
1 deletion.
There are no files selected for viewing
225 changes: 225 additions & 0 deletions
225
DSCResources/MSFT_xWindowsPackageCab/MSFT_xWindowsPackageCab.psm1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
9 changes: 9 additions & 0 deletions
9
DSCResources/MSFT_xWindowsPackageCab/MSFT_xWindowsPackageCab.schema.mof
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
8 changes: 8 additions & 0 deletions
8
DSCResources/MSFT_xWindowsPackageCab/en-US/MSFT_xWindowsPackageCab.schema.mfl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
13 changes: 13 additions & 0 deletions
13
DSCResources/MSFT_xWindowsPackageCab/en-US/MSFT_xWindowsPackageCab.strings.psd1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
'@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.