1

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
JamesCW
  • 309
Reafidy
  • 319
  • 1
  • 8
  • 18

2 Answers2

1

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:

https://stackoverflow.com/questions/1215443/show-uac-prompt-when-launching-an-app

Paul Tyng
  • 111
  • 2
1

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.

Ben Voigt
  • 472