Turdie
Turdie

Reputation: 123

Enable sharing on printer and set sharename

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

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.

Upvotes: 1

Views: 7904

Answers (2)

Sushant
Sushant

Reputation: 21

PowerShell to enable or disable sharing on Printers 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, DummyPrinter is the name of the print queue, MyDummyPrinter is the desired shared name and PrintSvr01 is the remote server that hosts the printer.

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

Multiple Printers?

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

Upvotes: 2

user128300
user128300

Reputation:

Basically, you just have to remove the quotes around True, False, and objPrinter.Name and 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:

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

Upvotes: 1

Related Questions