windows 在打印机上启用共享并设置共享名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6071835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:53:15  来源:igfitidea点击:

Enable sharing on printer and set sharename

windowspowershellvbscript

提问by shadowman12

Does anyone know a script that sets sharing on a local printer and sets the share name to the name of the printer itself? The OS is Windows Server 2008 R2 Service Pack 1

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

I already got this:

我已经得到了这个:

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

But I don't know how parse the printername into the ObjPrinter.ShareName. I would like a Powershell or VBScript. The script doesn't seem to be working in this way. Hopefully someone is able to help me.

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

采纳答案by shadowman12

Basically, you just have to remove the quotes around True, False, and objPrinter.Nameand use Put_to save the changes. I also would recommend to add error handling, because no every type of printer can be shared. The resulting code would look like this:

基本上,你只需要围绕删除引号TrueFalse以及objPrinter.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

回答by Sushant

PowerShell to enable or disable sharing on Printers http://winplat.net/2015/12/04/powershell-to-share-and-unshared-the-printers/

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

To share a printer

共享打印机

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

To share a printer on remote computer

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

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

Where, DummyPrinteris the name of the print queue, MyDummyPrinteris the desired shared name and PrintSvr01is the remote server that hosts the printer.

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

Please note, the parameter -Publishenabled the option ‘List in Directory'. You can omit if you do not want the option.

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

Multiple Printers?

多台打印机?

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

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

To unshare a printer

取消共享打印机

To unshare, set the –Shared parameter to $False Set-Printer -Name DummyPrinter -Shared $True -ComuterName PrintSvr01

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