Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

command line scripting help 1

Status
Not open for further replies.

mutley1

MIS
Jul 24, 2003
909
Hi all,

I'm trying to get 3 bits of information onto 1 line of an output file and not sure if it is possible. I have a server name, a service name and am then running an sc query to obtain the status of the service. Obviously when you do sc \\SERVER query BLAH, there are several lines of text returned so I am using findsrt "STATE" to return the single line.

In the output file i want to see SERVER, SERVICE, STATE= (from sc command) on 1 line. I'm using echo to get the first 2 peices of info (hardcoded line by line but not a problem), but not sure how to get the output of sc on that line as well. It seems to be a problem with findstr as well.

Any ideas?

Cheers,

M.
 
Woa, wait a minute. NT4? Are these servers NT4? If so why are you posting int he WIndows 2000 forum?

If they are NT4 you will need to install the WMI core and cross your fingers because that does nto provide FULL WMI functionality on NT4. You will also need to ensure that the WSH 5.6 engine has been installed on each server too.

I hope you find this post helpful.

Regards,

Mark
 
Yeah sorry, I should have shifted the post. I realised when Anti made the comment. However, I am based on a 2K machine running queries off to the other servers, and the post originally started out as using SC and echo.....on my 2K server!

Thanks for the info though.

BR,

M.
 
OK - everything running on a W2K server, the servers I have put in the input file are W2K (and 1 2003). No error, but blank report. I'll find my way into this VB stuff!! :)
 
No error? Is On Error Resume Next still commented out?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Still commented out and the report is blank.
 
Could you post the actual code that you are running please (obfuscate any security sensitive information of course).

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
as above from Mark....

Code:
'  On Error Resume Next

'Set the report output directory (end with a backslash)
dir = "C:\Reports\"
'Set the text report file name
filename = "Service Status.txt"


'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
'open the data file
Set oTextStream = oFSO.OpenTextFile("wslist.txt")
'make an array from the data file
RemotePC = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close

For Each strComputer In RemotePC

Set objWMIService = GetObject _
        ("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colRunningServices = objWMIService.ExecQuery _
    ("Select * from Win32_Service")
    For Each objService in colRunningServices
        If objService.DisplayName = "MSSQLSERVER" Then
        Report = Report & vbCrLf & "Computer " & strComputer & " reports service " & objService.DisplayName  & " is " & objService.State 
        End If
    Next
Next

If Not oFSO.FolderExists(dir) Then
    oFSO.CreateFolder(dir)
End If
Set ts = oFSO.CreateTextFile (dir & filename, ForWriting)
ts.write report
wscript.echo "Report Created " & dir & filename

And the input file has 1 server in it at the moment.
 
Well for one thing, you never define ForWriting. Second, the CreateTextFile method does not support giving an iomode. It always opens the file for writing. The second arg for the method controls overwrite behavior. Try this:
Set ts = oFSO.CreateTextFile (dir & filename, True)


[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Well for one thing, you never define ForWriting

Good catch Tom. That is what happens when I rush and copy from one of my scripts to another. You are also right on the second argument, I was thinking OpenTextFile. :-(

Mutley, let's see if it is properly enumerating your server list.

Change this:
For Each strComputer In RemotePC

To this:
For Each strComputer In RemotePC
MsgBox strComputer


It should give a POP Up for each server in your list (so just test with one or two).



I hope you find this post helpful.

Regards,

Mark
 
Thanks guys....sorry to cause any hassle.....

Been off on hols but back in to the slog tomorrow so will give it a go and let you know the results.

Thanks for all your help

M.
 
Morning guys,

I've changed the line to

Set ts = oFSO.CreateTextFile (dir & filename, True)

and added the MsgBox. It is prompting me for the 2 servers in the list correctly, but the report is still coming out empty.
 
OK, then you may be looking at a permissions issue.

I'd first verify this by running the script locally on one of the servers. if it works then it is a remote access issue and you will need to use impersonation.

Code:
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

I hope you find this post helpful.

Regards,

Mark
 
Hi Mark,

Sorry - doesn't seem to be that either. I've logged onto the server locally as administrator (as opposed to a user who had admin rights) and run it with just that server in the input file and still coming out blank.
 
Something VERY STRANGE is going on. Try this please:

Code:
If [red]ucase(objService.DisplayName)[/red] = "MSSQLSERVER" Then

I hope you find this post helpful.

Regards,

Mark
 
Hi Mark,

The server I'm testing on doesn't have SQL, so am currently running with messenger. If i use the ucase code, then it returns something now, but without it there is just a blank report. should I just add that ucase in and leave it at that, or does the ucase indicate something. I guess it means force uppercase, but i have MESSENGER in uppercase already - but it works with ucase and not without! Bizarre......!!
 
And just to complicate matters, the other service I was hoping to monitor has a screen name of "Workgroup Service" and physical name of "Workgroup".....and neither of those work with or without ucase...and they are definately a mixture of upper and lower case.
 
What you need to do is force the comparison to be either all upper case or all lower case so no matter what case or mixed case the system reports your comparison will be the same.

ucase forces the uppercase and lcase forces lower case.

I hope you find this post helpful.

Regards,

Mark
 
All sorted and up and running. Thanks for everything guys!
 
I know its now sorted but for future info I wrote myself a batch script to do the same thing so thought I'd post it up just in case it was of use to somebody. When running it allows you to chose the service you want to query and what state it should be in. You need W2K Resource Kit installed and may need to copy "serviceconfig.pl" and "sclist" to the working folder.

Create two batch files, one called "runme.bat", the other called "servicereport.bat"

In runme.bat put the following:

cls
@ echo off
set /p service= Enter the service you wish to look for (dont use descriptive name) :

CHOICE /C:123 PICK A Service State (1=Stopped,2=Running,3=Paused)%1
IF ERRORLEVEL ==3 GOTO THREE
IF ERRORLEVEL ==2 GOTO TWO
IF ERRORLEVEL ==1 GOTO ONE

:ONE
set state=STOPPED
goto continue

:TWO
set state=RUNNING
goto continue

:THREE
set state=PAUSED

:continue

set date=|date /t
set time=|time /t

echo Run on %date% at %time% >> results\%service%_%state%.txt
echo %service% state confirmed as %state% >> results\%service%_%state%.txt
echo Run on %date% at %time% >> results\%service%_Not_%state%.txt
echo %service% not currently in %state% state >> results\%service%_Not_%state%.txt
echo Run on %date% at %time% >> results\Offline(%SERVICE%).txt
echo Servers offline >> results\offline(%SERVICE%).txt
echo RPC Failed to Reply >> results\RPCFailed(%SERVICE%).txt
echo Run on %date% at %time% >> results\RPCFailed(%SERVICE%).txt
echo Run on %date% at %time% >> results\%service%_notinstalled.txt
echo %service% not installed >> results\%service%_notinstalled.txt

for /f "tokens=1" %%i in (working.txt) do call servicereport.bat %%i




Then, in the "servicereport.bat" put


set pcname=%1
ping -n 1 -w 1500 %pcname%
if errorlevel 1 goto offlineservers
sclist -s -r %pcname%
if errorlevel 1 goto rpcfail
rem serviceadmin.pl -query -s %pcname% %service%
serviceconfig.pl -s %pcname% -general -filter "state=%state%" %service%
if errorlevel 2302 goto notinstalled
if errorlevel 30 goto serviceerror
rem serviceadmin.pl -query -s %pcname% -filter State=%state% %service%
rem if errorlevel 1 goto serviceerror
echo %pcname% >> results\%service%_%state%.txt
goto end

:eek:fflineservers
echo %pcname% >> results\offline(%SERVICE%).txt
goto end
REM #### Lists servers that failed to respond to a ping check

:notinstalled
echo %pcname% >> results\%service%_notinstalled.txt
goto end
REM #### Lists servers that don't have the Service present

:rpcfail
echo %pcname% >> results\RPCFailed(%SERVICE%).txt
goto end
REM #### Lists servers where the RPC request failed

:serviceerror
echo %pcname% >> results\%service%_Not_%state%.txt
goto end

:end


Create a file called "working.txt" in the same folder and populate it with your server list, and also create a sub-folder called "results". Run runme.bat and the sript will output 5 files to Results, called state_<state>, state_not<state>, notinstalled, offline and RPCFailed (where the service status cant be queried).

It could be drastically simplified without all the choice stuff at the start if you only wanted to query the same service all the time.

Chris
 
You actually need "serviceconfig.pl (not serviceadmin) and need "choice.exe" from the resource kit by the way, must remove the REM'd lines from above to tidy it up and avoid confusion :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top