Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I currently have an issue where the print queue is getting stuck on a central print server (windows server 2008). Using the "Clear all documents" function does not clear it and gets stuck too. I need non-admin users to be able to clear the print queue from their work stations.

I have tried using the following winforms program which I created and allows a user to stop the print spooler, delete printer files in the "C:\Windows\System32\spool\PRINTERS folder" and then start the print spooler but this functionality requires the program to be run as an administrator, how can I allow my normal users to execute this program without giving them admin privileges?

Or is there another way I can allow normal user to clear the print queue on the server?

Imports System.ServiceProcess
Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ClearJammedPrinter()
    End Sub
    Public Sub ClearJammedPrinter()
        Dim tspTimeOut As TimeSpan = New TimeSpan(0, 0, 5)
        Dim controllerStatus As ServiceControllerStatus = ServiceController1.Status

        Try

            If ServiceController1.Status <> ServiceProcess.ServiceControllerStatus.Stopped Then
                ServiceController1.Stop()
            End If

            Try
                ServiceController1.WaitForStatus(ServiceProcess.ServiceControllerStatus.Stopped, tspTimeOut)
            Catch
                Throw New Exception("The controller could not be stopped")
            End Try

            Dim strSpoolerFolder As String = "C:\Windows\System32\spool\PRINTERS"

            Dim s As String
            For Each s In System.IO.Directory.GetFiles(strSpoolerFolder)
                System.IO.File.Delete(s)
            Next s

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Try

                Select Case controllerStatus
                    Case ServiceControllerStatus.Running
                        If ServiceController1.Status <> ServiceControllerStatus.Running Then ServiceController1.Start()
                    Case ServiceControllerStatus.Stopped
                        If ServiceController1.Status <> ServiceControllerStatus.Stopped Then ServiceController1.Stop()
                End Select

                ServiceController1.WaitForStatus(controllerStatus, tspTimeOut)
            Catch
                MsgBox(String.Format("{0}{1}", "The print spooler service could not be returned to its original setting and is currently: ", ServiceController1.Status))
            End Try
        End Try
    End Sub
End Class
share|improve this question

migrated from stackoverflow.com Nov 10 '11 at 0:43

This question came from our site for professional and enthusiast programmers.

1  
You have the option to either impersonate a user whom is an admin (with credentials stored in settings/somewhere else) or to pop up the UAC prompt to run as an elevated user. Which are you after? –  Reddog Nov 9 '11 at 0:56
    
See this question: stackoverflow.com/questions/2532769/… possible duplicate –  Paul Tyng Nov 9 '11 at 0:56
    
@Reddog, Thanks, I would prefer to have the process run with little disruption to the user so impersonation sounds good but happy with anyway that works. –  Reafidy Nov 9 '11 at 1:00
    
@Paul T. I don't think that article is of use, I cant elevate because I need a normal user to run the application. –  Reafidy Nov 9 '11 at 1:03
    
Sorry I declined your flag before I saw the commentary in the answers below. Anyhow, is this code (above) running on the users workstation or on the server itself? –  Kev Nov 9 '11 at 23:50

2 Answers 2

up vote 1 down vote accepted

you can create a scheduled task which is set to run as admin, and give normal users the right to start it. sort of like how setuid works on unix.

however, this isn't necessary for your problem. you can change the permissions on the print spooler service so that normal users can start and stop it. but that's a better question for serverfault.

share|improve this answer
    
Ok thanks Ben, I think I can manage that, what about deleting the files in the windows/system32 folder though? –  Reafidy Nov 9 '11 at 2:28
    
@reafidy: Run as a scheduled task, or change the permissions of the C:\Windows\System32\spool\PRINTERS directory. (Don't change the permissions of C:\Windows or C:\Windows\System32, just the spooler subdirectory). –  Ben Voigt Nov 9 '11 at 3:44
    
I will setup a scheduled task, I'm guessing I do this on the server itself and somehow allow each of the users access to it via there workstations? –  Reafidy Nov 9 '11 at 4:02
    
@Reafidy: Wait, when did this question become an over-the-network operation? –  Ben Voigt Nov 9 '11 at 14:19
    
Sorry Ben, I should have pointed this out at the start. Everyone prints through a "print server" setup on our windows server. This occasionally gets jammed up and the only way to clear it is too stop the print spooler, clear the printer folder and restart. I am sick of doing this about once a week and want to allow our users to do it themselves. I need a holiday. :) –  Reafidy Nov 9 '11 at 18:02

Execute the delete command with the "runas" verb:

var p = new Process();
p.StartInfo.Verb = "runas";
p.StartInfo.FileName = "cmd.exe";

//add your delete command, etc. as args to the process

You could also just make your app require elevation in general by modifying its manifest:

Show UAC prompt when launching an app

share|improve this answer
    
I need a normal user to be able to execute the application. If I elevate the application then they will need admin username and password which they will not have. –  Reafidy Nov 9 '11 at 1:07
    
You can't really sidestep the UAC, that's the point of it. –  Paul Tyng Nov 9 '11 at 1:16
    
What about impersonation? –  Reafidy Nov 9 '11 at 2:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.