I am trying to build a generic function that receives a list of services and executes the following snippet. If I send in one value, it works, if I try to send in more than one value, it fails.
Is there a better way to go about this or am I missing something simple?
cls
$host_name = "."
#works
$ary_param = "sql"
#fails
#$ary_param = "sql,ftp"
$services = @($ary_param)
$colItems = Get-WmiObject -Class Win32_Service -ComputerName $host_name |
Select-Object Name, DisplayName, State, ProcessID, StartMode, StartName |
Where-Object{$_.Name -like '*' + $services + '*'}
$ServiceInfo = @()
foreach ($objItem in $colItems)
{
$tempServiceInfo = "" | Select Name, DisplayName, State, ProcessID, PriorityClass, StartMode, StartName
[string]$tempServiceInfo.Name = $objItem.Name.ToString()
[string]$tempServiceInfo.DisplayName = $objItem.DisplayName.ToString()
[string]$tempServiceInfo.State = $objItem.State.ToString()
[string]$tempServiceInfo.ProcessID = $objItem.ProcessID.ToString()
[string]$tempServiceInfo.StartMode = $objItem.StartMode.ToString()
[string]$tempServiceInfo.StartName = $objItem.StartName.ToString()
$svc_pid = $objItem.ProcessID
$priority_class = Get-Process -ComputerName $host_name | Select Id, PriorityClass | Where-Object{$_.Id -eq $svc_pid }
[string]$tempServiceInfo.PriorityClass = $priority_class.PriorityClass
$ServiceInfo += $tempServiceInfo
}
$ServiceInfo | Format-Table
Where-Objectfilter?