Note that if you utilize Action1 for patch management, it does not require this script, because Action1 does not rely on Windows Update Agent’s service manager catalog. It will simply detect all Microsoft product updates without any extra configuration steps.
#####################################
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[parameter(Mandatory = $true)]
[ValidateSet(“Present”,”Absent”)]
[System.String]
$Ensure
)
#Get the registered update services
$UpdateServices = (New-Object -ComObject Microsoft.Update.ServiceManager).Services
$returnValue = @{
Ensure = $Ensure
}
#Check if the microsoft update service is registered
if($UpdateServices | where {$_.ServiceID -eq ‘7971f918-a847-4430-9279-4a52d1efe18d’})
{
$returnValue.Ensure = ‘Present’
}
Else
{
$returnValue.Ensure = ‘Absent’
}
$returnValue
}
function Set-TargetResource
{
[CmdletBinding(SupportsShouldProcess=$true)]
param
(
[parameter(Mandatory = $true)]
[ValidateSet(“Present”,”Absent”)]
[System.String]
$Ensure
)
Switch($Ensure)
{
‘Present’
{
If($PSCmdlet.ShouldProcess(“Enable Microsoft Update”))
{
Try
{
Write-Verbose “Enable the Microsoft Update setting”
(New-Object -ComObject Microsoft.Update.ServiceManager).AddService2(‘7971f918-a847-4430-9279-4a52d1efe18d’,7,””)
Restart-Service wuauserv -ErrorAction SilentlyContinue
}
Catch
{
$ErrorMsg = $_.Exception.Message
Write-Verbose $ErrorMsg
}
}
}
‘Absent’
{
If($PSCmdlet.ShouldProcess(“$Drive”,”Disable Microsoft Update”))
{
Try
{
Write-Verbose “Disable the Microsoft Update setting”
(New-Object -ComObject Microsoft.Update.ServiceManager).RemoveService(‘7971f918-a847-4430-9279-4a52d1efe18d’)
}
Catch
{
$ErrorMsg = $_.Exception.Message
Write-Verbose $ErrorMsg
}
}
}
}
}
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[parameter(Mandatory = $true)]
[ValidateSet(“Present”,”Absent”)]
[System.String]
$Ensure
)
#Output the result of Get-TargetResource function.
$Get = Get-TargetResource -Ensure $Ensure
If($Ensure -eq $Get.Ensure)
{
return $true
}
Else
{
return $false
}
}
Export-ModuleMember -Function *-TargetResource
# The MIT License (MIT)
# Copyright (c) DSC Community contributors.
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.