Functions/Get-ECSWSUSUpdateReport.ps1

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
Function Get-ECSWSUSUpdateReport
    {
    <#
    .SYNOPSIS
    This report will give you a list of the update status for a given update across your wsus environment
 
    .DESCRIPTION
    This report will give you a list of the update status for a given update across your wsus environment
 
    .PARAMETER UpdateIDObject
    This is the update id GUID object that you'll pipe from a command such as Get-ECSWSUSUpdate
 
    .PARAMETER IncludeNonApplicableUpdates
    By default, we only grab applicable updates. There may be a reason i can't forsee thatto see ALL updates for a given computer, using this switch if that's the case.
 
    .PARAMETER UpdateApprovalAction
    This is to influence whether you only want to report on updates that are approved, not approved or any status. We default to any.
 
 
    .EXAMPLE
    This example gets the status of a singular update
    Get-ECSWSUSUpdateReport -UpdateIDObject "dddcf9cd-6eee-4e16-8bd8-85117ed9b7de" -UpdateApprovalAction Install
 
    .EXAMPLE
    This example, we'll get a list of updated pipelined from get-ecswsusudpate
    Get-ECSWSUSUpdate | Select-Object -First 5 -ExpandProperty updateid | Get-ECSWSUSUpdateReport -UpdateApprovalAction Install
 
 
 
    #>

    [CmdletBinding()]
    

    Param
        (
        #############################################################
        #UpdateIDObject


        [Parameter(
            ValueFromPipelineByPropertyName = $true,
            ValueFromPipeline = $true,
            Mandatory=$true
            )]

        [ValidateNotNullorEmpty()]
        $UpdateIDObject,

        #############################################################
        #IncludeNonApplicableUpdates

        [Parameter(Mandatory=$false)]  
        [Switch]$IncludeNonApplicableUpdates = $false,


        #############################################################
        #UpdateApprovalAction
        [Parameter(Mandatory=$false)]
        [ValidateSet("All","Install","NotApproved")] 
        [String]$UpdateApprovalAction = "All"

        )

    Process
        {
        #########################################################################
        #Global Params

        $WSUSSQLServerName = $Global:ECSWSUSDatabaseConnection.WSUSSQLServerName
        Write-Verbose "WSUSSQLServerName = $($WSUSSQLServerName)"
        $WSUSDatabaseName = $Global:ECSWSUSDatabaseConnection.WSUSDatabaseName
        Write-Verbose "WSUSDatabaseName = $($WSUSDatabaseName)"
        $Credential = $Global:ECSWSUSDatabaseConnection.Credential
        
        #END Global Params
        #########################################################################

        #########################################################################
        #Test connection state

        Write-Verbose "Testing the WSUS database connectivity before querying WSUS"
        $TestWSUSConnectionState = Test-ECSWSUSDatabaseConnected
        If ($TestWSUSConnectionState.OverallStatus -eq $false)
            {
            Throw "Either you are not connected to the WSUS database, or we had an error. Please run Test-ECSWSUSDatabaseConnected to determine the detailed status"
            }

        #END Test connection state
        #########################################################################


        Foreach ($UpdateObject in $UpdateIDObject)

            {

            #########################################################################
            #Define dynamic SQL query

            #Creating two different base queries based on whether we're doing a detailed report or a summary report only

            $SQLQueryToRun = @"
Use $($WSUSDatabaseName)
 
DECLARE @UpdateId uniqueidentifier;
SET @UpdateId = '$($UpdateObject)';
 
 
SELECT [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateInstallationInfo].[UpdateId]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateInstallationInfo].[ComputerTargetId]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateInstallationInfo].[State]
      ,CASE
        WHEN State = 0 THEN 'Unknown'
        WHEN State = 1 THEN 'NotApplicable'
        When State = 2 THEN 'NotInstalled'
        WHEN State = 3 THEN 'Downloaded'
        WHEN State = 4 THEN 'Installed'
        WHEN State = 5 THEN 'Failed'
        WHEN State = 6 THEN 'InstalledPendingReboot'
        Else 'No Match'
        end as FriendlyState
 
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[RevisionNumber]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[DefaultTitle]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[DefaultDescription]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[ClassificationId]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[ArrivalDate]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[CreationDate]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[IsDeclined]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[IsWsusInfrastructureUpdate]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[MsrcSeverity]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[PublicationState]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[UpdateType]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[UpdateSource]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[KnowledgebaseArticle]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[SecurityBulletin]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[InstallationCanRequestUserInput]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[InstallationRequiresNetworkConnectivity]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[InstallationImpact]
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[InstallationRebootBehavior]
 
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateApproval].[Action]
 
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vComputerTarget].[Name]
 
      ,[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vComputerTargetGroup] .[Name] as TargetGoupName
 
  FROM [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateInstallationInfo]
 
  --Find the matching computer target
inner join [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vComputerTarget] on
[$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vComputerTarget].[ComputerTargetId] = [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateInstallationInfo].[ComputerTargetId]
 
--Find the approval ID to see whether the update is approved or not
inner join [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateEffectiveApprovalPerComputer] on
    (
    [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateEffectiveApprovalPerComputer].[ComputerTargetId] = [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateInstallationInfo].[ComputerTargetId]
    and
    [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateEffectiveApprovalPerComputer].[UpdateId] = [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateInstallationInfo].[UpdateId]
    )
     
 
--Link the update approval ID's for the effective computer approval
inner join [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateApproval] on
    (
    [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateApproval].[UpdateApprovalId] = [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateEffectiveApprovalPerComputer].[UpdateApprovalId]
 
    )
 
--Show the computer target group, so we know the frinedly name of where the approval comes from.
inner join [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vComputerTargetGroup] on
    (
    [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vComputerTargetGroup].[ComputerTargetGroupId] = [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateApproval].[ComputerTargetGroupId]
 
    )
 
--Get update details
inner join [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate] on
    (
    [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdate].[UpdateId] = [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateInstallationInfo].[UpdateId]
    )
 
where [$($WSUSDatabaseName)].[PUBLIC_VIEWS].[vUpdateInstallationInfo].[UpdateId] = @UpdateId and State != 1
"@

            

            #Applicable Updates only
            If ($IncludeNonApplicableUpdates -eq $true)
                {
                Write-Verbose "IncludeNonApplicableUpdates = $true"
                }
            Else
                {
                Write-Verbose "IncludeNonApplicableUpdates = $false"
                                 
                $SQLQueryToRun += @"
and
[PUBLIC_VIEWS].[vUpdateInstallationInfo].[State] != 1
"@

                }


            #Approved Updates only
            Write-Verbose "UpdateApprovalAction = $($UpdateApprovalAction)"
            If ($UpdateApprovalAction -ne "All")
                {
                $SQLQueryToRun += @"
and
[PUBLIC_VIEWS].[vUpdateApproval].[Action] = '$($UpdateApprovalAction)'
"@

                }


            #END Define dynamic SQL query
            #########################################################################

            #########################################################################
            #Executing SQL query
            Try
                {
                #Formulating base command
                $SQLCommandToRun = '$SQLQuery' + " = Invoke-Sqlcmd -ServerInstance $WSUSSQLServerName -Database $WSUSDatabaseName -AbortOnError" + ' -Query $SQLQueryToRun' + ' -ErrorAction "Stop"'

                #Executing command
                Write-Verbose "Executing the following command: $($SQLCommandToRun)"
                Write-Verbose "The following query is being executed:"
                Write-Verbose $SQLQueryToRun

                #Running query
                Invoke-Expression -Command $($SQLCommandToRun)

                #Making sure you had results, if not we're throwing an error
                $MeaureSQLQueryCount = $SQLQuery | Measure-Object | Select-Object -ExpandProperty count
                If ($MeaureSQLQueryCount -eq 0)
                    {
                    Throw "No results querying computer target $($UpdateObject.FullDomainName)"
                    }

                }
            Catch
                {
                Throw "Something went wrong with the SQL query, exception message = $($_.Exception.Message)"
                }

            #END Executing SQL query
            #########################################################################

            
            #########################################################################
            #Output Report

            $SQLQuery

            #End Output Report
            #########################################################################

            }
        }
    }