-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
135 additions
and
82 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,35 @@ | ||
function ConvertTo-WmiFilter { | ||
<# | ||
.Synopsis | ||
.DESCRIPTION | ||
.EXAMPLE | ||
ConvertTo-WmiFilter | ||
.INPUTS | ||
.NOTES | ||
Version: 1.0 | ||
DateModified: 25/Mar/2014 | ||
LasModifiedBy: Vicente Rodriguez Eguibar | ||
[email protected] | ||
Eguibar Information Technology S.L. | ||
http://www.eguibarit.com | ||
.SYNOPSIS | ||
Converts an Active Directory object to a WMI filter object. | ||
.DESCRIPTION | ||
This function takes an Active Directory object, retrieves its corresponding WMI filter from the Group Policy domain, | ||
and returns it as an object of type "Microsoft.GroupPolicy.WmiFilter". The function includes error handling and retry | ||
logic to address Active Directory replication delays. | ||
.PARAMETER ADObject | ||
An array of ADObject instances representing the Active Directory objects to convert to WMI filters. | ||
.EXAMPLE | ||
ConvertTo-WmiFilter -ADObject $adObjects | ||
.INPUTS | ||
[Microsoft.ActiveDirectory.Management.ADObject[]] - An array of Active Directory objects. | ||
.OUTPUTS | ||
[Microsoft.GroupPolicy.WmiFilter] - The converted WMI filter object. | ||
.NOTES | ||
Version: 1.1 | ||
DateModified: 31/May/2024 | ||
LastModifiedBy: Vicente Rodriguez Eguibar | ||
[email protected] | ||
Eguibar Information Technology S.L. | ||
http://www.eguibarit.com | ||
#> | ||
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] | ||
|
||
Param ( | ||
[Microsoft.ActiveDirectory.Management.ADObject[]] $ADObject | ||
) | ||
|
@@ -25,45 +41,65 @@ function ConvertTo-WmiFilter { | |
Write-Verbose -Message (' Starting: {0}' -f $MyInvocation.Mycommand) | ||
Write-Verbose -Message ('Parameters used by the function... {0}' -f (Get-FunctionDisplay $PsBoundParameters -Verbose:$False)) | ||
|
||
Import-Module -Name 'GroupPolicy' -Force -Verbose:$false | ||
|
||
############################## | ||
# Variables Definition | ||
|
||
} #end Begin | ||
|
||
Process { | ||
# The concept of this function has been taken directly from the GPWmiFilter.psm1 module | ||
# written by Bin Yi from Microsoft. I have modified it to allow for the challenges of | ||
# Active Directory replication. It will return the WMI filter as an object of type | ||
# "Microsoft.GroupPolicy.WmiFilter". | ||
$gpDomain = New-Object -TypeName Microsoft.GroupPolicy.GPDomain | ||
|
||
$ADObject | ForEach-Object { | ||
$path = 'MSFT_SomFilter.Domain="' + $gpDomain.DomainName + '",ID="' + $_.Name + '"' | ||
$filter = $null | ||
try { | ||
$filter = $gpDomain.GetWmiFilter($path) | ||
} catch { | ||
Write-Error -Message 'The WMI filter could not be found.' | ||
Get-CurrentErrorToDisplay -CurrentError $error[0] | ||
} | ||
if ($filter) { | ||
[Guid]$Guid = $_.Name.Substring(1, $_.Name.Length - 2) | ||
$filter | | ||
Add-Member -MemberType NoteProperty -Name Guid -Value $Guid -PassThru | | ||
Add-Member -MemberType NoteProperty -Name Content -Value $_.'msWMI-Parm2' -PassThru | ||
} #end Begin | ||
|
||
Process { | ||
|
||
# Iterate each ADObject | ||
foreach ($item in $ADObject) { | ||
if ($PSCmdlet.ShouldProcess($item.Name, 'Convert to WMI Filter')) { | ||
$path = 'MSFT_SomFilter.Domain="{0}",ID="{1}"' -f $gpDomain.DomainName, $item.Name | ||
$filter = $null | ||
$attempt = 0 | ||
$maxAttempts = 4 | ||
|
||
do { | ||
try { | ||
$filter = $gpDomain.GetWmiFilter($path) | ||
} catch { | ||
Write-Error -Message 'The WMI filter could not be found.' | ||
Get-CurrentErrorToDisplay -CurrentError $error[0] | ||
throw | ||
} #end Try-Catch | ||
|
||
if ($filter) { | ||
[Guid]$Guid = $item.Name.Substring(1, $item.Name.Length - 2) | ||
|
||
$filter | Add-Member -MemberType NoteProperty -Name Guid -Value $Guid -PassThru | ||
$filter | Add-Member -MemberType NoteProperty -Name Content -Value $item.'msWMI-Parm2' -PassThru | ||
|
||
break | ||
} else { | ||
Write-Warning -Message 'Waiting 5 seconds for Active Directory replication to complete.' | ||
Start-Sleep -Seconds 5 | ||
Write-Warning -Message 'Trying again to retrieve the WMI filter.' | ||
ConvertTo-WmiFilter $ADObject | ||
} | ||
} | ||
} #end Process | ||
|
||
End { | ||
Write-Verbose -Message "Function $($MyInvocation.InvocationName) finished converting the WMI filter." | ||
Write-Verbose -Message '' | ||
Write-Verbose -Message '-------------------------------------------------------------------------------' | ||
Write-Verbose -Message '' | ||
} #end Function | ||
} | ||
$attempt++ | ||
if ($attempt -lt $maxAttempts) { | ||
Write-Warning -Message 'Waiting 5 seconds for Active Directory replication to complete.' | ||
Start-Sleep -Seconds 5 | ||
Write-Warning -Message 'Trying again to retrieve the WMI filter.' | ||
} else { | ||
Write-Error -Message 'Max attempts reached. Could not retrieve the WMI filter.' | ||
break | ||
} #end If-Else | ||
} #end If-Else | ||
} while ($attempt -lt $maxAttempts) | ||
} #end ShouldProcess | ||
} #end Foreach | ||
} #end Process | ||
|
||
End { | ||
Write-Verbose -Message "Function $($MyInvocation.InvocationName) finished converting the WMI filter." | ||
Write-Verbose -Message '' | ||
Write-Verbose -Message '-------------------------------------------------------------------------------' | ||
Write-Verbose -Message '' | ||
} #end Function | ||
} |
Oops, something went wrong.