Solved

Check if a service is running. this batch script shows as not running even for machines where permission is an issue.

Posted on 2009-06-30
12
1,866 Views
Last Modified: 2012-05-07
Hi,

Check if a service is running. this batch script shows as not running even for machines where permission is an issue.
I get as "not running" for all the 4 services. When i check the machine is ON.
I dout it may be a permission error.

Can anyone help teeking this code.

Regards
Sharath
:: ================
:: READ THIS FIRST
:: ================
:: * To run this script you must have domain administrators rights.
:: * This script require "Computers.txt" file from where it will pick computer names.
:: * You need to add service names inside the script
:: * Successful run will generate "ServiceStatusRpt.txt"
:: * Copy and Paste following script into notepad and save it with any name having .cmd extension.
:: Batch Script Start
 
@ECHO OFF
SETLOCAL EnableDelayedExpansion
 
:: Add Service Names Here with prefix ECHO
(     ECHO Sophos Agent
      ECHO SAVservice
      ECHO Sophos Device Control Service
      ECHO Sophos AutoUpdate Service)>SvcName.txt
 
IF NOT EXIST Computers.txt Goto ShowErr
FOR %%R IN (Computers.txt) Do IF %%~zR EQU 0 Goto ShowErr
IF EXIST ServiceStatusRpt.txt DEL /F /Q ServiceStatusRpt.txt
 
ECHO Machine Name:Sophos Agent:SAVService:Sophos Device Control Service:Sohpos AutoUpdate Service>ServiceStatusRpt.txt
FOR /F %%c IN ('Type Computers.txt') Do (
    Echo Processing: %%c
    PING -n 1 -w 1000 %%c|Find /I "TTL" >NUL
    IF NOT ErrorLevel 1 (
        Set Report=%%c:
        FOR /F "delims=*" %%s IN ('TYPE SvcName.txt') DO (
            WMIC /NODE:"%%c" SERVICE WHERE "Name='%%s' AND State='Running'" GET Status 2>NUL |FIND /I "OK" >NUL
            IF NOT ERRORLEVEL 1 (
                Set Report=!Report!Running:
            ) ELSE (
                Set Report=!Report!NOT Running:
            )
        )
    ) ELSE (
        Set Report=%%c: Not able to connect
    )
    ECHO !Report:~0,-1!>>ServiceStatusRpt.txt 
)      
Goto EndScript
:ShowErr
Echo "Computers.txt" file does not exist or file is empty!
:EndScript
IF EXIST SvcName.txt DEL /F /Q SvcName.txt
ENDLOCAL
EXIT /B 0
:: Batch Script End

Open in new window

0
Comment
Question by:bsharath
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 5

Accepted Solution

by:
AbqBill earned 500 total points
ID: 24748564
Hi bsharath, Do you have PowerShell on at least one machine? The attached script I believe will do what you want. Bill.
# Returns 0 if ping succeeded, non-zero for failure
function test-iphost($iphost) {
  $status = get-wmiobject -class Win32_PingStatus `
    -namespace root/cimv2 -property PrimaryAddressResolutionStatus,StatusCode `
    -filter "Address='$iphost'"
  if ($status.PrimaryAddressResolutionStatus -ne 0) {
    $status.PrimaryAddressResolutionStatus
  }
  else {
    $status.StatusCode
  }
}
 
# Put the service names you want to check for in this array
$services = @("tmlisten",
              "OfcPfwSvc",
              "ntrtscan")
 
# The script assumes a list of computer names is in Computers.txt
if (-not (test-path "Computers.txt")) {
  "Computers.txt file not found"
  exit
} else {
  $computers = get-content "Computers.txt"
}
 
# You can customize the report file's name here
$reportfile = "ServiceStatusReport.csv"
 
# Output the CSV header
$csvheader = '"Computer"'
$services | foreach-object { $csvheader += (',"' + $_ + '"') }
$csvheader | out-file $reportfile -force -encoding ASCII
 
foreach ($computer in $computers) {
  write-host -nonewline "Processing $computer..."
  $output = ('"' + $computer + '"')
  if ((test-iphost $computer) -eq 0) {
    $services | foreach-object {
      $output += (',"' + ((get-wmiobject Win32_Service -computer $computer `
        -namespace root/cimv2 -property State -filter "Name='$_'").State) +
        '"')
    }
  } else {
    $output += ',"Unable to connect"'
  }
  $output | out-file $reportfile -append -encoding ASCII
  write-host "done."
}
 
"Finished. See $reportfile for results."

Open in new window

0
 
LVL 16

Expert Comment

by:t0t0
ID: 24750090
Look at the following line, should the '|' (pipe symbol) be preceeded with a '^' (ceret sign)?

>>    PING -n 1 -w 1000 %%c|Find /I "TTL" >NUL

    PING -n 1 -w 1000 %%c^|Find /I "TTL" >NUL

0
 
LVL 11

Author Comment

by:bsharath
ID: 24762868
toto for all machines that are even On i get the below message

Not able to connec
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:AbqBill
ID: 24763762
Hi bsharath, have you tried the PowerShell script I posted? Bill.
0
 
LVL 11

Author Comment

by:bsharath
ID: 24763831
Hi Bill
I would love to use it but ...I need the steps on using it
0
 
LVL 11

Author Comment

by:bsharath
ID: 24763832
Hi Bill
I would love to use it but ...I need the steps on using it
0
 
LVL 5

Expert Comment

by:AbqBill
ID: 24764282
Hi bsharath, do you have PowerShell installed on at least one machine? If so, copy this script to that machine, rename it to .ps1, start PowerShell, and run the script from the PowerShell prompt. Bill.
0
 
LVL 11

Author Comment

by:bsharath
ID: 24764322
Can i get the link for download please..
For windows 2003 OS....
0
 
LVL 5

Expert Comment

by:AbqBill
ID: 24764760
Google 'download windows powershell'. Bill.
0
 
LVL 11

Author Comment

by:bsharath
ID: 24771147
Abqbill...
I get this for many systems

Processing Dev1115...Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At D:\Services.ps1:41 char:41
+       $output += (',"' + ((get-wmiobject  <<<< Win32_Service -computer $computer `
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At D:\Services.ps1:41 char:41
+       $output += (',"' + ((get-wmiobject  <<<< Win32_Service -computer $computer `
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At D:\Services.ps1:41 char:41
+       $output += (',"' + ((get-wmiobject  <<<< Win32_Service -computer $computer `
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At D:\Services.ps1:41 char:41
+       $output += (',"' + ((get-wmiobject  <<<< Win32_Service -computer $computer `
0
 
LVL 5

Expert Comment

by:AbqBill
ID: 24772965
Hi bsharath, that error code usually means that WMI can't connect to a computer. One common cause is the Windows firewall. If the computer's firewall does not allow the WMI connection, then naturally you will get errors. Knowledge base article http://support.microsoft.com/kb/875605 should help you with this. Bill.
0
 
LVL 11

Author Comment

by:bsharath
ID: 24779320
Thank U
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Batch: reboot servers one by one. 4 38
Convert .csv to tab delimited text 11 82
Need help Creating a Powershell script 8 97
Hta File displays dynamic File names 4 65
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question