DscResources/MSFT_xMicrosoftUpdate/MSFT_xMicrosoftUpdate.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
function Write-DeprecatedMessage
{
    Write-Warning -Message 'xMicrosoftUpdate is deprecated. Please use xWindows Update Agent'
}

function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [parameter(Mandatory = $true)]
        [ValidateSet("Present","Absent")]
        [System.String]
        $Ensure
    )

    Write-DeprecatedMessage
    Write-Verbose -Message "Getting Windows Update Agent services..."
    #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-Object {$_.ServiceID -eq '7971f918-a847-4430-9279-4a52d1efe18d'})
    {
        Write-Verbose -Message "Microsoft Update Present..."
        $returnValue.Ensure = 'Present'
    }
    Else
    {
        Write-Verbose -Message "Microsoft Update Absent..."
        $returnValue.Ensure = 'Absent'
    }
    
    $returnValue
}


function Set-TargetResource
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    param
    (
        [parameter(Mandatory = $true)]
        [ValidateSet("Present","Absent")]
        [System.String]
        $Ensure
    )

    Write-DeprecatedMessage
    Switch($Ensure)
    {
        'Present'
        {
            If($PSCmdlet.ShouldProcess("Enable Microsoft Update"))
            {
                Try
                {
                    Write-Verbose -Message "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 -Message "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
{
    # Verbose messages are written in Get.
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSDSCUseVerboseMessageInDSCResource", "")]
    [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