forked from Pascal-KOTTE/patchtrending
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspTrendPatchComplianceByUpdate.sql
More file actions
133 lines (111 loc) · 5.65 KB
/
spTrendPatchComplianceByUpdate.sql
File metadata and controls
133 lines (111 loc) · 5.65 KB
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
BEGIN TRAN
GO
exec sp_rename 'TREND_WindowsCompliance_ByUpdate', 'TREND_WindowsCompliance_ByUpdate_old'
GO
drop procedure spTrendPatchComplianceByUpdate
GO
create procedure spTrendPatchComplianceByUpdate
@collectionguid as uniqueidentifier = '01024956-1000-4cdb-b452-7db0cff541b6',
@force as int = 0
as
-- #########################################################################################################
-- PART I: Make sure underlying infrastructure exists and is ready to use
if (exists(select 1 from sys.objects where name = 'PM_TRENDS_TEMP' and type = 'U'))
begin
truncate table PM_TRENDS_TEMP
end
else
begin
CREATE TABLE [dbo].[PM_TRENDS_TEMP](
[_SWUGuid] [uniqueidentifier] NOT NULL,
[Bulletin] [varchar](250) NOT NULL,
[Update] [varchar](250) NOT NULL,
[Severity] [varchar](250) NOT NULL,
[Custom Severity] [nvarchar](100) NULL,
[Release Date] [datetime] NOT NULL,
[Compliance] [numeric](6, 2) NULL,
[Applicable (Count)] [int] NULL,
[Installed (Count)] [int] NULL,
[Not Installed (Count)] [int] NULL,
[_SWBGuid] [uniqueidentifier] NOT NULL,
[_ScopeCollection] [uniqueidentifier] NULL,
[_Collection] [uniqueidentifier] NULL,
[_StartDate] [datetime] NULL,
[_EndDate] [datetime] NULL,
[_DistributionStatus] [nvarchar](16) NULL,
[_OperatingSystem] [nvarchar](128) NULL,
[_VendorGuid] [uniqueidentifier] NULL,
[_CategoryGuid] [uniqueidentifier] NULL
) ON [PRIMARY]
end
if (not exists(select 1 from sys.objects where type = 'U' and name = 'TREND_WindowsCompliance_ByUpdate'))
begin
CREATE TABLE [dbo].[TREND_WindowsCompliance_ByUpdate](
[_Exec_id] [int] NOT NULL,
[CollectionGuid] [uniqueidentifier] NOT NULL,
[_Exec_time] [datetime] NOT NULL,
[Bulletin] [varchar](250) NOT NULL,
[UPDATE] [varchar](250) NOT NULL,
[Severity] [varchar](250) NOT NULL,
[Installed] [int] NULL,
[Applicable] [int] NULL,
[DistributionStatus] [nvarchar](16) NULL
) ON [PRIMARY]
CREATE UNIQUE CLUSTERED INDEX [IX_TREND_WindowsCompliance_ByUpdate] ON [dbo].[TREND_WindowsCompliance_ByUpdate]
(
[CollectionGuid] asc,
[Bulletin] ASC,
[Update] ASC,
[_exec_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING =
OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IX_TREND_WindowsCompliance_ByUpdate_OrderbyUpdate] ON [dbo].[TREND_WindowsCompliance_ByUpdate]
(
[UPDATE] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
end
-- PART II: Get data into the trending table if no data was captured in the last 24 hours
if (select MAX(_exec_time) from TREND_WindowsCompliance_ByUpdate where CollectionGuid = @CollectionGuid) < dateadd(hour, -23, getdate()) or ((select COUNT(*) from TREND_WindowsCompliance_ByUpdate where CollectionGuid = @CollectionGuid) = 0) or (@force = 1)
begin
-- Get the compliance by update to a 'temp' table
insert into PM_TRENDS_TEMP
exec spPMWindows_ComplianceByUpdate
@OperatingSystem = '%',
@DistributionStatus = 'Active',
@FilterCollection = @collectionguid,
@StartDate = '1900-06-29T00:00:00',
@EndDate = '2020-06-29T00:00:00',
@pCulture = 'en-GB',
@ScopeCollectionGuid = '91c68fcb-1822-e793-b59c-2684e99a64cd',
@TrusteeScope = '{2e1f478a-4986-4223-9d1e-b5920a63ab41}',
@VendorGuid = '00000000-0000-0000-0000-000000000000',
@CategoryGuid = '00000000-0000-0000-0000-000000000000',
@DisplayMode = 'all'
declare @id as int
set @id = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where CollectionGuid = @CollectionGuid)
insert into TREND_WindowsCompliance_ByUpdate
select (ISNULL(@id + 1, 1)), @collectionguid, GETDATE() as '_Exec_time', Bulletin, [UPDATE], Severity, [Installed (Count)] as 'Installed', [Applicable (Count)] as 'Applicable', _DistributionStatus as 'DistributionStatus'
from PM_TRENDS_TEMP
end
-- Return the latest results
select *, applicable - installed as 'Vulnerable', cast(cast(installed as float) / cast(applicable as float) * 100 as money) as 'Compliance %'
from TREND_WindowsCompliance_ByUpdate
where _exec_id = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where CollectionGuid = @CollectionGuid)
and CollectionGuid = @CollectionGuid
-- and cast(cast(installed as float) / cast(applicable as float) * 100 as money) < %ComplianceThreshold%
-- and applicable > %ApplicableThreshold%
union
select max(_exec_id), @CollectionGuid, max(_exec_time), Bulletin, '-- ALL --' as [update], '' as severity, sum(installed) as 'Installed', sum(applicable) as 'Applicable', '' as DistributionStatus, sum(applicable) - sum(installed) as 'Vulnerable', cast(cast(sum(installed) as float) / cast(sum(applicable) as float) * 100 as money) as 'Compliance %'
from TREND_WindowsCompliance_ByUpdate
where _exec_id = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where CollectionGuid = @CollectionGuid)
and CollectionGuid = @CollectionGuid
group by Bulletin
--having sum(applicable) >%ApplicableThreshold%
-- and cast(cast(sum(installed) as float) / cast(sum(applicable) as float) * 100 as money) < %ComplianceThreshold%
order by Bulletin,[update]
GO
exec spTrendPatchComplianceByUpdate '01024956-1000-4cdb-bbbb-7db0cff541b6'
GO
insert TREND_WindowsCompliance_ByUpdate ([_Exec_id], [CollectionGuid], [_Exec_time], [Bulletin], [UPDATE], [Severity], [Installed], [Applicable], [DistributionStatus])
select [_Exec_id], '311E8DAE-2294-4FF2-B9EF-B3D6A84183CB' as 'CollectionGuid', [_Exec_time], [Bulletin], [UPDATE], [Severity], [Installed], [Applicable], [DistributionStatus] from TREND_WindowsCompliance_ByUpdate_old
commit