Hector
Hector

Reputation: 1

Calling an Array in PowerShell

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)")

Upvotes: 0

Views: 243

Answers (2)

boeprox
boeprox

Reputation: 1868

WMI queries do not work well with arrays and need to be done a different way. If you want to keep the filtering on the server side, you can do some work prior to running the command by creating a filter string as shown here:

$Exclude = "RemoteRegistry","CpqNicMgmt"
$StringBuilder = New-Object System.Text.StringBuilder
[void]$StringBuilder.Append("State != 'Running' AND StartMode = 'Auto' AND ")
[void]$StringBuilder.Append("($(($Exclude -replace '^(.*)$','Name != "$1"') -join ' AND '))")
$Query = $StringBuilder.ToString()
$Services = Get-WmiObject -Class Win32_Service -Filter $Query

There may be better ways to accomplish this, but this was the first thing that I could think of to accomplish the goal of your question.

Upvotes: 0

Vesper
Vesper

Reputation: 18747

Filtering an array out of a list is not done on the WMI side. Instead, you should use Where-Object to filter out those services which name is contained in $Exclude.

$Services = Get-WmiObject -Class Win32_Service -Filter {State != 'Running' and StartMode = 'Auto'} |
Where-Object {$Exclude -notcontains $_.Name}

Upvotes: 2

Related Questions