调用Get-Service时过滤服务

Bla*_*man 6 powershell

我过去做过这个,并且记不起正确的命令(我想我是在使用instring或soemthign?)

我想列出所有运行的Windows服务,其中包含"sql"一词.

列出所有Windows服务是:

Get-Service
Run Code Online (Sandbox Code Playgroud)

是否有一个instring函数来执行此操作?

Ste*_*ski 19

Get-Service -Name *sql*
Run Code Online (Sandbox Code Playgroud)

更长的选择是:

Get-Service | where-object {$_.name -like '*sql*'}
Run Code Online (Sandbox Code Playgroud)

许多cmdlet提供内置过滤和支持通配符.如果您查看帮助文件(Get-Help Get-Service -full),您将看到

 -name <string[]>
     Specifies the service names of services to be retrieved. Wildcards are
     permitted. By default, Get-Service gets all of the services on the comp
     uter.

     Required?                    false
     Position?                    1
     Default value                *
     Accept pipeline input?       true (ByValue, ByPropertyName)
     Accept wildcard characters?  true
Run Code Online (Sandbox Code Playgroud)

通常,如果将过滤内置到cmdlet中,那么这是首选方法,因为它通常更快,更高效.
在这种情况下,可能没有太多的性能优势,但在V2中,您可以从远程计算机中提取服务并进行过滤,这将是首选方法(将较少的数据发送回调用计算机).


小智 5

您可以获得正在运行的所有服务,并且包含单词sql。

 Get-Service | Where-Object {$_.Status -eq "Running"} | Where-Object {$_.Name -like "*sql*"}
Run Code Online (Sandbox Code Playgroud)

如果您需要更多信息,请参阅此内容(区别不大) http://nisanthkv.blog.com/2012/06/29/get-services-using-powershell

希望能帮助到你...