Solved

Modify Current Script for Querying Services.

Posted on 2013-01-21
7
315 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 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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