Solved

Modify Current Script for Querying Services.

Posted on 2013-01-21
7
311 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:oBdA
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Author Comment

by:Alan OBrien
Comment Utility
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 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
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
Comment Utility
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 37

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now