Skip to content

Commit

Permalink
Merge pull request #107 from nyanhp/restartsetting
Browse files Browse the repository at this point in the history
Added CMClientSettingsComputerRestart
  • Loading branch information
NEllis280 authored Sep 21, 2022
2 parents 0d2f470 + 17410f6 commit 4d9f50a
Show file tree
Hide file tree
Showing 11 changed files with 842 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ when collection queries have quotes.
- Fixed an issue causing some tests to unexpectedly fail with a newer
version of DSCResource.Common

- Added CMClientSettingsComputerRestart resource

## [3.0.0] - 2022-01-03

### Added
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu
client policy settings for Compliance settings.
- **CMClientSettingsComputerAgent**: Provides a resource for modifying the
client policy settings for Computer Agent settings.
- **CMClientSettingsComputerRestart**: Provides a resource for modifying the
client policy settings for Computer Restart settings.
- **CMClientSettingsDelivery**: Provides a resource for modifying the
client policy settings for Delivery settings.
- **CMClientSettingsHardware**: Provides a resource for modifying the
Expand Down Expand Up @@ -1685,6 +1687,29 @@ you are using apply and auto correct.

- [CMClientSettingsComputerAgent](Source\Examples\Resources\CMClientSettingsComputerAgent\CMClientSettingsComputerAgent.ps1)

### CMClientSettingsComputerRestart

- **[String] SiteCode** _(Key)_: Specifies the Site Code for the Configuration
Manager site.
- **[String] ClientSettingName** _(Key)_: Specifies which client settings policy
to modify.
- **[UInt32] CountdownMins** _(Write)_: Specifies countdown, in minutes,
for restart to take place..
- Values Range: 1 - 1440
- **[UInt32] FinalWindowMins** _(Write)_: Specifies the time window a restart
has to take place in.
- Values Range: 1 - 1440
- **[Boolean] ReplaceToastNotificationWithDialog** _(Write)_: Specifies if toast
notifications are replaced with dialog windows.
- **[Boolean] NoRebootEnforcement** _(Write)_: Specifies if reboots are not enforced.
- **[String] ClientSettingStatus** _(Read)_: Specifies if the client settings policy
exists.
- **[String] ClientType** _(Read)_: Specifies the type of client policy setting.

#### CMClientSettingsComputerRestart Examples

- [CMClientSettingsComputerRestart](Source\Examples\Resources\CMClientSettingsComputerRestart\CMClientSettingsComputerRestart.ps1)

### CMClientSettingsDelivery

- **[String] SiteCode** _(Key)_: Specifies the Site Code for the Configuration
Expand Down Expand Up @@ -2230,6 +2255,7 @@ all of the modules and specify if it is currently supported by ReverseDSC.
- DSC_CMClientSettingsCloudService : Fully Supported
- DSC_CMClientSettingsCompliance : Fully Supported
- DSC_CMClientSettingsComputerAgent : Fully Supported
- DSC_CMClientSettingsComputerRestart : Fully Supported
- DSC_CMClientSettingsDelivery : Fully Supported
- DSC_CMClientSettingsHardware : Fully Supported
- DSC_CMClientSettingsMetered : Fully Supported
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
$script:dscResourceCommonPath = Join-Path -Path $PSScriptRoot -ChildPath '..\..\Modules\DscResource.Common'
$script:configMgrResourcehelper = Join-Path -Path $PSScriptRoot -ChildPath '..\..\Modules\ConfigMgrCBDsc.ResourceHelper'

Import-Module -Name $script:dscResourceCommonPath
Import-Module -Name $script:configMgrResourcehelper

$script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'

<#
.SYNOPSIS
This will return a hashtable of results.
.PARAMETER SiteCode
Specifies the site code for Configuration Manager site.
.Parameter ClientSettingName
Specifies which client settings policy to modify.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[String]
$SiteCode,

[Parameter(Mandatory = $true)]
[String]
$ClientSettingName
)

Write-Verbose -Message $script:localizedData.RetrieveSettingValue
Import-ConfigMgrPowerShellModule -SiteCode $SiteCode
Set-Location -Path "$($SiteCode):\"

$clientSetting = Get-CMClientSetting -Name $ClientSettingName

if ($clientSetting)
{
$type = @('Default', 'Device', 'User')[$clientSetting.Type]
$settings = Get-CMClientSetting -Name $ClientSettingName -Setting ComputerRestart

if ($settings)
{
$countdownMins = $settings.RebootLogoffNotificationCountdownDuration
$finalWindowMins = $settings.RebootLogoffNotificationFinalWindow
$replaceToast = [System.Convert]::ToBoolean($settings.RebootNotificationsDialog)
$noRebootEnforcement = -not [System.Convert]::ToBoolean($settings.EnforeReboot)
}

$status = 'Present'
}
else
{
$status = 'Absent'
}

return @{
SiteCode = $SiteCode
ClientSettingName = $ClientSettingName
CountdownMins = $countdownMins
FinalWindowMins = $finalWindowMins
ReplaceToastNotificationWithDialog = $replaceToast
NoRebootEnforcement = $noRebootEnforcement
ClientSettingStatus = $status
ClientType = $type
}
}

<#
.SYNOPSIS
This will set the desired state.
.PARAMETER SiteCode
Specifies a site code for the Configuration Manager site.
.Parameter ClientSettingName
Specifies which client settings policy to modify.
.PARAMETER CountdownMins
Specifies countdown, in minutes, for restart to take place.
.PARAMETER FinalWindowMins
Specifies the time window a restart has to take place in.
.PARAMETER ReplaceToastNotificationWithDialog
Specifies if toast notifications are replaced with dialog windows.
.PARAMETER NoRebootEnforcement
Specifies if reboots are not enforced.
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[String]
$SiteCode,

[Parameter(Mandatory = $true)]
[String]
$ClientSettingName,

[Parameter()]
[ValidateRange(1, 1440)]
[uint32]
$CountdownMins,

[Parameter()]
[ValidateRange(1, 1440)]
[uint32]
$FinalWindowMins,

[Parameter()]
[bool]
$ReplaceToastNotificationWithDialog,

[Parameter()]
[bool]
$NoRebootEnforcement
)

Import-ConfigMgrPowerShellModule -SiteCode $SiteCode
Set-Location -Path "$($SiteCode):\"
$state = Get-TargetResource -SiteCode $SiteCode -ClientSettingName $ClientSettingName

try
{
if ($state.ClientSettingStatus -eq 'Absent')
{
throw ($script:localizedData.ClientPolicySetting -f $ClientSettingName)
}

if ($state.ClientType -eq 'User')
{
throw $script:localizedData.WrongClientType
}

if ($CountdownMins -le $FinalWindowMins)
{
throw ($script:localizedData.CountdownLessFinalWindow -f $CountdownMins, $FinalWindowMins)
}

$defaultValues = @('CountdownMins', 'FinalWindowMins', 'ReplaceToastNotificationWithDialog', 'NoRebootEnforcement')

foreach ($param in $PSBoundParameters.GetEnumerator())
{
if ($defaultValues -contains $param.Key)
{
if ($param.Value -ne $state[$param.Key])
{
Write-Verbose -Message ($script:localizedData.SettingValue -f $param.Key, $param.Value)
$buildingParams += @{
$param.Key = $param.Value
}
}
}
}

if ($buildingParams)
{
Set-CMClientSettingComputerRestart -Name $ClientSettingName @buildingParams
}
}
catch
{
throw $_
}
finally
{
Set-Location -Path "$env:temp"
}
}

<#
.SYNOPSIS
This will test the desired state.
.PARAMETER SiteCode
Specifies a site code for the Configuration Manager site.
.Parameter ClientSettingName
Specifies which client settings policy to modify.
.PARAMETER CountdownMins
Specifies countdown, in minutes, for restart to take place.
.PARAMETER FinalWindowMins
Specifies the time window a restart has to take place in.
.PARAMETER ReplaceToastNotificationWithDialog
Specifies if toast notifications are replaced with dialog windows.
.PARAMETER NoRebootEnforcement
Specifies if reboots are not enforced.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[String]
$SiteCode,

[Parameter(Mandatory = $true)]
[String]
$ClientSettingName,

[Parameter()]
[ValidateRange(1, 1440)]
[uint32]
$CountdownMins,

[Parameter()]
[ValidateRange(1, 1440)]
[uint32]
$FinalWindowMins,

[Parameter()]
[bool]
$ReplaceToastNotificationWithDialog,

[Parameter()]
[bool]
$NoRebootEnforcement
)

Import-ConfigMgrPowerShellModule -SiteCode $SiteCode
Set-Location -Path "$($SiteCode):\"
$state = Get-TargetResource -SiteCode $SiteCode -ClientSettingName $ClientSettingName
$result = $true

if ($state.ClientSettingStatus -eq 'Absent')
{
Write-Warning -Message ($script:localizedData.ClientPolicySetting -f $ClientSettingName)
$result = $false
}
elseif ($state.ClientType -eq 'User')
{
Write-Warning -Message $script:localizedData.WrongClientType
$result = $false
}
else
{
$defaultValues = @('CountdownMins', 'FinalWindowMins', 'ReplaceToastNotificationWithDialog', 'NoRebootEnforcement')

$testParams = @{
CurrentValues = $state
DesiredValues = $PSBoundParameters
ValuesToCheck = $defaultValues
}

$result = Test-DscParameterState @testParams -TurnOffTypeChecking -Verbose
}

Write-Verbose -Message ($script:localizedData.TestState -f $result)
Set-Location -Path "$env:temp"
return $result
}

Export-ModuleMember -Function *-TargetResource
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[ClassVersion("1.0.0"), FriendlyName("CMClientSettingsComputerRestart")]
class DSC_CMClientSettingsComputerRestart : OMI_BaseResource
{
[Key, Description("Specifies the SiteCode for the Configuration Manager site.")] String SiteCode;
[Key, Description("Specifies which client settings policy to modify.")] String ClientSettingName;
[Write, Description("Specifies countdown, in minutes, for restart to take place.")] UInt32 CountdownMins;
[Write, Description("Specifies the time window a restart has to take place in.")] UInt32 FinalWindowMins;
[Write, Description("Specifies if toast notifications are replaced with dialog windows.")] Boolean ReplaceToastNotificationWithDialog;
[Write, Description("Specifies if reboots are not enforced.")] Boolean NoRebootEnforcement;
[Read, Description("Specifies if the client settings policy exists.")] String ClientSettingStatus;
[Read, Description("Specifies the type of client policy setting.")] String ClientType;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ConvertFrom-StringData @'
RetrieveSettingValue = Getting results for Configuration Manager client policy for computer agent settings.
ClientPolicySetting = Client Policy setting {0} does not exist, and will need to be created prior to making client setting changes.
TestState = Test-TargetResource compliance check returned: {0}.
SettingValue = Setting value: {0} to {1}.
WrongClientType = Client Settings for computer restart only applies to Default and Device Client settings.
CountdownLessFinalWindow = Countdown {0} minutes is less or equal final window {1} minutes.
'@
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<#
.SYNOPSIS
A DSC configuration script to modify client policy settings for computer restart settings.
#>
Configuration Example
{
Import-DscResource -ModuleName ConfigMgrCBDsc

Node localhost
{

CMClientSettingsComputerRestart DeviceAgent
{
SiteCode = 'Lab'
ClientSettingName = 'ClientTest'
NoRebootEnforcement = $true
CountdownMins = 30
FinalWindowMins = 10
ReplaceToastNotificationWithDialog = $true
}
}
}
Loading

0 comments on commit 4d9f50a

Please sign in to comment.