I'm trying to display the name (just the name) of all of the installed services that contain the string "SQL". For example, I want to see
- SQLAgent$SQL2008_R2
- SQLBrowser
- SQLWriter
So I try this:
Get-WmiObject Win32_Service
This displays all of the services, but as a list.
Exit Code : 0
Name : ProtectedStorage
ProcessId : 664
StartMode : Manual
State : Running
Status : OK
Exit Code : 1077
Name : QWAVE
ProcessId : 0
StartMode : Manual
State : Stopped
Status : OK
(etc...)
This is good, but I just want to see the name. So I type:
Get-WmiObject Win32_Service | select-object Name
And I get what I expect:
sppuino开发者_运维问答tfy
SQLAgent$SQL2008_RT
SQLBrowser
SQLWriter
SSDPSRV
(etc ..)
All is good. I take the next step of filtering the names to only include SQL related ones:
Get-WmiObject Win32_Service | select-object Name | select-string -pattern 'SQL'
And now it's confusing. Here is my output:
@{Name=BcmSqlStartupSvc}
@{Name=MSOLAP$SQL2008_R2}
@{Name=MSSQL$SQL2008_R2}
(etc ...)
Why am I getting this output, instead of just the names? What should I be typing to get just the names?
You can use Get-Service instead of get-WMIObject and do it like this"
get-service sql* | select -expand name
The easiest way to achieve that is using -Filter Parameter
Get-WMIObject Win32_Service -Filter "Name LIKE '%SQL%'" |
Select -ExpandProperty Name
In case, you want to go with your code only, here is the way you can modify it:
Get-WmiObject Win32_Service | Select-Object -ExpandProperty Name |
Select-String -pattern 'SQL'
Edit: LIKE operator takes a few meta characters to support matching pattern.
[] - for range matching. For example, Name LIKE '[a-f]%' will list all services starting with any letter from a to f.
^ - not. For example, Name LIKE '[^a-f]%' will list services that do not start with any letter from a to f.
_ - matches one letter. For example, Name LIKE 'S_L%' will list services that start with S and followed by any letter.
Get-Service | Where-Object {$_.Name -match "SQL"} |Select-Object Name
加载中,请稍侯......
精彩评论