Solved

Modify Current Script for Querying Services.

Posted on 2013-01-21
7
316 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 84

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
Independent Software Vendors: 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 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 84

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

751 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