DSCResources/cWMIEventBinding/cWMIEventBinding.psm1
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 |
# Fallback message strings in en-US DATA localizedData { # same as culture = "en-US" ConvertFrom-StringData @' GettingFilterToConsumerBinding="Retreiving any avilable bindings for {0} and {1} of type {2}". BindingFound="Binding found for filter {0}, consumer {1} of type {2}". BindingNotFound="Binding found for filter {0}, consumer {1} of type {2}". GettingFilterAndConsumer="Retrieving Filter and Consumer instances for {0} and {1}". FilterAndConsumerCannotBeCreated="Filter and Consumer instances cannot be created for filter {0} and consumer {1}". CreatingFilterToConsumerBinding="Creating event binding for filter {0} and consumer {1} of type {2}". CreatedEventBinding="Created event binding for filter {0} and consumer {1} of type {2}". RemovingEventBinding="Removing event binding for filter {0} and consumer {1} of type {2}". RemovedEventBinding="Removed event binding for filter {0} and consumer {1} of type {2}". EventBindingExistsNoAction="Event binding for filter {0} and consumer {1} of type {2} exists. No action needed.". EventBindingDoesNotExistShouldCreate="Event binding for filter {0} and consumer {1} of type {2} does not exist. It will be created". EventBindingExistsShouldDelete="Event binding for filter {0} and consumer {1} of type {2} exits. It should be deleted". EventBindingDoesNotExistNoAction="Event binding for filter {0} and consumer {1} of type {2} does not exist. No action needed". '@ } if (Test-Path $PSScriptRoot\en-us) { Import-LocalizedData LocalizedData -filename WMIEventBinding.psd1 } $ConsumerHash = @{ LogFile = 'LogFileEventConsumer' EventLog = 'NTEventLogEventConsumer' CommandLine = 'CommandLineEventConsumer' Script = 'ActiveScriptEventConsumer' SMTP = 'SMTPEventConsumer' } $DeliveryQoSHash = @{ Synchronous = 0 Express = 1 } function Get-TargetResource { [CmdletBinding()] [OutputType([Hashtable])] param ( [Parameter(Mandatory)] [String] $Filter, [Parameter(Mandatory)] [String] $Consumer, [Parameter(Mandatory)] [ValidateSet('LogFile','EventLog','CommandLine','Script','SMTP')] [String] $ConsumerType ) $Configuration = @{ Filter = $Filter Consumer = $Consumer } Write-Verbose ($localizedData.GettingFilterToConsumerBinding -f $Filter, $Consumer, $ConsumerType) $Binding = Get-CimInstance -Namespace 'root\subscription' -ClassName __FilterToConsumerBinding | Where-Object { ($_.Filter.Name -eq $Filter) -and ($_.Consumer.Name -eq $Consumer) } if ($Binding) { Write-Verbose ($localizedData.BindingFound -f $Filter, $Consumer, $ConsumerType) $Configuration.Add('ConsumerType',$Binding.Consumer.CimClass.CimClassName) $Configuration.Add('Ensure','Present') } else { Write-Verbose ($localizedData.BindingNotFound -f $Filter, $Consumer, $ConsumerType) $Configuration.Add('Ensure','Absent') } $Configuration } function Set-TargetResource { [CmdletBinding()] param ( [Parameter(Mandatory)] [String] $Filter, [Parameter(Mandatory)] [String] $Consumer, [Parameter(Mandatory)] [ValidateSet('LogFile','EventLog','CommandLine','Script','SMTP')] [String] $ConsumerType, [Parameter()] [Boolean] $MaintainSecurityContext = $false, [Parameter()] [Boolean] $SlowDownProviders = $false, [Parameter()] [ValidateSet('Synchronous', 'Express')] [string] $DeliveryQoS, [Parameter()] [ValidateSet('Present','Absent')] [String] $Ensure = 'Present' ) if ($Ensure -eq 'Present') { Write-Verbose ($localizedData.GettingFilterAndConsumer -f $Filter, $Consumer) try { $FilterObject = Get-CimInstance -ClassName '__EventFilter' -Namespace 'root\subscription' -Filter "Name='${filter}'" $ConsumerObject = Get-CimInstance -ClassName $ConsumerHash[$ConsumerType] -Namespace 'root\subscription' -Filter "Name='${Consumer}'" } catch { throw ($localizedData.FilterAndConsumerCannotBeCreated -f $Filter, $Consumer, $ConsumerType) } $BinderHash = @{ Filter = [ref]$FilterObject Consumer = [ref]$ConsumerObject MaintainSecurityContext = $MaintainSecurityContext SlowDownProviders = $SlowDownProviders } if ($DeliveryQoS) { $BinderHash.Add('DeliveryQoS', [uint32]($DeliveryQoSHash[$DeliveryQoS])) } Write-Verbose ($localizedData.CreatingFilterToConsumerBinding -f $Filter, $Consumer, $ConsumerType) New-CimInstance -ClassName '__FilterToConsumerBinding' -Namespace 'root\subscription' -Property $BinderHash Write-Verbose ($localizedData.CreatedEventBinding -f $Filter, $Consumer, $ConsumerType) } else { Write-Verbose ($localizedData.RemovingEventBinding -f $Filter, $Consumer, $ConsumerType) $binding = Get-CimInstance -Namespace 'root\subscription' -ClassName __FilterToConsumerBinding | Where-Object { ($_.Filter.Name -eq $Filter) -and ($_.Consumer.Name -eq $Consumer) } Remove-CimInstance -InputObject $binding Write-Verbose ($localizedData.RemovedEventBinding -f $Filter, $Consumer, $ConsumerType) } } function Test-TargetResource { [CmdletBinding()] [OutputType([boolean])] param ( [Parameter(Mandatory)] [String] $Filter, [Parameter(Mandatory)] [String] $Consumer, [Parameter(Mandatory)] [ValidateSet('LogFile','EventLog','CommandLine','Script','SMTP')] [String] $ConsumerType, [Parameter()] [Boolean] $MaintainSecurityContext = $false, [Parameter()] [Boolean] $SlowDownProviders = $false, [Parameter()] [ValidateSet('Synchronous', 'Express')] [string] $DeliveryQoS, [Parameter()] [ValidateSet('Present','Absent')] [String] $Ensure = 'Present' ) Write-Verbose ($localizedData.GettingFilterToConsumerBinding -f $Filter, $Consumer, $ConsumerType) $binding = Get-CimInstance -Namespace 'root\subscription' -ClassName __FilterToConsumerBinding | Where-Object { ($_.Filter.Name -eq $Filter) -and ($_.Consumer.Name -eq $Consumer) } if ($Ensure -eq 'Present') { if ($binding) { Write-Verbose ($localizedData.EventBindingExistsNoAction -f $Filter, $Consumer, $ConsumerType) return $true } else { Write-Verbose ($localizedData.EventBindingDoesNotExistShouldCreate -f $Filter, $Consumer, $ConsumerType) return $false } } else { if ($binding) { Write-Verbose ($localizedData.EventBindingExistsShouldDelete -f $Filter, $Consumer, $ConsumerType) return $false } else { Write-Verbose ($localizedData.EventBindingDoesNotExistNoAction -f $Filter, $Consumer, $ConsumerType) return $true } } } |