启用打印机共享并设置共享名称

时间:2011-05-20 12:23:03

标签: windows powershell vbscript

有没有人知道在本地打印机上设置共享的脚本并将共享名称设置为打印机本身的名称?操作系统是Windows Server 2008 R2 Service Pack 1

我已经知道了:

strComputer = "."
Set objWMIService = GetObject( _
    "winmgmts:" & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
 If objPrinter.Shared = "False" Then
    ObjPrinter.Shared = "True"
    ObjPrinter.ShareName = "objPrinter.Name"
 End If
Next

但我不知道如何将printername解析为ObjPrinter.ShareName。我想要一个Powershell或VBScript。该脚本似乎没有以这种方式工作。希望有人能够帮助我。

2 个答案:

答案 0 :(得分:2)

PowerShell启用或禁用打印机上的共享 http://winplat.net/2015/12/04/powershell-to-share-and-unshared-the-printers/

共享打印机

Set-Printer -Name DummyPrinter -Shared $True -Published $True -ShareName MyDummyPrinter

在远程计算机上共享打印机

Set-Printer -Name DummyPrinter -Shared $True -Published $True -ShareName MyDummyPrinter -ComuterName PrintSvr01

其中, DummyPrinter是打印队列的名称, MyDummyPrinter是所需的共享名称 PrintSvr01是托管打印机的远程服务器。

请注意,参数-Publish启用了“目录列表”选项。如果您不想要该选项,可以省略。

多台打印机?

get-printer -ComputerName PrintSvr01 | foreach{Set-Printer -name $_.name -Shared $true -ShareName $_.name -Published $true -ComputerName PrintSvr01}

取消共享打印机

要取消共享,请将-Shared参数设置为$ False Set-Printer -Name DummyPrinter -Shared $True -ComuterName PrintSvr01

答案 1 :(得分:1)

基本上,您只需删除TrueFalseobjPrinter.Name周围的引号,然后使用Put_保存更改。我还建议添加错误处理,因为不能共享所有类型的打印机。生成的代码如下所示:

strComputer = "."
Set objWMIService = GetObject( _
    "winmgmts:" & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
 If objPrinter.Shared = False Then
     objPrinter.Shared = True
     objPrinter.ShareName = objPrinter.Name

     On Error Resume Next
     objPrinter.Put_
     msg = Err.Description
     On Error GoTo 0
     If msg <> "" Then
        MsgBox "Cannot share " & objPrinter.Name & ": " & msg
     End If
 End If
Next