有没有人知道在本地打印机上设置共享的脚本并将共享名称设置为打印机本身的名称?操作系统是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。该脚本似乎没有以这种方式工作。希望有人能够帮助我。
答案 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)
基本上,您只需删除True
,False
和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