Running command line command without the DOS window

Running command line command without the DOS window

Post by R0xU » Sat, 06 Feb 2010 14:39:01


Hi I am trying to loop through a recordset and issue a dos command to start a
service. When I try to execute the code below, I get about 50 DOS windows
opening at the same time, can anyone advise if I can execute these commands
silently, and how can I capture the results? All help is greatly
appreciated...

Private Sub btnStartServices_Click()

Dim rs As DAO.Recordset
Dim strServer, strService, strCommand As String
Dim strQry As String

strQry = "qry01_StartServices"

Set rs = CurrentDb.OpenRecordset(strQry, dbOpenDynaset)
If rs.RecordCount = 0 Then MsgBox "No Records"
With rs
.MoveFirst
Do

Set strServer = rs![Server]
Set strService = rs![Service]

Me![nowStarting] = strServer & " - " & strService
Me.Refresh

Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("sc //" & strServer & " start " &
strService)
MsgBox objExecObject.StdOut.ReadAll()

' strCommand = "sc //" & strServer & " start " & strService

' Shell strCommand

.MoveNext
Loop Until .EOF
.Close
End With
Set rst = Nothing

End Sub
 
 
 

Running command line command without the DOS window

Post by Matt Willi » Sat, 06 Feb 2010 23:13:44


There are things you can do to hide the cmd window but they involve 3rd
party apps to do it. CMDOW is one that I've used in the past. I'd just use
WMI to start the services instead and not worry about the cmd windows. It
doesn't look like you're using SC.exe for anything but starting the services
and that can easily be done with WMI. Here are some examples of using WMI to
work with services.
http://www.yqcomputer.com/

HTH

Matt

 
 
 

Running command line command without the DOS window

Post by R0xU » Sun, 07 Feb 2010 07:16:01

i Matt,

Thanks for you reply and steering me in the right direction.

I have modified my code and when I run it, I get the following error:

Run-time error '-2147217385 (80041017)': Automation error

which occurs on this line:

For Each objService In colServices

A copy of my code is below. Does anyone know what is causing this?

Any assistance is always greatly appreciated.

Cheers,
GLT.

Private Sub btnStartServices_Click()

Dim rs As DAO.Recordset
Dim strServer, strService, strCommand As String
Dim strQry As String
Dim objWMIService As Object
Dim colServices As Object
Dim objService As Object
Dim errReturnCode As Long
Dim strErrorMessage As String

strQry = "qry01_StartServices"

errReturnCode = 9999

Set rs = CurrentDb.OpenRecordset(strQry, dbOpenDynaset)
If rs.RecordCount = 0 Then MsgBox "No Records"
With rs
.MoveFirst
Do

strServer = "."
Set strService = rs![Service]

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

Set colServices = objWMIService.ExecQuery _
("SELECT * FROM Win32_Service" & _
" WHERE Name = '" & strService & "'")

Me![nowStarting] = strServer & " - " & strService
Me.Refresh

' MsgBox (strServer & " - " & strService)

For Each objService In colServices
errReturnCode = objService.StartService()
Next

.MoveNext
Loop Until .EOF
.Close
End With
Set rs = Nothing

Select Case errReturnCode
Case Is = 0
MsgBox "Service " & strServiceName & " has been" & _
" started successfully. ", vbInformation
Case Is = 9999
' If errReturnCode has not changed the Service was not found
MsgBox "Service """ & strServiceName & """ was not found" & _
" and may not exist. ", vbCritical
Case Else
' Get the error messages supplied by Microsoft
strErrorMessage = strErrorMessage =
ErrReturnStateChange(errReturnCode)
MsgBox "Service " & strServiceName _
& " change failed. " & vbCrLf & "Reason: " _
& strErrorMessage & " ", vbExclamation _
, "Service Change Failed"
End Select

SetServiceState = errReturnCode

Set objService = Nothing
Set colServices = Nothing
Set objWMIService = Nothing


End Sub

"Matt Williamson" wrote:

 
 
 

Running command line command without the DOS window

Post by Stuart McC » Sun, 07 Feb 2010 09:23:34


It wouldn't surprise me if MS's WMI code fills colServices with a collection
of Variants. Try declaring objService as Variant.
 
 
 

Running command line command without the DOS window

Post by R0xU » Sun, 07 Feb 2010 10:28:01

i Stuart,

Thanks for your reply,

I tried changing it to Variant and it still stopped with the same error.

Any other ideas?

Cheers,
GT.

"Stuart McCall" wrote:

 
 
 

Running command line command without the DOS window

Post by Stuart McC » Sun, 07 Feb 2010 11:35:07

GLT" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...

Nope. I've never had to use WMI so I've no experience to call on. I wuz just
guessing.
Maybe someone else has an idea...



 
 
 

Running command line command without the DOS window

Post by R0xU » Sun, 07 Feb 2010 11:53:01

k, finally worked this out and got it working.

The servive field contains the service display name, not the actual service
name - so I need to modify the script that collects the Server related that
is imported into my DB. Once I change the service to the actual service
name, all works well. Copy of the code:

Private Sub btnStartServices_Click()

On Error GoTo Err_btnStartServices

Dim rs As DAO.Recordset
Dim strServer, strService, strCommand As String
Dim strQry As String
Dim objWMIService As Object
Dim colServices As Object
Dim objService As Object
Dim errReturnCode As Long
Dim strErrorMessage As String
Dim loopCount As Integer

strQry = "qry01_StartServices"

errReturnCode = 9999

Set rs = CurrentDb.OpenRecordset(strQry, dbOpenDynaset)
If rs.RecordCount = 0 Then MsgBox "No Records"

loopCount = 0

With rs
.MoveFirst
Do

strServer = rs![Server]
strService = rs![Service]

Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"
& strServer & "\root\cimv2")
Set colServices = oWMI.ExecQuery("Select * from Win32_Service Where
Name ='" & strService & "'")

Me![nowStarting] = loopCount & strServer & " - " & strService
Me.Refresh

For Each objService In colServices
errReturnCode = objService.StartService()
Next

Select Case errReturnCode

Case Is = 0
MsgBox "Service " & strService & " has been" & _
" started successfully. ", vbInformation
Case Is = 9999
' If errReturnCode has not changed the Service was not found
MsgBox "Service """ & strService & """ was not found" & _
" and may not exist. ", vbCritical
Case Else
' Get the error messages supplied by Microsoft
strErrorMessage = strErrorMessage =
ErrReturnStateChange(errReturnCode)
MsgBox "Service " & strService _
& " change failed. " & vbCrLf & "Reason: " _
& strErrorMessage & " ", vbExclamation _
, "Service Change Failed"
End Select
loopCount = loopCount + 1
.MoveNext
Loop Until .EOF
.Close
End With
Set rs = Nothing

Err_btnStartServices:

SetServiceState = errReturnCode

MsgBox errReturnCode

Set objService = Nothing
Set colServices = Nothing
Set objWMIService = Nothing


End Sub

"GLT" wrote:

 
 
 

Running command line command without the DOS window

Post by Stuart McC » Sun, 07 Feb 2010 12:46:52


Great. I've saved a copy for future reference. Thanks for letting us know
the solution.