从“wmic service get”命令过滤微软服务

Filter microsoft service from `wmic service get` command

我正在使用 wmic 枚举已安装的服务:

wmic service get

是否可以对它隐藏微软服务?

查询 returns 以下字段,其中 none 似乎有帮助:

AcceptPause AcceptStop Caption CheckPoint CreationClassName Description DesktopInteract DisplayName ErrorControl ExitCode InstallDate Name PathName ProcessId ServiceSpecificExitCode ServiceType Started StartMode StartName State Status SystemCreationClassName SystemName TagId WaitHint

这个命令可以解决问题:

WMIC service where "Not PathName like '%Micro%' AND Not PathName like '%Windows%'" get Name,DisplayName,PathName,State,Status

您也可以使用 Powershell 脚本来完成:

在这里参考这个答案How to filter Microsoft Service using PowerShell?

您可以使用 powershell 脚本过滤 非 Microsoft 服务 :

$services = Get-WmiObject Win32_Service -Property Name,DisplayName,PathName | Select Name, DisplayName,PathName
$serviceList = New-Object System.Collections.ArrayList
foreach ($service in $services) {
  Try {
    $path = $service.Pathname.tostring().replace('"','')
    $cri = ([System.Diagnostics.FileVersionInfo]::GetVersionInfo($path)).legalcopyright
    if ($cri -notlike "*Microsoft*") {
      $serviceList += $service
    }
  } catch {}
}
$serviceList

编辑:2019 年 2 月 11 日 Vbscript 版本

这是另一个使用 vbscript 的版本,以便使用 Excel 导出输出结果。

只需将下面的代码复制并粘贴为 Non-Microsoft-Services.vbs 并双击执行。

Option Explicit
' Non-Microsoft-Services.vbs
Dim objExcel,strComputer,objWMIService
Dim State,colServices,x,objService,objWorksheet,objWorkbook
' Create a new and blank spreadsheet:
Set objExcel = CreateObject("Excel.Application")
Set objWorkBook = objExcel.WorkBooks.Add
objExcel.Visible = True

Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Name = "Services Non-Microsoft"
objWorksheet.Tab.ColorIndex = 3

' Format the cell A1 and add the text: Service Name
objExcel.Cells(1, 1).Value = "Service Name"
objExcel.Cells(1, 1).Font.Bold = TRUE
objExcel.Cells(1, 1).Interior.ColorIndex = 43
objExcel.Cells(1, 1).Font.ColorIndex = 2
' Format the cell A2 and add the text: Display Name
objExcel.Cells(1, 2).Value = "Display Name"
objExcel.Cells(1, 2).Font.Bold = TRUE
objExcel.Cells(1, 2).Interior.ColorIndex = 43
objExcel.Cells(1, 2).Font.ColorIndex = 2
'*************************************************
' Format the cell A3 and add the text: State
objExcel.Cells(1, 3).Value = "State"
objExcel.Cells(1, 3).Font.Bold = TRUE
objExcel.Cells(1, 3).Interior.ColorIndex = 43
objExcel.Cells(1, 3).Font.ColorIndex = 2
'*************************************************
' Format the cell A4 and add the text: Executable Path
objExcel.Cells(1, 4).Value = "Executable Path"
objExcel.Cells(1, 4).Font.Bold = TRUE
objExcel.Cells(1, 4).Interior.ColorIndex = 43
objExcel.Cells(1, 4).Font.ColorIndex = 2
'*************************************************
' Format the cell A5 and add the text: Description
objExcel.Cells(1, 5).Value = "Description"
objExcel.Cells(1, 5).Font.Bold = TRUE
objExcel.Cells(1, 5).Interior.ColorIndex = 43
objExcel.Cells(1, 5).Font.ColorIndex = 2
' Find the Non-Microsoft Windows services on this computer
strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery("Select * From Win32_Service where Not PathName like '%Micro%' AND Not PathName like '%Windows%'")
' Write each service to Excel, starting in A2
x = 1
For Each objService in colServices
    x = x + 1
    objExcel.Cells(x, 1) = objService.Name
    objExcel.Cells(x, 2) = objService.DisplayName
    objExcel.Cells(x, 3) = objService.State
    objExcel.Cells(x, 4) = objService.PathName
    objExcel.Cells(x, 5) = objService.Description
    State = objService.Started
    If State Then 
        Cellule x,3,"Running"
        objExcel.Cells(x, 1).Font.ColorIndex = 10
        objExcel.Cells(x, 2).Font.ColorIndex = 10
        objExcel.Cells(x, 3).Font.ColorIndex = 10
        objExcel.Cells(x, 4).Font.ColorIndex = 10
        objExcel.Cells(x, 5).Font.ColorIndex = 10
    ELSE
        Cellule X,3,"Stopped"
        objExcel.Cells(x, 1).Font.ColorIndex = 3
        objExcel.Cells(x, 2).Font.ColorIndex = 3
        objExcel.Cells(x, 3).Font.ColorIndex = 3
        objExcel.Cells(x, 4).Font.ColorIndex = 3
        objExcel.Cells(x, 5).Font.ColorIndex = 3
    end if
Next

objExcel.Columns("A:A").EntireColumn.AutoFit
objExcel.Columns("B:B").EntireColumn.AutoFit
objExcel.Columns("C:C").EntireColumn.AutoFit
objExcel.Columns("D:D").EntireColumn.AutoFit
objExcel.Columns("E:E").EntireColumn.AutoFit

Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
Dim Network : Set Network = CreateObject("WScript.Network")
Dim Computer : Computer = Network.ComputerName
Dim xlVer,objXL
Set objXL = CreateObject("Excel.Application") 
' Check Excel Version (12.0 = 2007)
xlVer = Split(objXL.Version,".")(0) 
If xlVer >= "12" Then
    objExcel.ActiveWorkbook.SaveAs fso.GetAbsolutePathName(".") & "\Non-Microsoft-Services_" & Computer & ".xlsx"
    objExcel.DisplayAlerts = True
' 56 = Excel 97-2003
' Voir la page http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.xlfileformat.aspx
Else
    objExcel.ActiveWorkbook.SaveAs fso.GetAbsolutePathName(".") & "\Non-Microsoft-Services_" & Computer & ".xls",56
    objExcel.DisplayAlerts = True
End If
'--------------------------------------------------------------------
Sub Cellule(X,NC,chaine)
    objExcel.Cells(X,NC).Value = Chaine
End Sub
'--------------------------------------------------------------------
'Function to determine the current directory
Function GetPath()
    Dim path
    path = WScript.ScriptFullName
    GetPath = Left(path, InStrRev(path, "\"))
End Function
'--------------------------------------------------------------------

这是我用这个 vbscript 得到的截图: