Gang:

I just had to put together a quick script to monitor the status (and re-start) some remote Windows NT services we have.

But I thought this script might come in handy for those that use Windows 2000 workstations to administer domains that support Windows NT and the KXRPC service.

The following script iterates across a list of servers and remotely queries the status of the KXRPC service.

It optionally gives one the ability to re-start any stopped services.

The list of servers is "embedded" in the script and is read like an external table. I did this just for the hell of it.

If this strategy doesn't work for you because of funky editor differences, it's not tough to change and iterate from a text file.

As well - this script will work on any remote service.

[file: rpcstat.kix]

code:

break on


$USAGE = 'kix32 rpcstat [$start=y] [$stop=y] [$out=filename]'


;=============================================================
; KIXRPC REMOTE SERVICE STATUS
;
; Requires Windows 2000 ( or Windows NT /w ADSI installed )
; Requires Domain Admin rights
;
; ABSTRACT:
;
; Scans a list of remote servers and queries the status
; of the KXRPC service.
;
; Optionally - gives one the ability to automatically start
; and stop all KXRPC services ...
;
; examples...
;
; kix32 rpcstat ; just query the status
; kix32 rpcstat $out=del.me ; query the status and pipe to file
; kix32 rpcstat $start=y ; query status and restart any stopped
; kix32 rpcstat $stop=y ; query the status and stop any running
;
;=============================================================


; Change this string table to include your servers ...


$SERVERS = "
DN1CMP
DN1CNT
DN1COV
"


$DELIM = chr(10)


$SERVICE_NAME = "KXRPC"


; ADSI service state codes ...


DIM $STATE[9]
$STATE[0] = "UNKNOWN"
$STATE[1] = "STOPPED"
$STATE[2] = "START_PENDING"
$STATE[3] = "STOP_PENDING"
$STATE[4] = "RUNNING"
$STATE[5] = "CONTINUE_PENDING"
$STATE[6] = "PAUSE_PENDING"
$STATE[7] = "PAUSED"
$STATE[8] = "ERROR"


; Redirect output to a file or device ...


if $out
del "$out"
$= redirectoutput ( "$out" )
endif


; Process the command line switches ...


if $start
$start = ucase ( ltrim ( $start ) )
else
$start = "N"
endif


if $stop
$stop = ucase ( ltrim ( $stop ) )
else
$stop = "N"
endif


; Loop for all servers in the list ...


while $servers


; Parse out the server name


$server = substr ( $servers, 1, instr( $servers, "$DELIM" )-len($DELIM))


; Clean it up ...


$server = ucase ( ltrim ( $server ) )


; If a server specified (ie. not a blank line in the list) ...


if $server


; Build the ADSI path to the server service ...


$adspath = "WinNT://$server/$SERVICE_NAME"


; Get the service object from ADSI ...


$service = olegetobject ( 0, "$adspath" )


if @error = 0


; Get the service state (ie, running,stopped) ...


$status = val ( olegetproperty ( $service, "status" ) )


; Check the status so it doesn't blow our state array ...
; Otherwise - get the textual representation of the state ...


if $status < 1 or $status > 8
$service_state = $STATE[0]
else
$service_state = $STATE[$status]
endif


; Report it ...


?"$server: $SERVICE_NAME IS $service_state."


if $service_state = "STOPPED" and $start = "Y"


" STARTING ..."


$= olecallfunc ( $service, "start" )


endif


if $service_state = "RUNNING" and $stop = "Y"


" STOPPING ..."


$= olecallfunc ( $service, "stop" )


endif


else


; Report an error ...


?"$server: @serror"


endif


endif


; Release the object from memory ...


$= olereleaseobject ( $service )


$servers = substr ( $servers, instr( $servers, "$DELIM" )+len($DELIM), len($servers))


loop


?


;======
:finish
;======


if $service
$= olereleaseobject ( $service )
endif


exit


Comments and feedback welcome.

Shawn.

[This message has been edited by Shawn (edited 04 October 2000).]