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 Chriss Miller 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.
 
Use vbscript/WMI.

Code:
Set WSHNetwork = CreateObject("Wscript.Network")
strComputer = WSHNetwork.Computername
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
    ("Select * from Win32_Service")
For Each objService in colRunningServices 
    If objService.DisplayName = "Messenger" Then
    Report = "Computer " & strComputer & " reports service " & objService.DisplayName  & " is " & objService.State
    End If
Next
WScript.Echo report

I just have it echoing the line you wanted, but easy enough to write to a file if you prefer.

I hope you find this post helpful.

Regards,

Mark
 
Thanks Mark,

I'll try to put the server & service name in the right place in that, but I think I'm in way over my head.....not a coder at all!! I was asking about the echo side just because I've got a batch file that runs the sc over 50 servers.

1. I take it I would have to do the above 50 times over, once for each server?
2. Could you point me to where I need to put the server name and where to specify the service name? Don't know much about VB - sorry!!!

Cheers,

M
 
All the more reason to do it with vbscript and WMI.

See my FAQ on the subject at faq329-4871.

strcomputer sets the PC name in the script above. And I am looking for the Messenger service in the example.

I hope you find this post helpful.

Regards,

Mark
 
Looks like Mark already go it. However, If your are new to vb script and have a couple hours you might want to flip through the Microsoft Script Center. The Scripting Guide there is easy to read and will get you up and running inside of an hour. Might want to check out the Scriptomatic there also, it can generate some farily impressive stuff through drop down boxes.

 
Thanks Anti,

very helpful. Need to get going on VB so if something can give me an idea within an hour then brilliant!!!

Thanks,

M.
 
Script Repository from MS and the Scriptomatic will get you started on almost all needs you may have. Both are free from Microsoft.

I hope you find this post helpful.

Regards,

Mark
 
Hi Mark,

I've had a nosey round the sites and gathered a bit of into & done some basic reading. I wanted to use the text file input as a list of servers, so used a bit of one of the scripts there and the bit you kindly provided. I've saved it as a vbs file, but it doesn't do anything when i run it...nothing od screen when doing 'cscript xxxx.vbs'.

Am i missing something? Also, could you tell me how to uotput to a file....I can't seem to find the basic syntax on the site!

Code:
On Error Resume Next

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\Matt\servers.txt", ForReading)

Do Until objTextFile.AtEndOfStream 
    strComputer = objTextFile.Readline

    ' =====================================================================
    ' Insert your code here
    ' =====================================================================

    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 = "Computer " & strComputer & " reports service " & objService.DisplayName  & " is " & objService.State
    End If
Next
WScript.Echo report

    ' =====================================================================
    ' End
    ' =====================================================================

Loop

objTextFile.Close

Thanks,

M.
 
Matt, did you read my FAQ on this as I posted earlier faq329-4871

Don't know why but I personally don't like the whole Do Loop method.

Also you would want to echo AFTER the looping is done. You would also want to build the "report" with each pass.

Final step is then to check if the desired location for the report is there and if not create the folder then save the report, overwriting any previous versions.

Here is the code you need. Note that the scritp is expecting the source file to be named wslist.txt and that file should be int he same directory as the script. [red]You need to edit the dir & filename sections.[/red]

Code:
'==========================================================================
'
' NAME: ReportServiceStatusMultiPC.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.TheSpidersParlor.com[/URL]
' COPYWRITE (c) 2006 All Rights Reserved
' DATE  : 2/24/2006
'
' COMMENT: 
'
'==========================================================================

 On Error Resume Next

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


'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

I'll create an FAQ soon to discuss the different methods of writing a report in the next few days.

I hope you find this post helpful.

Regards,

Mark
 
Sorry Mark, it's outputting a blank report.

I renamed the input file to wslist.txt and it is in the same directory as the script. It literally has 2 lines,

SERVER1
SERVER2

and that's it. I've also checked using SC command that the service is running and it is. I've also changed the script to check messenger (in case i got MSSQLSERVER wrong) and that outputs a blank as well.

Everything in the same directory and it does indeed produce a txt file, but with no data.

Any ideas?

Thanks,

M.
 
Comment out the On Error Resume Next and see what errors you get.

[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]
 
Line 21 char 1 syntax error. (I've not got headings that are in the code above - line 1 is the on error resume next so line 21 is:

Set objWMIService = GetObject _

Looks fine to me even though I don't code!

Thanks,

M.
 
Try using the full computer name with the domain tacked onto the end in your server list. Basicly what you find under System Properties -> Computer Name.
 
Are you running the script while logged on as admin?

I hope you find this post helpful.

Regards,

Mark
 
The account I am logged in as is an administrator on the machine I am using and an administrator on the 2 servers in my test file.
 
My bad, I was thinking about something else on the domain name thing.

I am getting the CreateTextFile part throwing a file exists error. From what I remember the second part of that method is a TRUE/FALSE, TRUE it overwrites the file, FALSE it does not. Try changing the:
Set ts = oFSO.CreateTextFile (dir & filename, ForWriting)
to:
Set ts = oFSO.CreateTextFile (dir & filename, TRUE)

At that point the code mark posted will run more then once.


As a side note, if you want to append to a file try the OpenTextFile method:
Set ts = oFSO.OpenTextFile ( dir & filename, 8, TRUE)
ts.Write "Run at " & Now() & vbCrLf & report & vbCrLf & vbcrlf



 
Thanks Anti - It's an NT4 domain not AD, but worth the thought!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top