[Solved] How to filter all non-Microsoft services with WMIC and redirect the result to a text file ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

[Solved] How to filter all non-Microsoft services with WMIC and redirect the result to a text file ?

#1 Post by Hackoo » 31 Oct 2019 05:02

Hi :wink:

Code: Select all

WMIC service where "Not PathName like '%Micro%' AND Not PathName like '%Windows%'" get Name, DisplayName, PathName, Status
So this command works 5/5 on the console but the problem is when I want to redirect the result to a text file, I get all the instances without any filter? while I see in the console the command gives me the expected result with the filter?
Last edited by Hackoo on 31 Oct 2019 05:21, edited 1 time in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to filter all non-Microsoft services with WMIC and redirect the result to a text file ?

#2 Post by aGerman » 31 Oct 2019 05:16

Can't reproduce your result. When I run ...

Code: Select all

C:\Users\steffen\Desktop>>"srvcs.txt" WMIC service where "Not PathName like '%Micro%' AND Not PathName like '%Windows%'" get Name, DisplayName, PathName, Status
... (contains my cmd prompt) I get ...

Code: Select all

DisplayName                                             Name                                                    PathName                                                                      Status  
ASUS HID Access Service                                 AsHidService                                            "C:\Program Files (x86)\ASUS\ATK Package\ATK Hotkey\AsHidSrv.exe"             OK      
ASLDR Service                                           ASLDRService                                            "C:\Program Files (x86)\ASUS\ATK Package\ATK Hotkey\AsLdrSrv.exe"             OK      
Device Activation Service                               DevActSvc                                               C:\Program Files (x86)\ASUS\ASUS Device Activation\DevActSvc.exe              OK      
Intel(R) Capability Licensing Service TCP IP Interface  Intel(R) Capability Licensing Service TCP IP Interface  "C:\Program Files\Intel\TXE Components\TCS\SocketHeciServer.exe"              OK      
Intel(R) Security Assist                                Intel(R) Security Assist                                "C:\Program Files (x86)\Intel\Intel(R) Security Assist\isa.exe"               OK      
Intel(R) Security Assist Helper                         isaHelperSvc                                            "C:\Program Files (x86)\Intel\Intel(R) Security Assist\isaHelperService.exe"  OK      
Intel(R) Dynamic Application Loader Host Interface      jhi_service                                             "C:\Program Files (x86)\Intel\TXE Components\DAL\jhi_service.exe"             OK      
RTK IIS Codec Service 64                                RtkI2SCodec64                                           "C:\Program Files\Realtek\Audio\IIS\RtkI2SAudioService64.exe"                 OK      
... which is for sure still filtered :?
What exactly did you try?

Steffen

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: How to filter all non-Microsoft services with WMIC and redirect the result to a text file ?

#3 Post by Hackoo » 31 Oct 2019 05:18

Sorry My bad i got it ,
I forgot to add the percent % in the batch file, so with a batch file works 5/5

Code: Select all

@echo off
WMIC service where "Not PathName like '%%Micro%%' AND Not PathName like '%%Windows%%'" get Name, DisplayName, PathName, Status >"%computername%.txt"
Start "" "%computername%.txt" & exit
:mrgreen:
I got something like this :

Code: Select all

DisplayName                         Name                          PathName                                                                               Status  
Agent                               Agent                         "C:\Users\Hackoo\Downloads\Compressed\AgentSetup\%ProgramFiles(x86)%\Agent\Agent.exe"  OK      
Disc Soft Lite Bus Service          Disc Soft Lite Bus Service    "C:\Program Files\DAEMON Tools Lite\DiscSoftBusServiceLite.exe"                        OK      
ESET HTTP Server                    EHttpSrv                      "C:\Program Files\ESET\ESET Endpoint Security\ehttpsrv.exe"                            OK      
ESET Service                        ekrn                          "C:\Program Files\ESET\ESET Endpoint Security\ekrn.exe"                                OK      
ESET SHA Service                    eshasrv                       "C:\Program Files\ESET\ESET Endpoint Security\eshasrv.exe"                             OK      
Foxit Reader Service                FoxitReaderService            "C:\Program Files\Foxit Software\Foxit Reader\FoxitConnectedPDFService.exe"            OK      
Google Chrome Elevation Service     GoogleChromeElevationService  "C:\Program Files\Google\Chrome\Application\77.0.3865.120\elevation_service.exe"       OK      
Service Google Update (gupdate)     gupdate                       "C:\Program Files\Google\Update\GoogleUpdate.exe" /svc                                 OK      
Service Google Update (gupdatem)    gupdatem                      "C:\Program Files\Google\Update\GoogleUpdate.exe" /medsvc                              OK      
HMA! Pro VPN                        HmaProVpn                     "C:\Program Files\HMA! Pro VPN\VpnSvc.exe"                                             OK      
HuaweiHiSuiteService.exe            HuaweiHiSuiteService.exe      "C:\Program Files\HiSuite\HandSetService\HuaweiHiSuiteService.exe" -/service           OK      
IObit Uninstaller Service           IObitUnSvr                    C:\Program Files\IObit\IObit Uninstaller\IUService.exe                                 OK      
Ivideon Video Surveillance Service  ivideon_video_service         "C:\Program Files\Ivideon\IvideonServer\videoserver.exe" --service run                 OK      
Malware Defender Service            MalwareDefenderService        c:\program files\malware defender\mdservice.exe                                        OK      
Mozilla Maintenance Service         MozillaMaintenance            "C:\Program Files\Mozilla Maintenance Service\maintenanceservice.exe"                  OK      
QFX Software Update Service         QFXUpdateService              C:\Program Files\KeyScrambler\QFXUpdateService.exe                                     OK      
Sandboxie Service                   SbieSvc                       "C:\Program Files\Sandboxie\SbieSvc.exe"                                               OK      
Windows Remediation Service         sedsvc                        "C:\Program Files\rempl\sedsvc.exe"                                                    OK      
SynTPEnh Caller Service             SynTPEnhService               "C:\Program Files\Synaptics\SynTP\SynTPEnhService.exe"                                 OK      
System Explorer Service             SystemExplorerHelpService     C:\Program Files\System Explorer\service\SystemExplorerService.exe                     OK      

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: [Solved] How to filter all non-Microsoft services with WMIC and redirect the result to a text file ?

#4 Post by Hackoo » 01 Nov 2019 23:13

Hi :wink:
Here is another version using vbscript in order to export the output results with Excel.
Non-Microsoft-Services.vbs

Code: Select all

Option Explicit
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
'--------------------------------------------------------------------
Image

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: [Solved] How to filter all non-Microsoft services with WMIC and redirect the result to a text file ?

#5 Post by aGerman » 05 Nov 2019 13:46

Speaking of other scripts ... Pretty much the same thing as HTA in case no MS Office installed:

Code: Select all

<html><head><title>Non-Microsoft Services</title><meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta http-equiv="x-ua-compatible" content="IE=9">
<style type="text/css">
  body {font: 12px Verdana, sans-serif;}
  table, thead {border: 3px solid #aaaaaa; border-collapse: collapse;}
  tr, td, th {padding: 5px; border: 2px solid #aaaaaa;}
  th {background-color: #555555; color: #ffffff;}
  tr {background-color: #fffbf0;}
  tr:nth-child(odd) {background-color: #e4ebf2;}
  td {white-space: pre-wrap;}
</style>
<script type="text/javascript">
  function generateTable() {
    var escObj = document.createElement('textarea'),
        escForHTML = function(str) {escObj.textContent = str; return escObj.innerHTML;},
        strHtml = '<table><thead><tr><th>Service Name</th><th>Display Name</th><th>State</th><th>Executable Path</th><th>Description</th></tr></thead><tbody>',
        objWMIService = (new ActiveXObject('WbemScripting.SWbemLocator')).ConnectServer('.', 'root\\cimv2'),
        colServices = new Enumerator(objWMIService.ExecQuery('SELECT * FROM Win32_Service WHERE NOT PathName LIKE \'%Micro%\' AND NOT PathName LIKE \'%Windows%\''));
    for (colServices.moveFirst(); !colServices.atEnd(); colServices.moveNext())
      strHtml += '<tr style="color: ' + ((colServices.item().State == 'Running') ? 'DarkGreen' : 'DarkRed') + '"><td>' + escForHTML(colServices.item().Name) +
                 '</td><td>' + escForHTML(colServices.item().DisplayName) + '</td><td>' + escForHTML(colServices.item().State) + '</td><td>' +
                 escForHTML(colServices.item().PathName) + '</td><td>' + escForHTML(colServices.item().Description) + '</td></tr>';
    document.getElementById('bodyNode').innerHTML = strHtml + '</tbody></table>';
  }
</script>
<HTA:APPLICATION windowState="maximize"></head><body id="bodyNode" onload="generateTable()"></body></html>
Non-Microsoft Services.hta.png
Non-Microsoft Services.hta.png (59 KiB) Viewed 9820 times
Steffen

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: [Solved] How to filter all non-Microsoft services with WMIC and redirect the result to a text file ?

#6 Post by Hackoo » 05 Nov 2019 18:30

aGerman wrote:
05 Nov 2019 13:46
Speaking of other scripts ... Pretty much the same thing as HTA in case no MS Office installed:

Code: Select all

<html><head><title>Non-Microsoft Services</title><meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta http-equiv="x-ua-compatible" content="IE=9">
<style type="text/css">
  body {font: 12px Verdana, sans-serif;}
  table, thead {border: 3px solid #aaaaaa; border-collapse: collapse;}
  tr, td, th {padding: 5px; border: 2px solid #aaaaaa;}
  th {background-color: #555555; color: #ffffff;}
  tr {background-color: #fffbf0;}
  tr:nth-child(odd) {background-color: #e4ebf2;}
  td {white-space: pre-wrap;}
</style>
<script type="text/javascript">
  function generateTable() {
    var escObj = document.createElement('textarea'),
        escForHTML = function(str) {escObj.textContent = str; return escObj.innerHTML;},
        strHtml = '<table><thead><tr><th>Service Name</th><th>Display Name</th><th>State</th><th>Executable Path</th><th>Description</th></tr></thead><tbody>',
        objWMIService = (new ActiveXObject('WbemScripting.SWbemLocator')).ConnectServer('.', 'root\\cimv2'),
        colServices = new Enumerator(objWMIService.ExecQuery('SELECT * FROM Win32_Service WHERE NOT PathName LIKE \'%Micro%\' AND NOT PathName LIKE \'%Windows%\''));
    for (colServices.moveFirst(); !colServices.atEnd(); colServices.moveNext())
      strHtml += '<tr style="color: ' + ((colServices.item().State == 'Running') ? 'DarkGreen' : 'DarkRed') + '"><td>' + escForHTML(colServices.item().Name) +
                 '</td><td>' + escForHTML(colServices.item().DisplayName) + '</td><td>' + escForHTML(colServices.item().State) + '</td><td>' +
                 escForHTML(colServices.item().PathName) + '</td><td>' + escForHTML(colServices.item().Description) + '</td></tr>';
    document.getElementById('bodyNode').innerHTML = strHtml + '</tbody></table>';
  }
</script>
<HTA:APPLICATION windowState="maximize"></head><body id="bodyNode" onload="generateTable()"></body></html>
Non-Microsoft Services.hta.png
Steffen
Very Nice HTA, i like it !
If you had time can you translate the function generateTable with vbscript !
Thank you !

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: [Solved] How to filter all non-Microsoft services with WMIC and redirect the result to a text file ?

#7 Post by aGerman » 05 Nov 2019 19:10

No problem.

Code: Select all

<html><head><title>Non-Microsoft Services</title><meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta http-equiv="x-ua-compatible" content="IE=9">
<style type="text/css">
  body {font: 12px Verdana, sans-serif;}
  table, thead {border: 3px solid #aaaaaa; border-collapse: collapse;}
  tr, td, th {padding: 5px; border: 2px solid #aaaaaa;}
  th {background-color: #555555; color: #ffffff;}
  tr {background-color: #fffbf0;}
  tr:nth-child(odd) {background-color: #e4ebf2;}
  td {white-space: pre-wrap;}
</style>
<script type="text/vbscript">
  Option Explicit
  Dim escObj
  Set escObj = document.createElement("textarea")
  Function escForHTML(str)
    escObj.textContent = str
    escForHTML = escObj.innerHTML
  End Function
  Sub generateTable
    Dim strHtml, objWMIService, colServices, objService, color
    strHtml = "<table><thead><tr><th>Service Name</th><th>Display Name</th><th>State</th><th>Executable Path</th><th>Description</th></tr></thead><tbody>"
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colServices = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE NOT PathName LIKE '%Micro%' AND NOT PathName LIKE '%Windows%'")
    For Each objService In colServices
      If objService.State = "Running" Then
        color = "DarkGreen"
      Else
        color = "DarkRed"
      End If
      strHtml = strHtml & "<tr style=""color: " & color & """><td>" & escForHTML(objService.Name) & _
                 "</td><td>" & escForHTML(objService.DisplayName) & "</td><td>" & escForHTML(objService.State) & "</td><td>" & _
                 escForHTML(objService.PathName) & "</td><td>" & escForHTML(objService.Description) & "</td></tr>"
    Next
    bodyNode.innerHTML = strHtml & "</tbody></table>"
  End Sub
</script>
<HTA:APPLICATION windowState="maximize"></head><body id="bodyNode" onload="generateTable()"></body></html>
Steffen

Post Reply