我目前遇到一个问题,打印队列卡在中央打印服务器(Windows Server 2008)上。使用“清除所有文档”功能无法清除它,而且也会卡住。我需要非管理员用户能够从他们的工作站清除打印队列。
我尝试使用我创建的以下 winforms 程序,并允许用户停止打印后台处理程序,删除“C:\Windows\System32\spool\PRINTERS 文件夹”中的打印机文件,然后启动打印后台处理程序,但此功能要求程序以管理员身份运行,我怎样才能允许我的普通用户执行该程序而不授予他们管理员权限?
或者是否有其他方法可以允许普通用户清除服务器上的打印队列?
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
答案1
您可以创建一个计划任务,设置为以管理员身份运行,并授予普通用户启动该任务的权限。有点像 setuid 在 unix 上的工作方式。
但是,这对于您的问题来说不是必需的。您可以更改打印后台处理程序服务的权限,以便普通用户可以启动和停止它。但对于 serverfault 来说,这是一个更好的问题。
答案2
使用“runas”动词执行删除命令:
var p = new Process();
p.StartInfo.Verb = "runas";
p.StartInfo.FileName = "cmd.exe";
//add your delete command, etc. as args to the process
您还可以通过修改应用程序的清单来使您的应用程序通常需要提升:
https://stackoverflow.com/questions/1215443/show-uac-prompt-when-launching-an-app