NEW ACTION1 PLATFORM: NOW WITH MACOS SUPPORT

This Wednesday | 12 PM EST / 11 AM CET

Action1 5 How-to Articles 5 Enabling Microsoft Update via PowerShell or Command Line?

Enabling Microsoft Update via PowerShell or Command Line?

January 3, 2023

By Mike Walters

Many Microsoft products, such as Microsoft Office, can be updated using built-in Windows Update features. However, it may not be enabled by default on all of your systems. Your existing RMM may fall short of properly enabling it, and therefore will not see updates for Microsoft products, leaving them unpatched and exposed to vulnerabilities. The following script enables it, and you can run this script using your RMM.

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.

See What You Can Do with Action1

 

Join our weekly LIVE demo “Patch Management That Just Works with Action1” to learn more

about Action1 features and use cases for your IT needs.

 

spiceworks logo
getapp logo review
software advice review
trustradius
g2 review
spiceworks logo

Related Posts