Jump to content

Service Restart Script for Non Admin Users


spudw2k
 Share

Recommended Posts

This information isn't strictly related to AutoIt--in fact the script was the easiest part--but I just finished an exercise that I hadn't had to do before but others may find useful so I wanted to share my findings and results.

At my place of work we have a "Desktop Admin" group which has admin privs on desktops (who da thunk) but not servers.  Our print server has daily hangups and our resolution is to restart the print spooler service.  We have a 24hr support model and only Desktop Admins are filling the night shifts.  If the print server or queue gets hung, they are were not able to restart the service.  We are unable to grant them login or admin privs on the server so I tried another route.

I learned that a Discrentionary Access Control List (DACL) can be applied to individual services. In addition to the Print Spooler service, at least status permission needed to be granted to the Service Control Manager service (scmanager).

I followed the following links for instructions to grant permissions to the SID of the Desktop Admin group.

http://networkadminkb.com/KB/a159/how-to-troubleshoot-access-to-sc-manager-other-object-access.aspx

Here is the simple script I put together for the Desktop Admins to run in order to restart the service.

Main()

Func Main()   ;Main Function
    $server = "PAXPS01"   ;Host with Spooler Service
    ProgressOn("Restart Print Spooler Service", "", "", -1, -1, 3)   ;UI Feedback - Initialize
    If _IsSpoolerRunning($server) Then   ;If Spooler Service is Running then Restart
        _RestartSpooler($server)   ;Restart Spooler Service Routine Function
    ElseIf _IsSpoolerStopped($server) Then   ;Else If Spooler Service is Stopped then Start
        _StartSpooler($server)   ;Start Spooler Service Function
    EndIf
    ProgressSet(100, "", "Print Spooler Restarted Successfully.")   ;UI Feedback - Completed
    MsgBox(0, "Restart Print Spooler", "Complete")   ;Alert Feedback - Completion
EndFunc   ;==>Main

Func _AccessDenied()   ;Access Denied
    MsgBox(0, "Restart Print Spooler", "Access Denied")   ;Alert Feedback - Access Denied Error
    Exit   ;Abort Script
EndFunc   ;==>_AccessDenied

Func _GetSTDOut($pid)   ;Get StdOut from Console Executable by Process ID
    Local $output   ;Stdout String Variable
    While 1   ;Do While PID is Running
        $output &= StdoutRead($pid)   ;Capture Stdout from PID
        If @error Then ExitLoop   ;Exit Loop if Error Occurs (PID Terminated or Non-Existing)
    WEnd
    Return $output   ;Return Stdout Captured from PID
EndFunc   ;==>_GetSTDOut

Func _IsSpoolerRunning($server)   ;Determine if Spooler Service is Running
    Return StringInStr(_RunSCCommand($server, "query", "Spooler"), "RUNNING")
EndFunc   ;==>_IsSpoolerRunning

Func _IsSpoolerStopped($server)   ;Determine if Spooler Service is Stopped
    Return StringInStr(_RunSCCommand($server, "query", "Spooler"), "STOPPED")
EndFunc   ;==>_IsSpoolerStopped

Func _RestartSpooler($server)   ;Restart Spooler Service Routine Function
    _StopSpooler($server)   ;Send Stop Command to Spooler Service
    Do   ;Loop Until Spooler Service is Stopped
        Sleep(1000)
    Until _IsSpoolerStopped($server)
    _StartSpooler($server)    ;Send Start Command to Spooler Service
    Do   ;Loop Until Spooler Service is Running
        Sleep(1000)
    Until _IsSpoolerRunning($server)
EndFunc   ;==>_RestartSpooler

Func _RunSCCommand($server, $cmd, $service)   ;Execute Service Control Command & Return Stdout
    $stdOut = _GetSTDOut(Run(@ComSpec & " /c sc.exe \\" & $server & " " & $cmd & " " & $service, @ScriptDir, @SW_HIDE, 0x8))
    If StringInStr($stdOut, "Access is Denied.") Then _AccessDenied()   ;Check for Access Denied string in Stdout
    Return $stdOut   ;Return Stdout from SC Command
EndFunc   ;==>_RunSCCommand

Func _StartSpooler($server)   ;Send Start command to Spooler Service
    ProgressSet(50, "", "Starting Print Spooler Service")   ;Set UI Feedback State (Starting)
    _RunSCCommand($server, "start", "Spooler")
EndFunc   ;==>_StartSpooler

Func _StopSpooler($server)   ;Send Stop command to Spooler Service
    ProgressSet(0, "", "Stopping Print Spooler Service")   ;Set UI Feedback State (Stopping)
    _RunSCCommand($server, "stop", "Spooler")
EndFunc   ;==>_StopSpooler
Edited by spudw2k
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...