Solved

Modify Current Script for Querying Services.

Posted on 2013-01-21
7
318 Views
Last Modified: 2013-01-21
Hi All,

I have a script that allows me to check if my backup services are running on multiple servers that have been rebooted after patching the OS, it also pings the server as well, the script runs fine in the dos window and i can see the results as they happen but for some reason the script will not output those results to a csv/excel file.

What i would like on the headers of the output file are the following.

Computer name - Ping - Service Name - Running - Stopped

Heres the script i have.

@echo off
setlocal enabledelayedexpansion
Set ServerFile=c:\Servers.txt
set ServiceList="BackupExecRPCService","BackupExecAgentAccelerator","BackupExecJobEngine","BackupExecDeviceMediaService","BackupExecAgentBrowser"
set CsvDelim=,
set CsvFile=%~dpn0.csv
set Header="Computer"
for %%a in (%ServiceList%) do (set Header=!Header!!CsvDelim!"%%~a")
>%CsvFile% echo !Header!
for /f "usebackq tokens=1" %%a in ("%ServerFile%") do (
      set Line="%%~a"
      echo Processing %%a ...
      ping.exe -4 %%a | find /i "TTL" >NUL
      if errorlevel 1 (
            echo ... not responding.
            for %%a in (%ServiceList%) do (set Line=!Line!!CsvDelim!"NO_PING")
      ) else (
            echo ... responding, querying services ...
            for %%s in (%ServiceList%) do (
                  set State=NOT_FOUND
                  for /f "tokens=2,3 delims=: " %%u in ('sc.exe \\%%~a query "%%~s" 2^>NUL ^| find "STATE"') do (set State=%%~v)
                  set Line=!Line!!CsvDelim!"!State!"
                  echo ... %%~s: !State!
            )
      )
      >>%CsvFile% echo !Line!
)
output.JPG
0
Comment
Question by:Alan OBrien
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38800800
What is the name of your script? If it contains spaces, try renaming the script by removing the spaces from it. Do you get any output file btw?
0
 
LVL 4

Author Comment

by:Alan OBrien
ID: 38800920
name of the script is checkservices.bat but it wont output to the csv, it will give me output on the command line(see attached jpg) which does give the correct results but no csv
0
 
LVL 85

Expert Comment

by:oBdA
ID: 38800930
The script should create a file <Scriptname without extension>.csv in the script's folder; if it doesn't, check if the account you're using has permissions to delete/create this csv file:
C:\Temp>test.cmd
Processing LOCALHOST ...
... responding, querying services ...
... Spooler: STOPPED
... DHCP: RUNNING
Processing FOO ...
... not responding.

C:\Temp>type test.csv
"Computer","Spooler","DHCP"
"LOCALHOST","STOPPED","RUNNING"
"FOO","NO_PING","NO_PING"

Open in new window

0
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
LVL 4

Author Comment

by:Alan OBrien
ID: 38800983
Hi

I have other scripts in the folder that can generate csv files, i run the script as administrator and i am setup on the machine as administrator so not sure what i am missing?
thanks.
0
 
LVL 85

Accepted Solution

by:
oBdA earned 500 total points
ID: 38800998
Please copy just that script and servers.txt into a new test folder and run it again from an elevated command prompt.
When the script is done, there should be a new file "checkservices.csv" containing the information.
0
 
LVL 4

Author Comment

by:Alan OBrien
ID: 38801087
For some reason the above worked even though i had ran the script from an elevated account, don't understand why it does but it does as other scripts ran fine in the folder. thanks for the help guys.
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38801373
strange, I ran first part of the script and got an output file just fine, without elevation...
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

617 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