VB .NET Services on remote machines...
#1
Scooby Regular
Thread Starter
Join Date: Nov 2001
Location: Leeds - It was 562.4bhp@28psi on Optimax, How much closer to 600 with race fuel and a bigger turbo?
Posts: 15,239
Likes: 0
Received 1 Like
on
1 Post
I have been writing a windows service, I want to be able to connect to a remote machine and query a service... the following code works fine to a win2k machine but not nt4 any ideas?? Is this using wmi or similar??
Sub Main()
Dim MyController As ServiceController
MyController = New ServiceController("Spooler")
MyController.MachineName = "TEST01"
Dim sStatus As String
MyController.Refresh()
sStatus = MyController.Status.ToString
Console.WriteLine(MyController.ServiceName & " is in state:" & sStatus)
End Sub
Sub Main()
Dim MyController As ServiceController
MyController = New ServiceController("Spooler")
MyController.MachineName = "TEST01"
Dim sStatus As String
MyController.Refresh()
sStatus = MyController.Status.ToString
Console.WriteLine(MyController.ServiceName & " is in state:" & sStatus)
End Sub
#3
Scooby Regular
Thread Starter
Join Date: Nov 2001
Location: Leeds - It was 562.4bhp@28psi on Optimax, How much closer to 600 with race fuel and a bigger turbo?
Posts: 15,239
Likes: 0
Received 1 Like
on
1 Post
An unhandled exception of type 'System.InvalidOperationException' occurred in system.serviceprocess.dll
Additional information: Service Spooler was not found on computer 'cpl005055'.
David
Additional information: Service Spooler was not found on computer 'cpl005055'.
David
#6
F1 help have a look for "Retrieving Lists of Services".
This is a foreign language (I use C#) snippet:
Imports System
Imports System.ServiceProcess
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim services() As ServiceController, i As Integer
services = ServiceController.GetServices("cpl005055")
Me.ListBox1.Items.Clear()
For i = 0 To services.Length - 1
Me.ListBox1.Items.Add(services(i).ServiceName)
Next
End Sub
This is a foreign language (I use C#) snippet:
Imports System
Imports System.ServiceProcess
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim services() As ServiceController, i As Integer
services = ServiceController.GetServices("cpl005055")
Me.ListBox1.Items.Clear()
For i = 0 To services.Length - 1
Me.ListBox1.Items.Add(services(i).ServiceName)
Next
End Sub
#7
using the magic of copy & paste
Imports System
Imports System.ServiceProcess
Sub Main()
Dim services() As ServiceController, i As Integer
services = ServiceController.GetServices("cpl005055")
For i = 0 To services.Length - 1
Console.WriteLine("Service : " & services(i).ServiceName & " Found")
Next
End Sub
Keep an eye out for any exceptions and see if Spooler is tipped out to the console. Tell us what you see...
[Edited by MrDeference - 4/4/2002 5:12:30 PM]
Imports System
Imports System.ServiceProcess
Sub Main()
Dim services() As ServiceController, i As Integer
services = ServiceController.GetServices("cpl005055")
For i = 0 To services.Length - 1
Console.WriteLine("Service : " & services(i).ServiceName & " Found")
Next
End Sub
Keep an eye out for any exceptions and see if Spooler is tipped out to the console. Tell us what you see...
[Edited by MrDeference - 4/4/2002 5:12:30 PM]
Trending Topics
#8
Scooby Regular
Thread Starter
Join Date: Nov 2001
Location: Leeds - It was 562.4bhp@28psi on Optimax, How much closer to 600 with race fuel and a bigger turbo?
Posts: 15,239
Likes: 0
Received 1 Like
on
1 Post
Service : Alerter Found
Service : AvSynMgr Found
Service : Browser Found
Service : ClipSrv Found
Service : CPQALERT Found
Service : CPQDMI Found
Service : DHCP Found
Service : EventLog Found
Service : EventSystem Found
Service : LanmanServer Found
Service : LanmanWorkstation Foun
Service : LmHosts Found
Service : LPDSVC Found
Service : McShield Found
Service : Messenger Found
Service : mnmsrvc Found
Service : MSIServer Found
Service : NAIMAGENT32 Found
Service : NALNTSERVICE Found
Service : NetDDE Found
Service : NetDDEdsdm Found
Service : Netlogon Found
Service : nmagent Found
Service : NtLmSsp Found
Service : PlugPlay Found
Service : ProtectedStorage Found
Service : Replicator Found
Service : RPCLOCATOR Found
Service : RpcSs Found
Service : Schedule Found
Service : SENS Found
Service : Spooler Found
Service : TapiSrv Found
Service : UPS Found
Service : WIN32SL Found
Service : WM Found
David
Service : AvSynMgr Found
Service : Browser Found
Service : ClipSrv Found
Service : CPQALERT Found
Service : CPQDMI Found
Service : DHCP Found
Service : EventLog Found
Service : EventSystem Found
Service : LanmanServer Found
Service : LanmanWorkstation Foun
Service : LmHosts Found
Service : LPDSVC Found
Service : McShield Found
Service : Messenger Found
Service : mnmsrvc Found
Service : MSIServer Found
Service : NAIMAGENT32 Found
Service : NALNTSERVICE Found
Service : NetDDE Found
Service : NetDDEdsdm Found
Service : Netlogon Found
Service : nmagent Found
Service : NtLmSsp Found
Service : PlugPlay Found
Service : ProtectedStorage Found
Service : Replicator Found
Service : RPCLOCATOR Found
Service : RpcSs Found
Service : Schedule Found
Service : SENS Found
Service : Spooler Found
Service : TapiSrv Found
Service : UPS Found
Service : WIN32SL Found
Service : WM Found
David
#9
Erm, OK, what does this do?
Imports System
Imports System.ServiceProcess
Sub Main()
Dim services() As ServiceController, i As Integer
services = ServiceController.GetServices("cpl005055")
For i = 0 To services.Length - 1
Console.WriteLine("Service : " & services(i).ServiceName & " Found. Status: " & services(i).Status)
Next
End Sub
Imports System
Imports System.ServiceProcess
Sub Main()
Dim services() As ServiceController, i As Integer
services = ServiceController.GetServices("cpl005055")
For i = 0 To services.Length - 1
Console.WriteLine("Service : " & services(i).ServiceName & " Found. Status: " & services(i).Status)
Next
End Sub
#10
Scooby Regular
Thread Starter
Join Date: Nov 2001
Location: Leeds - It was 562.4bhp@28psi on Optimax, How much closer to 600 with race fuel and a bigger turbo?
Posts: 15,239
Likes: 0
Received 1 Like
on
1 Post
Service : Alerter Found. Status: 1
Service : AvSynMgr Found. Status: 4
Service : Browser Found. Status: 1
Service : ClipSrv Found. Status: 1
Service : CPQALERT Found. Status: 4
Service : CPQDMI Found. Status: 4
Service : DHCP Found. Status: 4
Service : EventLog Found. Status: 4
Service : EventSystem Found. Status: 1
Service : LanmanServer Found. Status: 4
Service : LanmanWorkstation Found. Status: 4
Service : LmHosts Found. Status: 4
Service : LPDSVC Found. Status: 1
Service : McShield Found. Status: 4
Service : Messenger Found. Status: 4
Service : mnmsrvc Found. Status: 1
Service : MSIServer Found. Status: 1
Service : NAIMAGENT32 Found. Status: 1
Service : NALNTSERVICE Found. Status: 4
Service : NetDDE Found. Status: 1
Service : NetDDEdsdm Found. Status: 1
Service : Netlogon Found. Status: 4
Service : nmagent Found. Status: 1
Service : NtLmSsp Found. Status: 4
Service : PlugPlay Found. Status: 4
Service : ProtectedStorage Found. Status: 4
Service : Replicator Found. Status: 1
Service : RPCLOCATOR Found. Status: 1
Service : RpcSs Found. Status: 4
Service : Schedule Found. Status: 4
Service : SENS Found. Status: 1
Service : Spooler Found. Status: 1
Service : TapiSrv Found. Status: 1
Service : UPS Found. Status: 1
Service : WIN32SL Found. Status: 4
Service : WM Found. Status: 1
David
Service : AvSynMgr Found. Status: 4
Service : Browser Found. Status: 1
Service : ClipSrv Found. Status: 1
Service : CPQALERT Found. Status: 4
Service : CPQDMI Found. Status: 4
Service : DHCP Found. Status: 4
Service : EventLog Found. Status: 4
Service : EventSystem Found. Status: 1
Service : LanmanServer Found. Status: 4
Service : LanmanWorkstation Found. Status: 4
Service : LmHosts Found. Status: 4
Service : LPDSVC Found. Status: 1
Service : McShield Found. Status: 4
Service : Messenger Found. Status: 4
Service : mnmsrvc Found. Status: 1
Service : MSIServer Found. Status: 1
Service : NAIMAGENT32 Found. Status: 1
Service : NALNTSERVICE Found. Status: 4
Service : NetDDE Found. Status: 1
Service : NetDDEdsdm Found. Status: 1
Service : Netlogon Found. Status: 4
Service : nmagent Found. Status: 1
Service : NtLmSsp Found. Status: 4
Service : PlugPlay Found. Status: 4
Service : ProtectedStorage Found. Status: 4
Service : Replicator Found. Status: 1
Service : RPCLOCATOR Found. Status: 1
Service : RpcSs Found. Status: 4
Service : Schedule Found. Status: 4
Service : SENS Found. Status: 1
Service : Spooler Found. Status: 1
Service : TapiSrv Found. Status: 1
Service : UPS Found. Status: 1
Service : WIN32SL Found. Status: 4
Service : WM Found. Status: 1
David
#11
OK, that value on the end is the integer representation of the ServiceControllerStatus enumeration giving the running state:
Stopped 1
StartPending 2
StopPending 3
Running 4
ContinuePending 5
PausePending 6
Paused 7
If cpl005055 is the NT4 machine, and you have that output, you have all the info required. Just need to jig the code about a bit.
Stopped 1
StartPending 2
StopPending 3
Running 4
ContinuePending 5
PausePending 6
Paused 7
If cpl005055 is the NT4 machine, and you have that output, you have all the info required. Just need to jig the code about a bit.
#12
So, the short answer and the one that I should have written in the first post is that it looks like you should have called the overloaded constructor with the machine name and service:
Dim MyController As ServiceController
MyController = New ServiceController("Spooler", "TEST01")
In retrospect I should have spotted it earlier. Sorry.
Dim MyController As ServiceController
MyController = New ServiceController("Spooler", "TEST01")
In retrospect I should have spotted it earlier. Sorry.
#13
Scooby Regular
Thread Starter
Join Date: Nov 2001
Location: Leeds - It was 562.4bhp@28psi on Optimax, How much closer to 600 with race fuel and a bigger turbo?
Posts: 15,239
Likes: 0
Received 1 Like
on
1 Post
Not ideal but this combination works... I would prefer to specify the service name, but it just doesnt like it.
Sub Main()
Dim services() As ServiceController, i As Integer, d
services = ServiceController.GetServices("cpl005055")
For i = 0 To services.Length - 1
If services(i).ServiceName = "Spooler" And services(i).Status <> "4" Then
Try
services(i).Start()
Catch
Console.WriteLine("An Error Occurred Starting The Service")
End Try
End If
Next
End Sub
a couple of other things... please....
Do you know if you can send snmp alerts and messages the same as doing a net send easily...
Also if i have the service running and want to do something to do console.writeline to a console app that could run to monitor the status of the service, how would I do that? I dont need message queues do I?
David
Sub Main()
Dim services() As ServiceController, i As Integer, d
services = ServiceController.GetServices("cpl005055")
For i = 0 To services.Length - 1
If services(i).ServiceName = "Spooler" And services(i).Status <> "4" Then
Try
services(i).Start()
Catch
Console.WriteLine("An Error Occurred Starting The Service")
End Try
End If
Next
End Sub
a couple of other things... please....
Do you know if you can send snmp alerts and messages the same as doing a net send easily...
Also if i have the service running and want to do something to do console.writeline to a console app that could run to monitor the status of the service, how would I do that? I dont need message queues do I?
David
#14
Second one first,
use the TextWriterTraceListener (gulp!).
You need to use the IO namespace as well as the system.diagnostics one. Create a stream file and point the tracer to write to it (Trace.Listeners.Add). Thus when you call
System.Diagnostics.Trace... it will write it to the file.
This can also be done with the app.exe.config file, which is more in keeping with the ".NET way"
You will have to flush and close the file handle regulary
otherwise nothing will be written till the end of the app...
Re: SNMP alerts and net send alikes. You could just call the shellexecute API call. Not terribly elegant, granted. Other than that, I don't know...
use the TextWriterTraceListener (gulp!).
You need to use the IO namespace as well as the system.diagnostics one. Create a stream file and point the tracer to write to it (Trace.Listeners.Add). Thus when you call
System.Diagnostics.Trace... it will write it to the file.
This can also be done with the app.exe.config file, which is more in keeping with the ".NET way"
You will have to flush and close the file handle regulary
otherwise nothing will be written till the end of the app...
Re: SNMP alerts and net send alikes. You could just call the shellexecute API call. Not terribly elegant, granted. Other than that, I don't know...
#15
Scooby Regular
Thread Starter
Join Date: Nov 2001
Location: Leeds - It was 562.4bhp@28psi on Optimax, How much closer to 600 with race fuel and a bigger turbo?
Posts: 15,239
Likes: 0
Received 1 Like
on
1 Post
Excellent, that at least gives me something to go on... you've been a great help!!
David
David
Thread
Thread Starter
Forum
Replies
Last Post
Mattybr5@MB Developments
Full Cars Breaking For Spares
28
28 December 2015 11:07 PM
Mattybr5@MB Developments
Full Cars Breaking For Spares
12
18 November 2015 07:03 AM