Create WMI Filters

  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
<#
    .Synopsis
    Script for creating WMI Filters for use with Group Policy Manager.
    .DESCRIPTION
    The Script will create several WMI Filters for filtering based on:
    - Processor Architecture.
    - If the Hosts is a Virtual Machine
    - Operating System Version.
    - Type of Operating System.
    - If Java is installed
    - If Version 6 or 7 of Java JRE is installed.
    - Version of IE
    .EXAMPLE
    Running script if verbose output

    .\install-wmifilters.ps1 -Verbose
    .NOTES
    Author: Carlos Perez carlos_perez[at]darkoperator.com
    Date: 1/13/13
    Requirements: Execution policy should be RemoteSigned since script is not signed.
#>

[cmdletbinding(SupportsShouldProcess = $true)]
param()

Import-Module -Name ActiveDirectory

Function Set-DCAllowSystemOnlyChange
{
  param ([switch]$Set)
  if ($Set)
  {
    Write-Verbose -Message 'Checking is registry key is set to allow changes to AD System Only Attributes is set.'
    $ntds_vals = (Get-Item -Path HKLM:\System\CurrentControlSet\Services\NTDS\Parameters).GetValueNames()
    if ( $ntds_vals -eq 'Allow System Only Change')
    {
      $kval = Get-ItemProperty -Path HKLM:\System\CurrentControlSet\Services\NTDS\Parameters -Name 'Allow System Only Change'
      if ($kval -eq '1')
      {
        Write-Verbose -Message 'Allow System Only Change key is already set'
      }
      else
      {
        Write-Verbose -Message 'Allow System Only Change key is not set'
        Write-Verbose -Message 'Creating key and setting value to 1'
        $null = Set-ItemProperty -Path HKLM:\System\CurrentControlSet\Services\NTDS\Parameters -Name 'Allow System Only Change' -Value 0
      }
    }
    else
    {
      $null = New-ItemProperty -Path HKLM:\System\CurrentControlSet\Services\NTDS\Parameters -Name 'Allow System Only Change' -Value 1 -PropertyType 'DWord'
    }
  }
  else
  {
    $ntds_vals = (Get-Item -Path HKLM:\System\CurrentControlSet\Services\NTDS\Parameters).GetValueNames()
    if ( $ntds_vals -eq 'Allow System Only Change')
    {
      Write-Verbose -Message 'Disabling Allow System Only Change Attributes on server'
      $null = Set-ItemProperty -Path HKLM:\System\CurrentControlSet\Services\NTDS\Parameters -Name 'Allow System Only Change' -Value 0
    }
  }
}
Function Create-WMIFilters
{
  # Based on function from http://gallery.technet.microsoft.com/scriptcenter/f1491111-9f5d-4c83-b436-537eca9e8d94
  # Name,Query,Description
  $WMIFilters = @(
    ('Hyper-V Virtual Machines', 
      'SELECT * FROM Win32_ComputerSystem WHERE Model = "Virtual Machine"', 
    'Microsoft Hyper-V 2.0 AND 3.0'), 
    ('VMware Virtual Machines', 
      'SELECT * FROM Win32_ComputerSystem WHERE Model LIKE "VMware%"', 
    'VMware Fusion, WORkstation AND ESXi'), 
    ('Parallels Virtual Machines', 
      'SELECT * FROM Win32_ComputerSystem WHERE Model LIKE "Parallels%"', 
    'OSX Parallels Virtual Machine'), 
    ('VirtualBox Virtual Machines', 
      'SELECT * FROM Win32_ComputerSystem WHERE Model LIKE "VirtualBox%"', 
    'Oracle VirtualBox Virtual Machine'), 
    ('Xen Virtual Machines', 
      'SELECT * FROM Win32_ComputerSystem WHERE Model LIKE "HVM dom%"', 
    'Citrix Xen Server Virtual Machine'), 
    ('Virtual Machines', 
      'SELECT * FROM Win32_ComputerSystem WHERE (Model LIKE "Parallels%" OR Model LIKE "HVM dom% OR Model LIKE "VirtualBox%" OR Model LIKE "Parallels%" OR Model LIKE "VMware%" OR Model = "Virtual Machine")', 
    'Virtual Machine from Hyper-V, VMware, Xen, Parallels OR VirtualBox'), 
    ('Java is Installed', 
      'SELECT * FROM win32_DirectORy WHERE (name="c:\\Program Files\\Java" OR name="c:\\Program Files (x86)\\Java")', 
    'Oracle Java'), 
    ('Java JRE 7 is Installed', 
      'SELECT * FROM win32_DirectORy WHERE (name="c:\\Program Files\\Java\\jre7" OR name="c:\\Program Files (x86)\\Java\\jre7")', 
    'Oracle Java JRE 7'), 
    ('Java JRE 6 is Installed', 
      'SELECT * FROM win32_DirectORy WHERE (name="c:\\Program Files\\Java\\jre6" OR name="c:\\Program Files (x86)\\Java\\jre6")', 
    'Oracle Java JRE 6'), 
    ('Workstation 32-bit', 
      'Select * from WIN32_OperatingSystem WHERE ProductType=1 Select * from Win32_Processor WHERE AddressWidth = "32"', 
    ''), 
    ('x64 Machines - Not Domain Controllers', 
      'select * from Win32_OperatingSystem WHERE ProductType = "1" OR ProductType = "3" AND OSArchitecture = "64-bit"', 
    ''),
    ('x86 Machines - Not Domain Controllers', 
      'select * from Win32_OperatingSystem WHERE ProductType = "1" OR ProductType = "3" AND OSArchitecture = "32-bit"', 
    ''), 
    ('Workstation 64-bit', 
      'Select * from WIN32_OperatingSystem WHERE ProductType=1 Select * from Win32_Processor WHERE AddressWidth = "64"', 
    ''), 
    ('Workstations', 
      'SELECT * FROM Win32_OperatingSystem WHERE ProductType = "1"', 
    ''), 
    ('Domain Controllers', 
      'SELECT * FROM Win32_OperatingSystem WHERE ProductType = "2"', 
    ''), 
    ('Servers', 
      'SELECT * FROM Win32_OperatingSystem WHERE ProductType = "3"', 
    ''), 
    ('Windows XP', 
      'SELECT * FROM Win32_OperatingSystem WHERE (Version LIKE "5.1%" OR Version LIKE "5.2%") AND ProductType = "1"', 
    ''), 
    ('Windows Vista', 
      'SELECT * FROM Win32_OperatingSystem WHERE Version LIKE "6.0%" AND ProductType = "1"', 
    ''), 
    ('Windows 7', 
      'SELECT * FROM Win32_OperatingSystem WHERE Version LIKE "6.1%" AND ProductType = "1"', 
    ''), 
    ('Windows 8', 
      'SELECT * FROM Win32_OperatingSystem WHERE Version LIKE "6.2%" AND ProductType = "1"', 
    ''), 
    ('Windows Server 2003', 
      'SELECT * FROM Win32_OperatingSystem WHERE Version LIKE "5.2%" AND ProductType = "3"', 
    ''), 
    ('Windows Server 2008', 
      'SELECT * FROM Win32_OperatingSystem WHERE Version LIKE "6.0%" AND ProductType = "3"', 
    ''), 
    ('Windows Server 2008 R2', 
      'SELECT * FROM Win32_OperatingSystem WHERE Version LIKE "6.1%" AND ProductType = "3"', 
    ''), 
    ('Windows Server 2012', 
      'SELECT * FROM Win32_OperatingSystem WHERE Version LIKE "6.2%" AND ProductType = "3"', 
    ''), 
    ('Windows Vista AND Windows Server 2008', 
      'SELECT * FROM Win32_OperatingSystem WHERE Version LIKE "6.0%" AND ProductType<>"2"', 
    ''), 
    ('Windows Server 2003 AND Windows Server 2008', 
      'SELECT * FROM Win32_OperatingSystem WHERE (Version LIKE "5.2%" OR Version LIKE "6.0%") AND ProductType="3"', 
    ''), 
    ('Windows XP AND 2003', 
      'SELECT * FROM Win32_OperatingSystem WHERE Version LIKE "5.%" AND ProductType<>"2"', 
    ''), 
    ('Windows 8 AND 2012', 
      'SELECT * FROM Win32_OperatingSystem WHERE Version LIKE "6.2%" AND ProductType<>"2"', 
    ''), 
    ('Internet Explorer 10', 
    'SELECT * FROM CIM_Datafile WHERE (Name="c:\\Program Files (x86)\\Internet Explorer\\iexplore.exe" OR Name="c:\\Program Files\\Internet Explorer\\iexplore.exe") AND version LIKE "10.%"'), 
    ('Internet Explorer 9', 
    'SELECT * FROM CIM_Datafile WHERE (Name="c:\\Program Files (x86)\\Internet Explorer\\iexplore.exe" OR Name="c:\\Program Files\\Internet Explorer\\iexplore.exe") AND version LIKE "9.%"'), 
    ('Internet Explorer 8', 
    'SELECT * FROM CIM_Datafile WHERE (Name="c:\\Program Files (x86)\\Internet Explorer\\iexplore.exe" OR Name="c:\\Program Files\\Internet Explorer\\iexplore.exe") AND version LIKE "8.%"'), 
    ('Internet Explorer 7', 
    'SELECT * FROM CIM_Datafile WHERE (Name="c:\\Program Files (x86)\\Internet Explorer\\iexplore.exe" OR Name="c:\\Program Files\\Internet Explorer\\iexplore.exe") AND version LIKE "7.%"')
  )

  $defaultNamingContext = (get-adrootdse).defaultnamingcontext 
  $configurationNamingContext = (get-adrootdse).configurationNamingContext 
  $msWMIAuthor = '[email protected]' + [System.DirectoryServices.ActiveDirectory.Domain]::getcurrentdomain().name

  Write-Verbose -Message 'Starting creation of WMI Filters:'
  for ($i = 0; $i -lt $WMIFilters.Count; $i++) 
  {
    $WMIGUID = [string]'{'+([System.Guid]::NewGuid())+'}'   
    $WMIDN = 'CN='+$WMIGUID+',CN=SOM,CN=WMIPolicy,CN=System,'+$defaultNamingContext
    $WMICN = $WMIGUID
    $WMIdistinguishedname = $WMIDN
    $WMIID = $WMIGUID

    $now = (Get-Date).ToUniversalTime()
    $msWMICreationDate = ($now.Year).ToString('0000') + ($now.Month).ToString('00') + ($now.Day).ToString('00') + ($now.Hour).ToString('00') + ($now.Minute).ToString('00') + ($now.Second).ToString('00') + '.' + ($now.Millisecond * 1000).ToString('000000') + '-000'

    $msWMIName = $WMIFilters[$i][0]
    $msWMIParm1 = $WMIFilters[$i][2] + ' '
    $msWMIParm2 = '1;3;10;' + $WMIFilters[$i][1].Length.ToString() + ';WQL;root\CIMv2;' + $WMIFilters[$i][1] + ';'

    $Attr = @{
      'msWMI-Name'           = $msWMIName
      'msWMI-Parm1'          = $msWMIParm1
      'msWMI-Parm2'          = $msWMIParm2
      'msWMI-Author'         = $msWMIAuthor
      'msWMI-ID'             = $WMIID
      'instanceType'         = 4
      'showInAdvancedViewOnly' = 'TRUE'
      'distinguishedname'    = $WMIdistinguishedname
      'msWMI-ChangeDate'     = $msWMICreationDate
      'msWMI-CreationDate'   = $msWMICreationDate
    }
    $WMIPath = ('CN=SOM,CN=WMIPolicy,CN=System,'+$defaultNamingContext)

    Write-Verbose -Message "Adding WMI Filter for: $msWMIName"
    $null = New-ADObject -name $WMICN -type 'msWMI-Som' -Path $WMIPath -OtherAttributes $Attr
  }
  Write-Verbose -Message 'Finished adding WMI Filters'
}

Set-DCAllowSystemOnlyChange -Set
Create-WMIFilters
Set-DCAllowSystemOnlyChange