I have a string array $Exclude which I want to put into a filter in a Get-WMIObject cmdlet. I have added it at the end but it does not work.
How can I filter out the services that are listed in that array?
$ServicesToExclude = "RemoteRegistry,CpqNicMgmt"
$Exclude = $ServicesToExclude.split(",")
$Services = Get-WmiObject -Class Win32_Service -Filter {State != 'Running' and StartMode = 'Auto' and Name -ne $Exclude}
$Result = foreach ($Service in $Services.Name)
{
Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\$Service" |
Where-Object {$_.Start -eq 2 -and $_.DelayedAutoStart -ne 1}|
Select-Object -Property @{label='ServiceName';expression={$_.PSChildName}} |
get-Service
}
If ($Result.count -gt 0){
$Displayname = $Result.displayname
[string] $Line = "`n-----------------------------------------"
$Api.LogScriptEvent( 'Stopped_Auto_Services.ps1',1234,4,"`nStopped Automatic Services$Line `n$($Displayname)")