Last active
July 18, 2023 11:17
-
-
Save zbalkan/f79ebf1363e727f9959543d4d40e712c to your computer and use it in GitHub Desktop.
Updated version of https://github.com/darkoperator/powershell_scripts/blob/master/install-wmifilters.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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 ActiveDirectory | |
Function Set-DCAllowSystemOnlyChange | |
{ | |
param ([switch]$Set) | |
if ($Set) | |
{ | |
Write-Verbose "Checking is registry key is set to allow changes to AD System Only Attributes is set." | |
$ntds_vals = (Get-Item HKLM:\System\CurrentControlSet\Services\NTDS\Parameters).GetValueNames() | |
if ( $ntds_vals -eq "Allow System Only Change") | |
{ | |
$kval = Get-ItemProperty HKLM:\System\CurrentControlSet\Services\NTDS\Parameters -name "Allow System Only Change" | |
if ($kval -eq "1") | |
{ | |
Write-Verbose "Allow System Only Change key is already set" | |
} | |
else | |
{ | |
Write-Verbose "Allow System Only Change key is not set" | |
Write-Verbose "Creating key and setting value to 1" | |
Set-ItemProperty HKLM:\System\CurrentControlSet\Services\NTDS\Parameters -name "Allow System Only Change" -Value 0 | Out-Null | |
} | |
} | |
else | |
{ | |
New-ItemProperty HKLM:\System\CurrentControlSet\Services\NTDS\Parameters -name "Allow System Only Change" -Value 1 -PropertyType "DWord" | Out-Null | |
} | |
} | |
else | |
{ | |
$ntds_vals = (Get-Item HKLM:\System\CurrentControlSet\Services\NTDS\Parameters).GetValueNames() | |
if ( $ntds_vals -eq "Allow System Only Change") | |
{ | |
Write-Verbose "Disabling Allow System Only Change Attributes on server" | |
Set-ItemProperty HKLM:\System\CurrentControlSet\Services\NTDS\Parameters -name "Allow System Only Change" -Value 0 | Out-Null | |
} | |
} | |
} | |
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 Model FROM Win32_ComputerSystem WHERE Model = "Virtual Machine"', | |
'Microsoft Hyper-V 2.0 AND 3.0'), | |
('VMware Virtual Machines', | |
'SELECT Model FROM Win32_ComputerSystem WHERE Model LIKE "VMware%"', | |
'VMware Fusion, WORkstation AND ESXi'), | |
('Workstation 32-bit', | |
'SELECT ProductType FROM WIN32_OperatingSystem WHERE ProductType=1 SELECT AddressWidth FROM Win32_Processor WHERE AddressWidth = "32"', | |
''), | |
('Workstation 64-bit', | |
'SELECT ProductType FROM WIN32_OperatingSystem WHERE ProductType=1 SELECT AddressWidth FROM Win32_Processor WHERE AddressWidth = "64"', | |
''), | |
('Workstations (Laptop + Desktop)', | |
'SELECT ProductType FROM Win32_OperatingSystem WHERE ProductType = "1"', | |
''), | |
('Domain Controllers', | |
'SELECT ProductType FROM Win32_OperatingSystem WHERE ProductType = "2"', | |
''), | |
('Servers', | |
'SELECT ProductType FROM Win32_OperatingSystem WHERE ProductType = "3"', | |
''), | |
('Desktop', | |
'SELECT ProductType FROM Win32_OperatingSystem WHERE PCSystemType = "1" OR PCSystemType = "3"', | |
''), | |
('Laptop', | |
'SELECT PCSystemType FROM Win32_OperatingSystem WHERE PCSystemType = "1" OR PCSystemType = "3"', | |
''), | |
('Windows 7', | |
'SELECT Version, ProductType FROM Win32_OperatingSystem WHERE Version LIKE "6.1%" AND ProductType = "1"', | |
''), | |
('Windows 8', | |
'SELECT Version, ProductType FROM Win32_OperatingSystem WHERE Version LIKE "6.2%" AND ProductType = "1"', | |
''), | |
('Windows 8.1', | |
'SELECT Version, ProductType FROM Win32_OperatingSystem WHERE Version LIKE "6.3%" AND ProductType = "1"', | |
''), | |
('Windows 10', | |
'SELECT Version, ProductType FROM Win32_OperatingSystem WHERE Version LIKE "10.%" AND ProductType = "1"', | |
''), | |
('Windows 11', | |
'SELECT Version, ProductType FROM Win32_OperatingSystem WHERE BuildNumber LIKE "22000" AND ProductType = "1"', | |
''), | |
('Windows Server 2003', | |
'SELECT Version, ProductType FROM Win32_OperatingSystem WHERE Version LIKE "5.2%" AND ProductType <> "1"', | |
''), | |
('Windows Server 2008', | |
'SELECT Version, ProductType FROM Win32_OperatingSystem WHERE Version LIKE "6.0%" AND ProductType <> "1"', | |
''), | |
('Windows Server 2008 R2', | |
'SELECT Version, ProductType FROM Win32_OperatingSystem WHERE Version LIKE "6.1%" AND ProductType <> "1"', | |
''), | |
('Windows Server 2012', | |
'SELECT Version, ProductType FROM Win32_OperatingSystem WHERE Version LIKE "6.2%" AND ProductType <> "1"', | |
''), | |
('Windows Server 2012 R2', | |
'SELECT Version, ProductType FROM Win32_OperatingSystem WHERE Version LIKE "6.3%" AND ProductType <> "1"', | |
''), | |
('Windows Server 2016', | |
'SELECT Version, ProductType FROM Win32_OperatingSystem WHERE Version LIKE "10%" AND ProductType <> "1"', | |
''), | |
('Windows Server version 1709-1803-1809', | |
'SELECT BuildNumber, ProductType FROM Win32_OperatingSystem WHERE BuildNumber LIKE "16299" AND ProductType <> "1"', | |
''), | |
('Windows Server 2019', | |
'SELECT BuildNumber, ProductType FROM Win32_OperatingSystem WHERE BuildNumber LIKE "17763" AND ProductType <> "1"', | |
''), | |
('Windows Server version 1903', | |
'SELECT BuildNumber, ProductType FROM Win32_OperatingSystem WHERE BuildNumber LIKE "18362" AND ProductType <> "1"', | |
''), | |
('Windows Server version 1909', | |
'SELECT BuildNumber, ProductType FROM Win32_OperatingSystem WHERE BuildNumber LIKE "18363" AND ProductType <> "1"', | |
''), | |
('Windows Server version 2004', | |
'SELECT BuildNumber, ProductType FROM Win32_OperatingSystem WHERE BuildNumber LIKE "19041" AND ProductType <> "1"', | |
''), | |
('Windows Server version 20H2', | |
'SELECT BuildNumber, ProductType FROM Win32_OperatingSystem WHERE BuildNumber LIKE "19042" AND ProductType <> "1"', | |
''), | |
('Windows Server 2022', | |
'SELECT BuildNumber, ProductType FROM Win32_OperatingSystem WHERE BuildNumber LIKE "20348" AND ProductType <> "1"', | |
''), | |
('Time Server', | |
'SELECT DomainRole FROM Win32_ComputerSystem WHERE DomainRole = "5"', | |
'') | |
) | |
$defaultNamingContext = (get-adrootdse).defaultnamingcontext | |
$configurationNamingContext = (get-adrootdse).configurationNamingContext | |
$msWMIAuthor = "Administrator@" + [System.DirectoryServices.ActiveDirectory.Domain]::getcurrentdomain().name | |
Write-Verbose "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 "Adding WMI Filter for: $msWMIName" | |
if ($null -eq (Get-ADObject -SearchBase $WMIPath -Filter { DisplayName -like $msWMIName })) | |
{ | |
New-ADObject -name $WMICN -type "msWMI-Som" -Path $WMIPath -OtherAttributes $Attr -ErrorAction Continue -DisplayName $msWMIName -Description $msWMIParm1 | Out-Null | |
} | |
} | |
Write-Verbose "Finished adding WMI Filters" | |
} | |
Set-DCAllowSystemOnlyChange -Set | |
Create-WMIFilters | |
Set-DCAllowSystemOnlyChange |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added:
Removed: