Add Printer App


In a multi customer environment (AD), I’ve found no easy way of limiting the printservers a user can reach from the normal Windows Add Printers. Therefore I’ve created a small Powershell app to add printers from a printserver. To be able to use this, you have to do the following on the print server:
Add your group of users (i.e “Domain Users”) to the group «Distributed COM Users» on the print server
Open Computer Management, go to Services and Applications, right click «WMI Control» and hit Properties. Go to the Security tab, find «Root\cimv2» and mark this. Click the «Security» button.

Click “Advanced”, then “Add”. Choose your group (i.e “Domain Users”) and give the “Enable Account” and “Remote Enable” rights:
Hit “OK” and restart the service “Windows Management Instrumation”

Now create a shortcut to the app, containing the printserver name:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -nologo -File Add_Printer_Util.ps1 PRINTSERVER01

This is the app in use:

Code:

Param(
        [string]$PrintServer = $(throw "PrintServer required")
)

#Generated Form Function
function GenerateForm {
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion
[System.Windows.Forms.Cursor]::Current = 'WaitCursor'
    
#region Generated Form Objects
$AddPrinter_1 = New-Object System.Windows.Forms.Form
$Printers_Box = New-Object System.Windows.Forms.ComboBox
$Add = New-Object System.Windows.Forms.Button
$InfoLabel = New-Object System.Windows.Forms.Label
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

$Add_OnClick=
{
    if($Printers_Box.SelectedItem -eq $null)
    {
        [System.Windows.Forms.MessageBox]::Show("Please select a printer." , "Add Printer" , 0)
        return
    }

    $Default = [System.Windows.Forms.MessageBox]::Show("Do you want to set this as your default printer?" , "Add Printer" , 4)
    if($Default -eq "YES")
    {
        [System.Windows.Forms.Cursor]::Current = 'WaitCursor'
        $Printer = $Printers_Box.SelectedItem
        $PrinterShare = "\\" + $PrintServer + "\" + $Printer

        $net = new-Object -com WScript.Network
        $net.AddWindowsPrinterConnection($PrinterShare)
 
        ## Set default printer
        $a = Get-WMIObject -query "Select * From Win32_Printer" | Where-Object{$_.Name -eq "$PrinterShare"}
        $a.SetDefaultPrinter()

        [System.Windows.Forms.MessageBox]::Show("Printer added and set as default" , "Add Printer" , 0)
        return
    }
    else
    {
        [System.Windows.Forms.Cursor]::Current = 'WaitCursor'
        $net = new-Object -com WScript.Network
        $net.AddWindowsPrinterConnection($PrinterShare)
        [System.Windows.Forms.MessageBox]::Show("Printer added" , "Add Printer" , 0)
        return
    }
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$AddPrinter_1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 100
$System_Drawing_Size.Width = 300
$AddPrinter_1.ClientSize = $System_Drawing_Size
$AddPrinter_1.DataBindings.DefaultDataSourceUpdateMode = 0
$AddPrinter_1.Name = "AddPrinter_1"
$AddPrinter_1.Text = "Add Printer"

#Printers_Box
$Printers_Box.DataBindings.DefaultDataSourceUpdateMode = 0
$Printers_Box.DropDownStyle = 2
$Printers_Box.FormattingEnabled = $True
[System.Windows.Forms.Cursor]::Current = 'WaitCursor'
$Printers = (gwmi win32_printer -computername $PrintServer | select Name | sort Name).Name
foreach($Printer in $Printers)
{
    if($Printer -ne "Microsoft XPS Document Writer")
    {
        $Printers_Box.Items.Add("$Printer")|Out-Null
    }
}
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 43
$Printers_Box.Location = $System_Drawing_Point
$Printers_Box.Name = "Printers_Box"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 200
$Printers_Box.Size = $System_Drawing_Size
$Printers_Box.TabIndex = 0
$Printers_Box.add_SelectedIndexChanged($handler_Customer_Box_SelectedIndexChanged)

$AddPrinter_1.Controls.Add($Printers_Box)
#End Printers_Box

#Add Button
$Add.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 220
$System_Drawing_Point.Y = 43
$Add.Location = $System_Drawing_Point
$Add.Name = "Add"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 75
$Add.Size = $System_Drawing_Size
$Add.TabIndex = 1
$Add.Text = "Add"
$Add.UseVisualStyleBackColor = $True
$Add.add_Click($Add_OnClick)

$AddPrinter_1.Controls.Add($Add)
#End Add Button

#InfoLabel
$InfoLabel.DataBindings.DefaultDataSourceUpdateMode = 0
$InfoLabel.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8,1,3,0)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 20
$InfoLabel.Location = $System_Drawing_Point
$InfoLabel.Name = "InfoLabel"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 200
$InfoLabel.Size = $System_Drawing_Size
$InfoLabel.TabIndex = 12
$InfoLabel.Text = "Choose Printer and click Add"
$InfoLabel.TextAlign = 1

$AddPrinter_1.Controls.Add($InfoLabel)
#End InfoLabel

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $AddPrinter_1.WindowState
#Init the OnLoad event to correct the initial state of the form
$AddPrinter_1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$AddPrinter_1.ShowDialog()| Out-Null

} #End Function

#Call the Function

GenerateForm

Leave a comment

Your email address will not be published. Required fields are marked *