'****************************************
'* IntelliAdmin.com Script              *
'* Allows you to start or stop          *
'* A service across an entire domain    *
'****************************************

'Global Variables
Dim sDomain
Dim sServiceName
Dim sUserName
Dim sPassword
Dim sAction
Dim iReturnCode

'Used to execute our command line utility and display the result
Sub ExecuteUtility(sComputerName, sCommandLine)

 Dim poProcess
 Dim sErrorMsg

 'Execute the command line program
 Set poProcess = WShell.Exec(sCommandLine)
  
 'Wait for it to finish
 do while poProcess.status=0
  WScript.Sleep 500
 loop
 
 'Did it complete with success?
 if (poProcess.ExitCode=0) then
  'Yes, let the user know it has completed
  if (UCase(Trim(sAction))="STOP") then
   WScript.Echo "Service " & sServiceName &  " stopped on " & sComputerName & " successfully"
  else
   WScript.Echo "Service " & sServiceName &  " started on " & sComputerName & " successfully"
  end if
 else
  sErrorMsg = DisplayResult(poProcess.ExitCode)
  if (UCase(sAction)="STOP") then
   WScript.Echo "Could not stop service "  & sServiceName & " on " & sComputerName & " because: " & sErrorMsg
  else
   WScript.Echo "Could not start service "  & sServiceName & " on " & sComputerName & " because: " & sErrorMsg
  end if
 end if

 'Help our garbage collector
 poProcess = nil
End Sub


'Function to convert error numbers into nice descriptions
Function DisplayResult(iError)
 Dim sMessage
 
 select Case iError
  case "-2"
   sMessage="Access denied"
  case "-3"
   sMessage="Invalid Parameters"
  case "-4"
   sMessage="Duplicate Parameter"
  case "-6"
   sMessage="Service is already running"
  case "-7"
   sMessage="Service is disabled"
  case "-8"
   sMessage="Service is not running"
  case "-9"
   sMessage="Service is missing files"
  case "-10"
   sMessage="Service path is not valid"
  case "-11"
   sMessage="Service does not exist"
  case "-12"
   sMessage="Unknown Error"
  case "-13"
   sMessage="Missing arguments"
  case "-14"
   sMessage="Could not connect to remote host"
 end Select

 DisplayResult=sMessage

end function



if (WScript.Arguments.Count<3) then
 sMessage="***************************************************************************" & Chr(13) & Chr(10)
 sMessage=sMessage & "* Domain Service Manager                                                  *" & Chr(13) & Chr(10)
 sMessage=sMessage & "* Stop or start services across an entire domain                          *" & Chr(13) & Chr(10)
 sMessage=sMessage & "* Syntax:                                                                 *" & Chr(13) & Chr(10)
 sMessage=sMessage & "* RemoteService.vbs <Domain> <action> <ServiceName> [UserName] [Password] *" & Chr(13) & Chr(10)
 sMessage=sMessage & "* Possible Actions: Stop, Start                                           *" & Chr(13) & Chr(10)
 sMessage=sMessage & "* Username and password are optional                                      *" & Chr(13) & Chr(10)
 sMessage=sMessage & "* --------------------------------------------------------------          *" & Chr(13) & Chr(10)
 sMessage=sMessage & "* Windows Admin Tips & Software: http://www.intelliadmin.com              *" & Chr(13) & Chr(10)
 sMessage=sMessage & "***************************************************************************" & Chr(13) & Chr(10)
 WScript.Echo(sMessage)

else
 sDomain      = WScript.Arguments.Item(0)
 sAction      = WScript.Arguments.Item(1)
 sServiceName = WScript.Arguments.Item(2)

 if (Wscript.Arguments.Count>3) then
  sUserName    = WScript.Arguments.Item(3) 
 end if
 if (Wscript.Arguments.Count>4) then
  sPassword    = WScript.Arguments.Item(4)
 end if

 'Check to make sure the action is valid
 if ((UCase(sAction)<>"START") and (UCase(sAction)<>"STOP")) then
  WScript.Echo "Invalid Action (" & sAction & ") - must use START or STOP"
 else

  'Get domain object so we can query a list of computers
  Set DomObj = GetObject("WinNT://" & sDomain )

  'Filter only computer objects
  DomObj.Filter = Array("computer")

  'Get a shell so we can execute our cmd line program
  set WShell = CreateObject("WScript.Shell")

  'Wscript.Echo "Return Code: " & WShell.Run("remoteservice.exe")

  'Loop through all computers and execute our RemoteService.exe program
  For Each sComputer In DomObj
   ExecuteUtility sComputer.Name, "RemoteService.Exe \\" & sComputer.Name & " " & sAction & " " & sServiceName & " " & Chr(34) & sUserName & Chr(34) & " " & Chr(34) & sPassword & Chr(34)
  Next
 end if
end if
