service

5 views
Skip to first unread message

Guercio

unread,
Sep 20, 2002, 10:19:04 AM9/20/02
to
can i stop and restart a win2000 service with remote Wsh ?

--
'ao
Guercio


Torgeir Bakken

unread,
Sep 20, 2002, 10:30:08 AM9/20/02
to
Guercio wrote:

> can i stop and restart a win2000 service with remote Wsh ?

Hi

Yes, the remote script can just shell out and use "Net stop/start". But I think
is is easier to use other means for this:

A)
Use the command line based util PsService.exe in the free PsTools suite at
http://www.sysinternals.com,


B)
Use ADSI from a VBScript.

Here is a script that uses ping.exe to check if an external computer is
connectible using ping.exe, and if not, starts the "Fax" service.

It is using ADSI to handle the service (on NT 4.0, Dsclient must be installed on
the computer executing the script).

IADsService
http://msdn.microsoft.com/library/en-us/netdir/adsi/iadsservice.asp

IADsServiceOperations
http://msdn.microsoft.com/library/en-us/netdir/adsi/iadsserviceoperations.asp


' script start

sExternalNode = "some name/ip address here"

Const ADS_SERVICE_RUNNING = 4

If Not IsConnectible(sExternalNode,"","") Then
' . is for local computer
Set oComp = GetObject("WinNT://.,computer")
Set oService = oComp.GetObject("Service", "Fax")
If (oService.Status <> ADS_SERVICE_RUNNING) Then
' the operation is not running, start it
oService.Start
Do
' Hold the script to the service is started
Set oService = oComp.GetObject("Service", "Fax")
WScript.Sleep 100
i = i + 1
If i = 100 Then
' If the sevice hasn't started after 10 seconds,
' something must be wrong
WScript.Echo "Could not start the service!"
Exit Do
End If
Loop Until oService.Status = ADS_SERVICE_RUNNING
End If
End If

Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' Works an "all" WSH versions
' sHost is a hostname or IP

' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used

If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750

Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "\runresult.tmp"

oShell.Run "%comspec% /c ping -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True

Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsDefault)

sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)

Select Case InStr(sResults,"TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select
End Function


--
torgeir


Guercio

unread,
Sep 20, 2002, 11:18:32 AM9/20/02
to
"Torgeir Bakken" <Torgeir.B...@hydro.com> wrote in message
news:3D8B30F0...@hydro.com...

> Guercio wrote:
>
> > can i stop and restart a win2000 service with remote Wsh ?
>
> Hi
>
> Yes, the remote script can just shell out and use "Net stop/start". But I
think
> is is easier to use other means for this:

[SNIP]

as always you're the best :))

--
'ao
Guercio


Reply all
Reply to author
Forward
0 new messages