Skip to content

Instantly share code, notes, and snippets.

@markgodiy
Created April 4, 2023 17:42
Show Gist options
  • Select an option

  • Save markgodiy/3690dc48e97c0f7ad4f24bac55a7fa6c to your computer and use it in GitHub Desktop.

Select an option

Save markgodiy/3690dc48e97c0f7ad4f24bac55a7fa6c to your computer and use it in GitHub Desktop.
James_PrinterMapper
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(600, 400)
############################################## Start functions
# Function to return the name of the selected snaphsot from the second DropDown box
$Global:PrintServer = ""
function FetchPrinters {
$printerTxtFileLocation = 'C:\Temp\PrinterList.txt'
# $Global:PrintServer = $Global:DropDownBox1.SelectedItem.ToString()
Get-Printer -ComputerName "bamcpsvip" | where {$_.Name -like "bamc_h_lab*"} | Select-Object -ExpandProperty Name | Sort-Object -CaseSensitive | Out-File "$printerTxtFileLocation" -Force
$PrinterList = Get-Content -Path "$printerTxtFileLocation" -Force
foreach ($printer in $PrinterList) {
[void] $DropDownBox2.Items.Add($printer)
}
$outputBox.text = "Fetching list of printers on $bamcpsvip"
}
$Global:Printer = ""
function procInfo {
param([string]$Server,[string]$Printer)
$ConnectionName = "\\$Server\$Printer"
write-host $ConnectionName
try {
Add-Printer -ConnectionName $ConnectionName
} catch {
write-host "Something went wrong. Unable to add printer."
}
}
############################################## end functions
############################################## Start drop down boxes
#----------------------------------------------------------
# Dropdown Menu
#----------------------------------------------------------
# $DropDownBox = New-Object System.Windows.Forms.ComboBox
# $DropDownBox.Location = New-Object System.Drawing.Size(20, 50)
# $DropDownBox.Size = New-Object System.Drawing.Size(180, 20)
# $DropDownBox.DropDownHeight = 200
$DropDownBox1 = New-Object System.Windows.Forms.ComboBox
$DropDownBox1.Text = "Select Site Name"
$DropDownBox1.Size = New-Object System.Drawing.Size(190, 20)
$DropDownBox1.Location = New-Object System.Drawing.Point(20, 20)
$Sites = @('bamcpsvip','bamcps01','bamcps02')
ForEach ($Site in $Sites) {$DropDownBox1.Items.Add($Site) | out-null}
$DropDownBox1.add_SelectedIndexChanged($Global:DropDownBox1_SelectedIndexChanged)
$Form.Controls.Add($DropDownBox1)
$DropDownBox2 = New-Object System.Windows.Forms.ComboBox
$DropDownBox2.Text = "Select the Printer to install"
$DropDownBox2.Location = New-Object System.Drawing.Size(20, 60)
$DropDownBox2.Size = New-Object System.Drawing.Size(180, 20)
$DropDownBox2.DropDownHeight = 200
$DropDownBox2.add_SelectedIndexChanged({ })
$Form.Controls.Add($DropDownBox2)
############################################## end drop down boxes
############################################## Start text fields
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10, 150)
$outputBox.Size = New-Object System.Drawing.Size(565, 200)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Vertical"
$Form.Controls.Add($outputBox)
############################################## end text fields
############################################## Start buttons
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(400, 30)
$Button.Size = New-Object System.Drawing.Size(110, 80)
$Button.Text = "Map Printer"
$Form.Controls.Add($Button)
$Button2 = New-Object System.Windows.Forms.Button
$Button2.Location = New-Object System.Drawing.Size(275, 30)
$Button2.Size = New-Object System.Drawing.Size(110, 80)
$Button2.Text = "Load BAMC Printer list"
$Button2.Add_Click( { FetchPrinters })
$Form.Controls.Add($Button2)
############################################## end buttons
$Form.Add_Shown( { $Form.Activate() })
$DropDownBox1.Add_SelectionChangeCommitted({
$script:SelectedServer = $DropDownBox1.SelectedItem
Write-Host $SelectedServer
})
$DropDownBox2.Add_SelectionChangeCommitted({
$script:SelectedPrinter = $DropDownBox1.SelectedItem
Write-Host $SelectedPrinter
})
$Button.Add_Click({ procInfo -Server $script:SelectedServer -Printer $script:SelectedPrinter})
[void] $Form.ShowDialog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment