Advertisement
UlrichC

Printer Server Manager

Jul 1st, 2019
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ########################Add Types###################################################
  2. Add-Type -AssemblyName PresentationFramework
  3. Add-Type -AssemblyName System.Windows.Forms
  4. Add-Type -Name Window -Namespace Console -MemberDefinition '
  5. [DllImport("Kernel32.dll")]
  6. public static extern IntPtr GetConsoleWindow();
  7.  
  8. [DllImport("user32.dll")]
  9. public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
  10. '
  11.  
  12. ########################Starting Global Variables#######################################
  13. $ErrorActionPreference = "Continue"
  14.  
  15. $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
  16.  
  17. $Global:PrintServer = "sadmwps01.admin.com"
  18.  
  19. $CleintIPs = @("20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49")
  20.  
  21. #Function Storage
  22. Function Hide-Console
  23. {
  24.     $consolePtr = [Console.Window]::GetConsoleWindow()
  25.     #0 hide
  26.     [Console.Window]::ShowWindow($consolePtr, 0)
  27. }
  28. Hide-Console
  29.  
  30.  
  31. #####################WorkFlows############################################
  32. WorkFlow GroupPing{
  33.  
  34.     Param (
  35.     [Parameter(Mandatory=$true,Position=0)]$IPArray)
  36.  
  37.     ForEach -Parallel ($IP in $IPArray){
  38.     $PingResponse = Test-Connection -ComputerName $IP -Count 1 -Quiet
  39.     Write-Output "$IP $PingResponse"
  40.     }
  41. }
  42.  
  43. #####################Functions#############################################
  44. Function Get-LocationIP{
  45. If (!($ComboBox_Location.Text)) {[System.Windows.Forms.MessageBox]::Show("You must select a building from the dropdown.", "No Building Selected"); Return}
  46. ElseIf ($ComboBox_Location.Text -Like "*EBA*") {$Global:LocationIP = "02"}
  47. ElseIf ($ComboBox_Location.Text -Like "*ECN*") {$Global:LocationIP = "05"}
  48. ElseIf ($ComboBox_Location.Text -Like "*ECE*") {$Global:LocationIP = "04"}
  49. ElseIf ($ComboBox_Location.Text -Like "*ECH*") {$Global:LocationIP = "06"}
  50. ElseIf ($ComboBox_Location.Text -Like "*ECL*") {$Global:LocationIP = "08"}
  51. ElseIf ($ComboBox_Location.Text -Like "*ECO*") {$Global:LocationIP = "10"}
  52. ElseIf ($ComboBox_Location.Text -Like "*ECR*") {$Global:LocationIP = "15"}
  53. ElseIf ($ComboBox_Location.Text -Like "*EFO*") {$Global:LocationIP = "11"}
  54. ElseIf ($ComboBox_Location.Text -Like "*EGA*") {$Global:LocationIP = "12"}
  55. ElseIf ($ComboBox_Location.Text -Like "*EGL*") {$Global:LocationIP = "16"}
  56. ElseIf ($ComboBox_Location.Text -Like "*EHA*") {$Global:LocationIP = "20"}
  57. ElseIf ($ComboBox_Location.Text -Like "*EGI*") {$Global:LocationIP = "14"}
  58. ElseIf ($ComboBox_Location.Text -Like "*EHW*") {$Global:LocationIP = "19"}
  59. ElseIf ($ComboBox_Location.Text -Like "*EHE*") {$Global:LocationIP = "22"}
  60. ElseIf ($ComboBox_Location.Text -Like "*EHI*") {$Global:LocationIP = "24"}
  61. ElseIf ($ComboBox_Location.Text -Like "*EHL*") {$Global:LocationIP = "26"}
  62. ElseIf ($ComboBox_Location.Text -Like "*EHT*") {$Global:LocationIP = "27"}
  63. ElseIf ($ComboBox_Location.Text -Like "*EHO*") {$Global:LocationIP = "28"}
  64. ElseIf ($ComboBox_Location.Text -Like "*EHU*") {$Global:LocationIP = "30"}
  65. ElseIf ($ComboBox_Location.Text -Like "*EIL*") {$Global:LocationIP = "32"}
  66. ElseIf ($ComboBox_Location.Text -Like "*EIN*") {$Global:LocationIP = "34"}
  67. ElseIf ($ComboBox_Location.Text -Like "*ELA*") {$Global:LocationIP = "36"}
  68. ElseIf ($ComboBox_Location.Text -Like "*ELI*") {$Global:LocationIP = "23"}
  69. ElseIf ($ComboBox_Location.Text -Like "*ELN*") {$Global:LocationIP = "25"}
  70. ElseIf ($ComboBox_Location.Text -Like "*ELO*") {$Global:LocationIP = "38"}
  71. ElseIf ($ComboBox_Location.Text -Like "*ELW*") {$Global:LocationIP = "40"}
  72. ElseIf ($ComboBox_Location.Text -Like "*EMC*") {$Global:LocationIP = "42"}
  73. ElseIf ($ComboBox_Location.Text -Like "*ENA*") {$Global:LocationIP = "17"}
  74. ElseIf ($ComboBox_Location.Text -Like "*EOA*") {$Global:LocationIP = "44"}
  75. ElseIf ($ComboBox_Location.Text -Like "*EON*") {$Global:LocationIP = "46"}
  76. ElseIf ($ComboBox_Location.Text -Like "*EOT*") {$Global:LocationIP = "21"}
  77. ElseIf ($ComboBox_Location.Text -Like "*EPA*") {$Global:LocationIP = "48"}
  78. ElseIf ($ComboBox_Location.Text -Like "*EPR*") {$Global:LocationIP = "09"}
  79. ElseIf ($ComboBox_Location.Text -Like "*ERI*") {$Global:LocationIP = "50"}
  80. ElseIf ($ComboBox_Location.Text -Like "*ESH*") {$Global:LocationIP = "52"}
  81. ElseIf ($ComboBox_Location.Text -Like "*ESP*") {$Global:LocationIP = "13"}
  82. ElseIf ($ComboBox_Location.Text -Like "*ESU*") {$Global:LocationIP = "56"}
  83. ElseIf ($ComboBox_Location.Text -Like "*ESY*") {$Global:LocationIP = "07"}
  84. ElseIf ($ComboBox_Location.Text -Like "*ETI*") {$Global:LocationIP = "29"}
  85. ElseIf ($ComboBox_Location.Text -Like "*EWA*") {$Global:LocationIP = "58"}
  86. ElseIf ($ComboBox_Location.Text -Like "*EWY*") {$Global:LocationIP = "60"}
  87. ElseIf ($ComboBox_Location.Text -Like "*EWI*") {$Global:LocationIP = "62"}
  88. ElseIf ($ComboBox_Location.Text -Like "*EMO*") {$Global:LocationIP = "35"}
  89. ElseIf ($ComboBox_Location.Text -Like "*MAB*") {$Global:LocationIP = "66"}
  90. ElseIf ($ComboBox_Location.Text -Like "*MCA*") {$Global:LocationIP = "69"}
  91. ElseIf ($ComboBox_Location.Text -Like "*MEA*") {$Global:LocationIP = "72"}
  92. ElseIf ($ComboBox_Location.Text -Like "*MEL*") {$Global:LocationIP = "75"}
  93. ElseIf ($ComboBox_Location.Text -Like "*MKE*") {$Global:LocationIP = "86"}
  94. ElseIf ($ComboBox_Location.Text -Like "*MKI*") {$Global:LocationIP = "78"}
  95. ElseIf ($ComboBox_Location.Text -Like "*MLA*") {$Global:LocationIP = "81"}
  96. ElseIf ($ComboBox_Location.Text -Like "*MTE*") {$Global:LocationIP = "84"}
  97. ElseIf ($ComboBox_Location.Text -Like "*HBA*") {$Global:LocationIP = "89"}
  98. ElseIf ($ComboBox_Location.Text -Like "*HEL*") {$Global:LocationIP = "87"}
  99. ElseIf ($ComboBox_Location.Text -Like "*HDR*") {$Global:LocationIP = "89"}
  100. ElseIf ($ComboBox_Location.Text -Like "*HLA*") {$Global:LocationIP = "91"}
  101. ElseIf ($ComboBox_Location.Text -Like "*HSE*") {$Global:LocationIP = "93"}
  102. ElseIf ($ComboBox_Location.Text -Like "*HST*") {$Global:LocationIP = "95"}
  103. ElseIf ($ComboBox_Location.Text -Like "*CSP*") {$Global:LocationIP = "99"}
  104. ElseIf ($ComboBox_Location.Text -Like "*ADM*") {$Global:LocationIP = "99"}
  105. ElseIf ($ComboBox_Location.Text -Like "*TRA*") {$Global:LocationIP = "103"}
  106. ElseIf ($ComboBox_Location.Text -Like "*PLA*") {$Global:LocationIP = "95"}
  107. ElseIf ($ComboBox_Location.Text -Like "*FOO*") {$Global:LocationIP = "51"}
  108. ElseIf ($ComboBox_Location.Text -Like "*ESC*") {$Global:LocationIP = "99"}
  109. Else {}
  110. }
  111.  
  112. ######################WPF Graphical Code###################################
  113. [xml]$xaml = @"
  114. <Window
  115. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  116. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Window_Main"
  117.  
  118. Title="Print Server Manager" Height="450" Width="600" MinWidth="600" MinHeight="450" MaxWidth="600" MaxHeight="450">
  119.  
  120. <Grid>
  121.  
  122. <Grid Name = "Grid_PrinterManagement">
  123. <ListBox x:Name="ListBox_Printers" HorizontalAlignment="Right" Height="379" VerticalAlignment="Top" Width="285" Margin="0,30,5,0" TabIndex="2"/>
  124. <TextBox x:Name="TextBox_Search" HorizontalAlignment="Left" Height="20" Margin="135,30,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="150" ToolTip="Search all printers by name. You can perform wild card searches with the * chartacter." TabIndex="1"/>
  125. <Button x:Name="Button_Search" Content="Search" HorizontalAlignment="Left" Height="20" Margin="10,30,0,0" VerticalAlignment="Top" Width="120" ToolTip="Search all printers by name. You can perform wild card searches with the * chartacter." IsDefault="True" TabIndex="12"/>
  126. <Button x:Name="Button_CancelJobs" Content="Cancel Print Jobs" HorizontalAlignment="Left" Height="20" Margin="10,105,0,0" VerticalAlignment="Top" Width="120" ToolTip="Sometimes this is not very effective. If a job is corrupt, it probably won't help." TabIndex="7"/>
  127. <Button x:Name="Button_PrintTestPage" Content="Print Test Page" HorizontalAlignment="Left" Height="20" Margin="10,130,0,0" VerticalAlignment="Top" Width="120" ToolTip="Prints an ironic test page." TabIndex="8"/>
  128. <Button x:Name="Button_ChangePrinterName" Content="Change Printer Name" HorizontalAlignment="Left" Height="20" Margin="10,55,0,0" VerticalAlignment="Top" Width="120" ToolTip="Renames a printer, and updates the share name of the printer at the same time." TabIndex="4"/>
  129. <Button x:Name="Button_ChagnePrinterPort" Content="Change Printer IP" HorizontalAlignment="Left" Height="20" Margin="10,80,0,0" VerticalAlignment="Top" Width="120" ToolTip="Changes the printer IP address, and cleans up the old port." TabIndex="6"/>
  130. <TextBox x:Name="TextBox_NewPrinterName" HorizontalAlignment="Left" Height="20" Margin="135,55,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="150" ToolTip="Renames a printer, and updates the share name of the printer at the same time." TabIndex="3"/>
  131. <TextBox x:Name="TextBox_NewPrinterIP" HorizontalAlignment="Left" Height="20" Margin="135,80,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="150" ToolTip="Changes the printer IP address, and cleans up the old port." TabIndex="5"/>
  132. <TextBlock x:Name="TextBlock_Status" HorizontalAlignment="Left" Height="40" Margin="10,0,0,10" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Bottom" Width="275" LineHeight="12"/>
  133. <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="55" Margin="9,354,0,0" VerticalAlignment="Top" Width="276"/>
  134. <Button x:Name="Button_ShowPrinterIP" Content="Show Printer IP" HorizontalAlignment="Left" Height="20" Margin="10,155,0,0" VerticalAlignment="Top" Width="120" TabIndex="10"/>
  135. <Button x:Name="Button_PingPrinter" Content="Ping Printer" HorizontalAlignment="Left" Height="20" Margin="10,180,0,0" VerticalAlignment="Top" Width="120" TabIndex="11"/>
  136. <Button x:Name="Button_PrintFlush" Content="Perform Print Flush" HorizontalAlignment="Left" Height="20" Margin="9,205,0,0" VerticalAlignment="Top" Width="120" TabIndex="11"/>
  137. <Button x:Name="Button_AddNewPrinter" Content="Add Printer Wizard" HorizontalAlignment="Left" Height="20" Margin="9,230,0,0" VerticalAlignment="Top" Width="120" TabIndex="11"/>
  138. <Button x:Name="Button_RemovePrinter" Content="Remove Printer" HorizontalAlignment="Left" Height="20" Margin="9,255,0,0" VerticalAlignment="Top" Width="120" TabIndex="11"/>
  139. <Button x:Name="Button_SearchForIPs" Content="Search For Open IPs" HorizontalAlignment="Left" Height="20" Margin="9,280,0,0" VerticalAlignment="Top" Width="120" TabIndex="11"/>
  140. </Grid>
  141.  
  142. <Grid Name = "Grid_AddPrinter">
  143. <ListBox x:Name="ListBox_Drivers" Height="379" Margin="0,30,5,0" Width="285" VerticalAlignment="Top" HorizontalAlignment="Right" TabIndex="5"/>
  144. <TextBox x:Name="TextBox_Printer_ShareName" HorizontalAlignment="Left" Height="25" Margin="10,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" TabIndex="1"/>
  145. <TextBox x:Name="TextBox_IP_Port" HorizontalAlignment="Left" Height="25" Margin="10,115,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" TabIndex="2"/>
  146. <TextBlock HorizontalAlignment="Left" Height="25" Margin="10,30,0,0" TextWrapping="Wrap" Text="Printer / Share Name" VerticalAlignment="Top" Width="200" FontSize="16"/>
  147. <TextBlock HorizontalAlignment="Left" Height="25" Margin="10,90,0,0" TextWrapping="Wrap" Text="IP Address / Port" VerticalAlignment="Top" Width="200" FontSize="16"/>
  148. <TextBlock HorizontalAlignment="Left" Height="25" Margin="302,0,0,0" TextWrapping="Wrap" Text="Choose a Driver" VerticalAlignment="Top" Width="200" FontSize="16"/>
  149. <Button x:Name="Button_AddPrinter" Content="Add Printer" HorizontalAlignment="Left" Height="25" Margin="9,267,0,0" VerticalAlignment="Top" Width="200" TabIndex="6"/>
  150. <Button x:Name="Button_ReturnToPrintManagment" Content="Return to Main Menu" HorizontalAlignment="Left" Height="25" Margin="9,297,0,0" VerticalAlignment="Top" Width="200" TabIndex="7"/>
  151. <TextBlock HorizontalAlignment="Left" Height="25" Margin="10,0,0,0" TextWrapping="Wrap" Text="Chose a Name and IP" VerticalAlignment="Top" Width="200" FontSize="16"/>
  152. <TextBlock x:Name="TextBlock_Add_Printer_Status" HorizontalAlignment="Left" Height="40" Margin="10,0,0,10" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Bottom" Width="275" LineHeight="12"/>
  153. <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="55" Margin="9,354,0,0" VerticalAlignment="Top" Width="276"/>
  154. <TextBox x:Name="TextBox_Description" HorizontalAlignment="Left" Height="25" Margin="9,230,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
  155. <TextBox x:Name="TextBox_Location" HorizontalAlignment="Left" Height="25" Margin="10,170,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" TabIndex="3"/>
  156. <TextBlock HorizontalAlignment="Left" Height="25" Margin="10,200,0,0" TextWrapping="Wrap" Text="Description" VerticalAlignment="Top" Width="200" FontSize="16"/>
  157. <TextBlock HorizontalAlignment="Left" Height="25" Margin="10,145,0,0" TextWrapping="Wrap" Text="Location" VerticalAlignment="Top" Width="200" FontSize="16"/>
  158. </Grid>
  159.  
  160. <Grid Name="AngryIP">
  161. <ListBox x:Name="ListBox_Results" HorizontalAlignment="Right" Height="350" VerticalAlignment="Top" Width="200" Margin="0,50,20,0"/>
  162. <TextBlock HorizontalAlignment="Left" Margin="372,25,0,0" TextWrapping="Wrap" Text="IP Already in Use?" VerticalAlignment="Top" Height="25"/>
  163. <TextBlock HorizontalAlignment="Left" Margin="10,77,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="25"><Run Text="Select the VLAN"/><LineBreak/><Run/></TextBlock>
  164. <TextBlock HorizontalAlignment="Left" Margin="10,77,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="25"/>
  165. <TextBlock HorizontalAlignment="Left" Margin="10,25,0,0" TextWrapping="Wrap" Text="Select the Building" VerticalAlignment="Top" Height="25"/>
  166. <ComboBox x:Name="ComboBox_Location" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="10,50,0,0" Height="25">
  167.    <ListBoxItem Content="ADM (ESC / Admin building)"/>
  168.    <ListBoxItem Content="EBA (Bartlett Elementary)"/>
  169.    <ListBoxItem Content="ECE (Century Oaks Elementary)"/>
  170.    <ListBoxItem Content="ECH (Channing  Elementary)"/>
  171.    <ListBoxItem Content="ECL (Clinton Elementary)"/>
  172.    <ListBoxItem Content="ECN (Centennial Elementary)"/>
  173.    <ListBoxItem Content="ECO (Coleman Elementary)"/>
  174.    <ListBoxItem Content="ECR (Creekside Elementary)"/>
  175.    <ListBoxItem Content="EFO (Fox Meadow Elementary)"/>
  176.    <ListBoxItem Content="EGA (Garfield Elementary)"/>
  177.    <ListBoxItem Content="EGI (Harriet Gifford Elementary)"/>
  178.    <ListBoxItem Content="EGL (Glenbrook Elementary)"/>
  179.    <ListBoxItem Content="EHA (Hanover Countryside)"/>
  180.    <ListBoxItem Content="EHE (Heritage Elementary)"/>
  181.    <ListBoxItem Content="EHI (Highland Elementary)"/>
  182.    <ListBoxItem Content="EHL (Hillcrest Elementary)"/>
  183.    <ListBoxItem Content="EHO (Horizon Elementary)"/>
  184.    <ListBoxItem Content="EHT (Hilltop Elementary)"/>
  185.    <ListBoxItem Content="EHU (Huff Elementary)"/>
  186.    <ListBoxItem Content="EHW (Hawk Hollow Elementary)"/>
  187.    <ListBoxItem Content="EIN (Independance Elementary)"/>
  188.    <ListBoxItem Content="EIL (Illinois Park Elementary)"/>
  189.    <ListBoxItem Content="ELA (Laurel Hill Elementary)"/>
  190.    <ListBoxItem Content="ELI (Liberty Elementary)"/>
  191.    <ListBoxItem Content="ELN (Lincoln Elementary)"/>
  192.    <ListBoxItem Content="ELO (Lords Park Elementary)"/>
  193.    <ListBoxItem Content="ELW (Lowrie Elementary)"/>
  194.    <ListBoxItem Content="EMC (Mckinley Elementary)"/>
  195.    <ListBoxItem Content="ENA (Nature Ridge Elementary)"/>
  196.    <ListBoxItem Content="EOA (Oakhill Elementary)"/>
  197.    <ListBoxItem Content="EON (Ontarioville Elementary)"/>
  198.    <ListBoxItem Content="EOT (Otter Creek Elementary)"/>
  199.    <ListBoxItem Content="EPA (Parkwood Elementary)"/>
  200.    <ListBoxItem Content="EPR (Prairieview Elementary)"/>
  201.    <ListBoxItem Content="ERI (Ridge Circle Elementary)"/>
  202.    <ListBoxItem Content="ERO (Ronaol O'Neal / Sheridan Elementary)"/>
  203.    <ListBoxItem Content="ESP (Springtrail Elementary)"/>
  204.    <ListBoxItem Content="ESU (Sunnydale Elementary)"/>
  205.    <ListBoxItem Content="ESY (Sycamore Elementary)"/>
  206.    <ListBoxItem Content="ETI (Timber Trails Elementary)"/>
  207.    <ListBoxItem Content="EWA (Washington Elementary)"/>
  208.    <ListBoxItem Content="EWI (Willard Elementary)"/>
  209.    <ListBoxItem Content="EWY (Wayne Elementary)"/>
  210.    <ListBoxItem Content="FOO (Food Operations Office)"/>
  211.    <ListBoxItem Content="HBA (Bartlett High)"/>
  212.    <ListBoxItem Content="HEL (Elgin High)"/>
  213.    <ListBoxItem Content="HGI (Gifford Street High)"/>
  214.    <ListBoxItem Content="HLA (Larkin High)"/>
  215.    <ListBoxItem Content="HSE (South Elgin High)"/>
  216.    <ListBoxItem Content="HST (Streamwood High)"/>
  217.    <ListBoxItem Content="MAB (Abbott Middle)"/>
  218.    <ListBoxItem Content="MCA (Canton Middle)"/>
  219.    <ListBoxItem Content="MEA (Eastview Middle)"/>
  220.    <ListBoxItem Content="MEL (Ellis Middle)"/>
  221.    <ListBoxItem Content="MKE (Kenyon Woods Middle)"/>
  222.    <ListBoxItem Content="MKI (Kimball Middle)"/>
  223.    <ListBoxItem Content="MLA (Larsen Middle)"/>
  224.    <ListBoxItem Content="MTE (Tefft Middle)"/>
  225.    <ListBoxItem Content="PLA (Plant Operations)"/>
  226.    <ListBoxItem Content="TRA (Transportation)"/>
  227. </ComboBox>
  228.  
  229. <ComboBox x:Name="ComboBox_VLAN" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="10,102,0,0" Height="25">
  230.    <ListBoxItem Content="10"/>
  231.    <ListBoxItem Content="11"/>
  232.    <ListBoxItem Content="12"/>
  233.    <ListBoxItem Content="13"/>
  234.    <ListBoxItem Content="14"/>
  235.    <ListBoxItem Content="15"/>
  236.    <ListBoxItem Content="16"/>
  237.    <ListBoxItem Content="17"/>
  238.    <ListBoxItem Content="18"/>
  239.    <ListBoxItem Content="19"/>
  240.    <ListBoxItem Content="20"/>
  241.    <ListBoxItem Content="21"/>
  242.    <ListBoxItem Content="22"/>
  243.    <ListBoxItem Content="23"/>
  244.    <ListBoxItem Content="24"/>
  245.    <ListBoxItem Content="25"/>
  246.    <ListBoxItem Content="26"/>
  247.    <ListBoxItem Content="27"/>
  248.    <ListBoxItem Content="28"/>
  249.    <ListBoxItem Content="29"/>
  250.    <ListBoxItem Content="30"/>
  251.    <ListBoxItem Content="31"/>
  252.    <ListBoxItem Content="32"/>
  253.    <ListBoxItem Content="33"/>
  254.    <ListBoxItem Content="34"/>
  255.    <ListBoxItem Content="35"/>
  256.    <ListBoxItem Content="36"/>
  257.    <ListBoxItem Content="37"/>
  258.    <ListBoxItem Content="38"/>
  259.    <ListBoxItem Content="39"/>
  260. </ComboBox>
  261. <Button x:Name="Button_LookForIPs" Content="Scan This VLAN" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="10,132,0,0" Height="25"/>
  262. <Button x:Name="Button_ExitPingMenu" Content="Return to Main Menu" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="10,162,0,0" Height="25"/>
  263. <TextBlock x:Name="TextBlock_ScanStatus" HorizontalAlignment="Left" Margin="10,329,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="71" Width="218" Text="Scanning will take a while. This program attempts to Ping every address in the specified VLAN."/>
  264. </Grid>
  265.  
  266. </Grid>
  267. </Window>
  268. "@
  269.  
  270. $reader = (New-Object System.Xml.XmlNodeReader $xaml)
  271. $PrintManager = [Windows.Markup.XamlReader]::Load($reader)
  272. ####################End of WPF#################################################
  273.  
  274.  
  275. ##########Map WPF Controls into powershell variables so they can be acted upon or modified########################################
  276.  
  277. #Template for easy copy and paste
  278. #$ = $PrintManager.FindName("")
  279.  
  280. #TextBlocks
  281. $TextBlock_Status = $PrintManager.FindName("TextBlock_Status")
  282. $TextBlock_Add_Printer_Status = $PrintManager.FindName("TextBlock_Add_Printer_Status")
  283. $TextBlock_ScanStatus = $PrintManager.FindName("TextBlock_ScanStatus")
  284.  
  285.  
  286. #TextBoxes
  287. $TextBox_NewPrinterName = $PrintManager.FindName("TextBox_NewPrinterName")
  288. $TextBox_NewPrinterIP = $PrintManager.FindName("TextBox_NewPrinterIP")
  289. $TextBox_Printer_ShareName = $PrintManager.FindName("TextBox_Printer_ShareName")
  290. $TextBox_IP_Port = $PrintManager.FindName("TextBox_IP_Port")
  291. $TextBox_Search = $PrintManager.FindName("TextBox_Search")
  292. $TextBox_Location = $PrintManager.FindName("TextBox_Location")
  293. $TextBox_Description = $PrintManager.FindName("TextBox_Description")
  294.  
  295. #ListBoxes
  296. $ListBox_Printers = $PrintManager.FindName("ListBox_Printers")
  297. $ListBox_Drivers = $PrintManager.FindName("ListBox_Drivers")
  298. $ListBox_Results = $PrintManager.FindName("ListBox_Results")
  299.  
  300. #ComboBoxes
  301. $ComboBox_VLAN = $PrintManager.FindName("ComboBox_VLAN")
  302. $ComboBox_Location = $PrintManager.FindName("ComboBox_Location")
  303.  
  304. #Buttons
  305. $Button_Search = $PrintManager.FindName("Button_Search")
  306. $Button_CancelJobs = $PrintManager.FindName("Button_CancelJobs")
  307. $Button_ChangePrinterName = $PrintManager.FindName("Button_ChangePrinterName")
  308. $Button_ChagnePrinterPort = $PrintManager.FindName("Button_ChagnePrinterPort")
  309. $Button_PrintTestPage = $PrintManager.FindName("Button_PrintTestPage")
  310. $Button_ShowPrinterIP = $PrintManager.FindName("Button_ShowPrinterIP")
  311. $Button_PingPrinter = $PrintManager.FindName("Button_PingPrinter")
  312. $Button_PrintFlush = $PrintManager.FindName("Button_PrintFlush")
  313. $Button_AddNewPrinter = $PrintManager.FindName("Button_AddNewPrinter")
  314. $Button_AddPrinter = $PrintManager.FindName("Button_AddPrinter")
  315. $Button_ReturnToPrintManagment = $PrintManager.FindName("Button_ReturnToPrintManagment")
  316. $Button_RemovePrinter = $PrintManager.FindName("Button_RemovePrinter")
  317. $Button_LookForIPs = $PrintManager.FindName("Button_LookForIPs")
  318. $Button_ExitPingMenu = $PrintManager.FindName("Button_ExitPingMenu")
  319. $Button_SearchForIPs = $PrintManager.FindName("Button_SearchForIPs")
  320.  
  321.  
  322. #Grids
  323. $Grid_PrinterManagement = $PrintManager.FindName("Grid_PrinterManagement")
  324. $Grid_AddPrinter = $PrintManager.FindName("Grid_AddPrinter")
  325. $AngryIP = $PrintManager.FindName("AngryIP")
  326.  
  327.  
  328. #############################################End of WPF Control Mappings#############################################
  329.  
  330.  
  331.  
  332.  
  333.  
  334. ########################Add actions to the button clicks#############################################################
  335. $Button_ExitPingMenu.Add_Click({
  336. $Grid_PrinterManagement.Visibility = "Visible"
  337. $Grid_AddPrinter.Visibility = "Hidden"
  338. $AngryIP.Visibility = "Hidden"
  339. })
  340.  
  341. $Button_SearchForIPs.Add_Click({
  342. $Grid_PrinterManagement.Visibility = "Hidden"
  343. $Grid_AddPrinter.Visibility = "Hidden"
  344. $AngryIP.Visibility = "Visible"
  345. })
  346.  
  347. $Button_LookForIPs.Add_Click({
  348. #Create / Clear array
  349. $IPArray = @()
  350. #Clear List Box
  351. $ListBox_Results.Items.Clear()
  352. Get-LocationIP
  353. If (!($ComboBox_VLAN)) {[System.Windows.Forms.MessageBox]::Show("You must select a VLAN from the dropdown.", "No VLAN Selected"); Return}
  354. $Global:Vlan = $ComboBox_VLAN.Text
  355. $CleintIPs | ForEach-Object {$IPArray += "10.$Global:LocationIP.$Global:Vlan.$_"}
  356. $Result = GroupPing $IPArray
  357. $Result | ForEach-Object {$ListBox_Results.Items.Add($_)}
  358. $TextBlock_ScanStatus.Text = "Scan complete."
  359. })
  360.  
  361. $Button_RemovePrinter.Add_Click({
  362. If(!($ListBox_Printers.SelectedItems)){[System.Windows.Forms.MessageBox]::Show("You must select a printer from the list box on the right hand side of this application.", "No Printer Selected"); Return}
  363. $RemovablePrinter = $ListBox_Printers.SelectedItems
  364. $RemovablePrinterPort = (Get-Printer -Computername $Global:PrintServer -Name $RemovablePrinter).Portname
  365. Remove-Printer -ComputerName $Global:PrintServer -Name $RemovablePrinter
  366. Remove-PrinterPort -Computername $Global:PrintServer -Name $RemovablePrinterPort
  367. (Get-Printer -Computername $Global:PrintServer).Name | ForEach-Object {$ListBox_Printers.Items.Add($_)}
  368. $TextBlock_Status.Text = "Printer removal process complete."
  369. })
  370.  
  371. $Button_AddNewPrinter.Add_Click({
  372. $Grid_PrinterManagement.Visibility = "Hidden"
  373. $AngryIP.Visibility = "Hidden"
  374. $Grid_AddPrinter.Visibility = "Visible"
  375. (Get-PrinterDriver -Computername $Global:PrintServer).Name | Sort-Object -InputObject $_ -ascending ForEach-Object {$ListBox_Drivers.Items.Add($_)}
  376. })
  377.  
  378. $Button_PingPrinter.Add_Click({
  379.  
  380. $PrinterIP = (Get-Printer -Computername $Global:PrintServer -Name $ListBox_Printers.SelectedItems).Portname
  381. If((Test-Connection -Cn $PrinterIP -BufferSize 16 -Count 1 -ea 0 -quiet)) {$TextBlock_Status.Text = "A device (probably the printer) responded to the ping."}
  382. Else{$TextBlock_Status.Text = "The printer did not respond to a ping request."}
  383.  
  384. })
  385.  
  386. $Button_AddPrinter.Add_Click({
  387. If(!($TextBox_Printer_ShareName)){[System.Windows.Forms.MessageBox]::Show("You must enter a printer name.", "No Printer Name Given!"); Return}
  388. If(!($TextBox_IP_Port)){[System.Windows.Forms.MessageBox]::Show("You must the IP address the printer will use.", "No IP Address Given!"); Return}
  389. If(!($ListBox_Drivers.SelectedItems)){[System.Windows.Forms.MessageBox]::Show("You must select a printer driver from the list box on the right hand side of this application.", "No Printer Driver Selected!"); Return}
  390. If(!($TextBox_Location )){[System.Windows.Forms.MessageBox]::Show("You must enter a printer location.", "No Printer Name Location!"); Return}
  391. If(!($TextBox_Description)){[System.Windows.Forms.MessageBox]::Show("You must enter a printer description.", "No Printer Description Given!"); Return}
  392. $Location = $TextBox_Location.Text
  393. $Descripton = $TextBox_Description.Text
  394. $AddPrinterIP = $TextBox_IP_Port.Text
  395. $AddPrinterName = $TextBox_Printer_ShareName.Text
  396.  
  397. If (((Get-Printer -Computername $Global:PrintServer).Portname) -Like "*$AddPrinterIP*") {[System.Windows.Forms.MessageBox]::Show("A port has been found that already uses that port. You need to remove the old port first.", "Duplicate Port Detected!"); Return}
  398.  
  399. Add-PrinterPort -Computername $Global:PrintServer -Name $AddPrinterIP -PrinterHostAddress $AddPrinterIP
  400. Add-Printer -ComputerName $Global:PrintServer -Name $AddPrinterName -DriverName $ListBox_Drivers.SelectedItems -PortName $TextBox_IP_Port.Text -ShareName $AddPrinterName -Location $Location -Comment $Descripton
  401.  
  402. $TextBlock_Add_Printer_Status.Text = "Printer creation process complete."
  403. })
  404.  
  405. $Button_ReturnToPrintManagment.Add_Click({
  406. $Grid_PrinterManagement.Visibility = "Visible"
  407. $Grid_AddPrinter.Visibility = "Hidden"
  408. (Get-Printer -Computername $Global:PrintServer).Name | ForEach-Object {$ListBox_Printers.Items.Add($_)}
  409. })
  410.  
  411.  
  412. $Button_ShowPrinterIP.Add_Click({
  413. If(!($ListBox_Printers.SelectedItems)){[System.Windows.Forms.MessageBox]::Show("You must select a printer from the list box on the right hand side of this application.", "No Printer Selected"); Return}
  414. $PrinterIP = (Get-Printer -Computername $Global:PrintServer -Name $ListBox_Printers.SelectedItems).Portname
  415. $TextBlock_Status.Text = "The printer's registered IP address is $PrinterIP"
  416. })
  417.  
  418. $Button_Search.Add_Click({
  419.  
  420. $UserInput = $TextBox_Search.Text
  421. $ListBox_Printers.Items.Clear()
  422. (Get-Printer -Computername $Global:PrintServer | Where-Object {$_.Name -Like "*$UserInput*"}).Name | ForEach-Object {$ListBox_Printers.Items.Add($_)}
  423. })
  424.  
  425. $Button_CancelJobs.Add_Click({
  426. If(!($ListBox_Printers.SelectedItems)){[System.Windows.Forms.MessageBox]::Show("You must select a printer from the list box on the right hand side of this application.", "No Printer Selected"); Return}
  427. $SelectedPrinter = $ListBox_Printers.SelectedItems
  428. Get-Printer -ComputerName $Global:PrintServer | Where-Object {$_.Name -Like "*$SelectedPrinter*"} | Get-PrintJob | Remove-PrintJob
  429. #[System.Windows.Forms.MessageBox]::Show("The print que has been cleared, but the print server is notorious for failing to do this properly if a job is malfunctioning, so double check that it worked.", "Sucess... Probably?"); Return
  430. $TextBlock_Status.Text = "Print que cleared, corrupted jobs may still be pending."
  431. })
  432.  
  433. $Button_PrintTestPage.Add_Click({
  434. If(!($ListBox_Printers.SelectedItems)){[System.Windows.Forms.MessageBox]::Show("You must select a printer from the list box on the right hand side of this application.", "No Printer Selected"); Return}
  435. $Printer = $ListBox_Printers.SelectedItems
  436. "Warning! This is a test message. If your printer did not print this page, please call information services immediately. If you did receive this message, please disregard it and dispose of this paper. In doing so, please remember recycle, or don’t, but if you don’t, the trees will hate you." | Out-Printer -Name "\\SADMWPS01\$Printer"
  437. $TextBlock_Status.Text = "Test page sent to printer."
  438. })
  439.  
  440. $Button_ChangePrinterName.Add_Click({
  441. If(!($ListBox_Printers.SelectedItems)){[System.Windows.Forms.MessageBox]::Show("You must select a printer from the list box on the right hand side of this application.", "No Printer Selected"); Return}
  442. $NewPrinterName = $TextBox_NewPrinterName.Text
  443. Set-Printer -ComputerName $Global:PrintServer -Name $ListBox_Printers.SelectedItems -ShareName $NewPrinterName
  444. Get-Printer -ComputerName $Global:PrintServer -Name $ListBox_Printers.SelectedItems | Rename-Printer -NewName $NewPrinterName
  445. $TextBlock_Status.Text = "Printer name / share name change complete."
  446. })
  447.  
  448. $Button_ChagnePrinterPort.Add_Click({
  449. If(!($ListBox_Printers.SelectedItems)){[System.Windows.Forms.MessageBox]::Show("You must select a printer from the list box on the right hand side of this application.", "No Printer Selected"); Return}
  450. If(!($TextBox_NewPrinterIP.Text)){[System.Windows.Forms.MessageBox]::Show("You must enter a valid ip address.", "No IP Entered"); Return}
  451. $OldPort = (Get-Printer -Computername $Global:PrintServer -Name $ListBox_Printers.SelectedItems).Portname
  452. $NewIP = $TextBox_NewPrinterIP.Text
  453. Add-PrinterPort -Computername $Global:PrintServer -Name "$NewIP" -PrinterHostAddress $NewIP
  454. Set-Printer -Computername $Global:PrintServer -Name PADMAD121CNI4245 -PortName "$NewIP"
  455. Remove-PrinterPort -Computername $Global:PrintServer -Name $OldPort
  456. $TextBlock_Status.Text = "Port / IP change complete."
  457. })
  458.  
  459. #Populate the listbox on the right of the GUI with a list of printers.
  460. (Get-Printer -Computername $Global:PrintServer).Name | ForEach-Object {$ListBox_Printers.Items.Add($_)}
  461. $TextBlock_Status.Text = "No jobs to process yet."
  462.  
  463. #Make sure the GUI's Grid's don't overlap and show at the same time.
  464. $Grid_AddPrinter.Visibility = "Hidden"
  465. $AngryIP.Visibility = "Hidden"
  466. $Printmanager.ShowDialog()
  467. #[System.Windows.Forms.MessageBox]::Show("Message", "Title"); Return
  468. #[System.Windows.Forms.MessageBox]::Show("", ""); Return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement