Initially the dropdown list displays all printers in the same subnet as the computer but if the user can't find the required printer the "show all printers" box can be selected and all printers will be displayed.
I recommend using it in conjunction with the Group Policy Point and Print Restrictions so that users without administrative privileges can install printer drivers.
Also I recommend running it from a shortcut with the following command:
C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe -executionpolicy bypass -noexit -windowstyle hidden -file MapPrinter.ps1
And the following is the script:
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
$dir = (Get-ChildItem $env:SystemRoot\WinSxS | ? {$_.Name -match "amd64_microsoft-windows-printing-
fdprint"}).FullName
$icon = "$dir\print_property.ico"
$info = new-object System.Windows.Forms.NotifyIcon
$info.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($icon)
#[System.Drawing.SystemIcons]::WinLogo
$info.Visible = $true
Unregister-Event -SourceIdentifier click_event -ErrorAction SilentlyContinue
Register-ObjectEvent $info MouseClick -sourceIdentifier click_event -Action {
$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$root = $dom.GetDirectoryEntry()
$search = [System.DirectoryServices.DirectorySearcher]$root
$search.Filter = "(objectCategory=printQueue)"
$result = $search.FindAll()
$network = Get-WmiObject win32_networkadapterconfiguration -Filter "IPEnabled = $True"
$subnetmask = $network.IPSubnet[0]
$IPAddress = $network.IPAddress[0]
function toBinary ($dottedDecimal){
$dottedDecimal.split(".") | %{$binary=$binary + $([convert]::toString($_,2).padleft(8,"0"))}
return $binary
}
function toDottedDecimal ($binary){
do {$dottedDecimal += "." + [string]$([convert]::toInt32($binary.substring($i,8),2)); $i+=8 } while ($i -le 24)
return $dottedDecimal.substring(1)
}
$ipBinary = toBinary $IPAddress
$smBinary = toBinary $subnetmask
$netBits=$smBinary.indexOf("0")
$subnet = toDottedDecimal $($ipBinary.substring(0,$netBits).padright(32,"0"))
$subnet = $subnet.split(".")
$subnet = $subnet[0..2] -join "."
Function Return-DropDown([Object]$DropDown){
$choice = $DropDown.SelectedItem.ToString()
$UNC = $printers.item($choice)
$printer = New-Object -com Wscript.Network
$printer.AddWindowsPrinterConnection($UNC)
}
$Form = New-Object System.Windows.Forms.Form
$Form.AutoSize = $true
$Form.startposition = "centerscreen"
$Form.Font = New-Object System.Drawing.Font("Arial",10,[System.Drawing.FontStyle]::Bold)
$Form.Text = "Printer Mapping Tool"
$Form.BackColor = "#013D6F"
$Form.ForeColor = "White"
$Form.Add_KeyDown({if($_.KeyCode -eq "Enter"){}})
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$Form.Close()}})
$Button = new-object System.Windows.Forms.Button
$Button.Text = "CONNECT PRINTER"
$Button.Add_Click({Return-DropDown($DropDown)})
$Button.Location = new-object System.Drawing.Size(60,90)
$Button.Size = new-object System.Drawing.Size(170,20)
$Button.Add_MouseHover({$Button.backcolor = [System.Drawing.Color]::CornflowerBlue})
$Button.Add_MouseLeave({$Button.backcolor = "#013D6F"})
$Checkbox = new-object System.Windows.Forms.Checkbox
$Checkbox.Location = new-object System.Drawing.Size(10,150)
$Checkbox.size = new-object System.Drawing.Size(20,40)
$Script:DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(10,20)
$DropDown.Size = new-object System.Drawing.Size(270,30)
$Label = new-object System.Windows.Forms.Label
$Label.Location = new-object System.Drawing.Size(30,163)
$Label.size = new-object System.Drawing.Size(200,40)
$Label.Text = "Show all printers."
$checkbox.Add_CheckStateChanged({
If ($Checkbox.Checked) {
$script:printers = $null
$DropDown.Items.Clear()
foreach($_ in $result){
$obj = [ADSI]$_.path
$portname = ($obj.portname).ToString()
$name = ($obj.name).ToString()
$uNCName = $obj.uNCName
$script:printers += @{$name=$uNCName}
}
} Else {
$script:printers = $null
$DropDown.Items.Clear()
foreach($_ in $result){
$obj = [ADSI]$_.path
$portname = ($obj.portname).ToString()
$name = ($obj.name).ToString()
$uNCName = $obj.uNCName
if($name -match $portname){
Try{$portip = [System.Net.Dns]::GetHostByName
($portname).addresslist.ipaddresstostring}
Catch{$portip=$null}
}
if($portname -match $subnet -or ($portip -match $subnet -and $portip -ne $null)){
$script:printers += @{$name=$uNCName}
$portip=$null
}
}
}
ForEach ($_ in ($printers.keys | sort)) {
$DropDown.Items.Add($_.ToString()) | Out-Null
}
})
$script:printers = $null
$DropDown.Items.Clear()
foreach($_ in $result){
$obj = [ADSI]$_.path
$portname = ($obj.portname).ToString()
$name = ($obj.name).ToString()
$uNCName = $obj.uNCName
if($name -match $portname){
Try{$portip = [System.Net.Dns]::GetHostByName
($portname).addresslist.ipaddresstostring}
Catch{$portip=$null}
}
if($portname -match $subnet -or ($portip -match $subnet -and $portip -ne $null)){
$script:printers += @{$name=$uNCName}
$portip=$null
}
}
ForEach ($_ in ($printers.keys | sort)) {
$DropDown.Items.Add($_.ToString()) | Out-Null
}
$Form.Controls.Add($Button)
$Form.Controls.Add($Checkbox)
$Form.Controls.Add($DropDown)
$Form.Controls.Add($Label)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
} | Out-Null
No comments:
Post a Comment