Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-Type -AssemblyName System.Windows.Forms
  2. [System.Windows.Forms.Application]::EnableVisualStyles()
  3.  
  4.  
  5. # .Net methods for hiding/showing the console in the background
  6. Add-Type -Name Window -Namespace Console -MemberDefinition '
  7. [DllImport("Kernel32.dll")]
  8. public static extern IntPtr GetConsoleWindow();
  9.  
  10. [DllImport("user32.dll")]
  11. public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
  12. '
  13.  
  14. function Show-Console
  15. {
  16.     $consolePtr = [Console.Window]::GetConsoleWindow()
  17.  
  18.     # Hide = 0,
  19.     # ShowNormal = 1,
  20.     # ShowMinimized = 2,
  21.     # ShowMaximized = 3,
  22.     # Maximize = 3,
  23.     # ShowNormalNoActivate = 4,
  24.     # Show = 5,
  25.     # Minimize = 6,
  26.     # ShowMinNoActivate = 7,
  27.     # ShowNoActivate = 8,
  28.     # Restore = 9,
  29.     # ShowDefault = 10,
  30.     # ForceMinimized = 11
  31.  
  32.     [Console.Window]::ShowWindow($consolePtr, 4)
  33. }
  34.  
  35. function Hide-Console
  36. {
  37.     $consolePtr = [Console.Window]::GetConsoleWindow()
  38.     #0 hide
  39.     [Console.Window]::ShowWindow($consolePtr, 0)
  40. }
  41.  
  42.  
  43. #region begin GUI{ 
  44.  
  45. $Form                            = New-Object system.Windows.Forms.Form
  46. $Form.ClientSize                 = '400,135'
  47. $Form.text                       = "Attached Monitors"
  48. $Form.TopMost                    = $false
  49.  
  50. $TextBox1                        = New-Object system.Windows.Forms.TextBox
  51. $TextBox1.multiline              = $false
  52. $TextBox1.width                  = 160
  53. $TextBox1.height                 = 20
  54. $TextBox1.location               = New-Object System.Drawing.Point(37,46)
  55. $TextBox1.Font                   = 'Microsoft Sans Serif,10'
  56.  
  57. $Button1                         = New-Object system.Windows.Forms.Button
  58. $Button1.text                    = "Search"
  59. $Button1.width                   = 90
  60. $Button1.height                  = 30
  61. $Button1.location                = New-Object System.Drawing.Point(213,40)
  62. $Button1.Font                    = 'Microsoft Sans Serif,10'
  63.  
  64. $Label1                          = New-Object system.Windows.Forms.Label
  65. $Label1.text                     = "Computer Name"
  66. $Label1.AutoSize                 = $true
  67. $Label1.width                    = 25
  68. $Label1.height                   = 10
  69. $Label1.location                 = New-Object System.Drawing.Point(37,20)
  70. $Label1.Font                     = 'Microsoft Sans Serif,10'
  71.  
  72. $CheckBox1                       = New-Object system.Windows.Forms.CheckBox
  73. $CheckBox1.text                  = "Show in Excel"
  74. $CheckBox1.AutoSize              = $false
  75. $CheckBox1.width                 = 250
  76. $CheckBox1.height                = 20
  77. $CheckBox1.location              = New-Object System.Drawing.Point(37,86)
  78. $CheckBox1.Font                  = 'Microsoft Sans Serif,10'
  79.  
  80. $Form.controls.AddRange(@($TextBox1,$Button1,$Label1,$CheckBox1))
  81.  
  82. #region gui events {
  83.  
  84. $TextBox1.Add_keydown($press_enter)
  85.  
  86. #endregion events }
  87.  
  88. #endregion GUI }
  89.  
  90. $press_enter = [System.Windows.Forms.KeyEventHandler]{
  91. <#
  92.     calls the PerformClick method on the 'Search' button
  93.     whenever system detects the 'Enter' key being pressed
  94. #>
  95.     if ($_.KeyCode -eq 'Enter') {
  96.         $Button1.PerformClick()
  97.         #Suppress sound from unexpected use of enter on keyPress/keyUp
  98.         $_.SuppressKeyPress = $true
  99.     }
  100. }
  101.  
  102.  
  103. $Button1.Add_Click({
  104.     Get-Monitor -ComputerName $TextBox1.Text }
  105. )
  106.  
  107.  
  108.      
  109.  
  110.  
  111. function Get-Monitor {
  112.     [CmdletBinding()]
  113.       param (
  114.         [Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
  115.         [String]$ComputerName = $env:ComputerName
  116.       )
  117.  
  118.       if (Test-Connection -ComputerName $ComputerName -Count 1 -ErrorAction SilentlyContinue) {
  119.         
  120.           #List of Manufacture Codes that could be pulled from WMI and their respective full names. Used for translating later down.
  121.           $ManufacturerHash = @{ 
  122.             "AAC" =    "AcerView";
  123.             "ACR" = "Acer";
  124.             "AOC" = "AOC";
  125.             "AIC" = "AG Neovo";
  126.             "APP" = "Apple Computer";
  127.             "AST" = "AST Research";
  128.             "AUO" = "Asus";
  129.             "BNQ" = "BenQ";
  130.             "CMO" = "Acer";
  131.             "CPL" = "Compal";
  132.             "CPQ" = "Compaq";
  133.             "CPT" = "Chunghwa Pciture Tubes, Ltd.";
  134.             "CTX" = "CTX";
  135.             "DEC" = "DEC";
  136.             "DEL" = "Dell";
  137.             "DPC" = "Delta";
  138.             "DWE" = "Daewoo";
  139.             "EIZ" = "EIZO";
  140.             "ELS" = "ELSA";
  141.             "ENC" = "EIZO";
  142.             "EPI" = "Envision";
  143.             "FCM" = "Funai";
  144.             "FUJ" = "Fujitsu";
  145.             "FUS" = "Fujitsu-Siemens";
  146.             "GSM" = "LG Electronics";
  147.             "GWY" = "Gateway 2000";
  148.             "HEI" = "Hyundai";
  149.             "HIT" = "Hyundai";
  150.             "HSL" = "Hansol";
  151.             "HTC" = "Hitachi/Nissei";
  152.             "HWP" = "HP";
  153.             "IBM" = "IBM";
  154.             "ICL" = "Fujitsu ICL";
  155.             "IVM" = "Iiyama";
  156.             "KDS" = "Korea Data Systems";
  157.             "LEN" = "Lenovo";
  158.             "LGD" = "Asus";
  159.             "LPL" = "Fujitsu";
  160.             "MAX" = "Belinea"
  161.             "MEI" = "Panasonic";
  162.             "MEL" = "Mitsubishi Electronics";
  163.             "MS_" = "Panasonic";
  164.             "NAN" = "Nanao";
  165.             "NEC" = "NEC";
  166.             "NOK" = "Nokia Data";
  167.             "NVD" = "Fujitsu";
  168.             "OPT" = "Optoma";
  169.             "PHL" = "Philips";
  170.             "REL" = "Relisys";
  171.             "SAN" = "Samsung";
  172.             "SAM" = "Samsung";
  173.             "SBI" = "Smarttech";
  174.             "SGI" = "SGI";
  175.             "SNY" = "Sony";
  176.             "SRC" = "Shamrock";
  177.             "SUN" = "Sun Microsystems";
  178.             "SEC" = "Hewlett-Packard";
  179.             "TAT" = "Tatung";
  180.             "TOS" = "Toshiba";
  181.             "TSB" = "Toshiba";
  182.             "VSC" = "ViewSonic";
  183.             "ZCM" = "Zenith";
  184.             "UNK" = "Unknown";
  185.             "_YV" = "Fujitsu";
  186.               }
  187.           
  188.       
  189.           #Takes each computer specified and runs the following code:
  190.           ForEach ($Computer in $ComputerName) {
  191.       
  192.             #Grabs the Monitor objects from WMI
  193.             $Monitors = Get-WmiObject -Namespace "root\WMI" -Class "WMIMonitorID" -ComputerName $Computer -ErrorAction SilentlyContinue
  194.         
  195.             #Creates an empty array to hold the data
  196.             $Monitor_Array = @()
  197.         
  198.         
  199.             #Takes each monitor object found and runs the following code:
  200.             ForEach ($Monitor in $Monitors) {
  201.           
  202.               #Grabs respective data and converts it from ASCII encoding and removes any trailing ASCII null values
  203.               try {
  204.                   If ($null -ne [System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName)) {
  205.                     $Mon_Model = ([System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName)).Replace("$([char]0x0000)","")
  206.                   } else {
  207.                     $Mon_Model = $null
  208.                 }
  209.               } catch {} #suppressing an error in the console
  210.  
  211.               $Mon_Serial_Number = ([System.Text.Encoding]::ASCII.GetString($Monitor.SerialNumberID)).Replace("$([char]0x0000)","")
  212.               $Mon_Attached_Computer = ($Monitor.PSComputerName).Replace("$([char]0x0000)","")
  213.               $Mon_Manufacturer = ([System.Text.Encoding]::ASCII.GetString($Monitor.ManufacturerName)).Replace("$([char]0x0000)","")
  214.           
  215.               #Filters out "non monitors". Place any of your own filters here. These two are all-in-one computers with built in displays. I don't need the info from these.
  216.               If ($Mon_Model -like "*800 AIO*" -or $Mon_Model -like "*8300 AiO*") {Break}
  217.           
  218.               #Sets a friendly name based on the hash table above. If no entry found sets it to the original 3 character code
  219.               $Mon_Manufacturer_Friendly = $ManufacturerHash.$Mon_Manufacturer
  220.               If ($Mon_Manufacturer_Friendly -eq $null) {
  221.                 $Mon_Manufacturer_Friendly = $Mon_Manufacturer
  222.               }
  223.           
  224.               #Creates a custom monitor object and fills it with 4 NoteProperty members and the respective data
  225.               $Monitor_Obj = [PSCustomObject]@{
  226.                 Manufacturer     = $Mon_Manufacturer_Friendly
  227.                 Model            = $Mon_Model
  228.                 SerialNumber     = $Mon_Serial_Number
  229.                 AttachedComputer = $Mon_Attached_Computer
  230.               }
  231.           
  232.               #Appends the object to the array
  233.               $Monitor_Array += $Monitor_Obj
  234.     
  235.             } #End ForEach Monitor
  236.       
  237.            #Outputs the Array
  238.            if ($CheckBox1.Checked -eq $false) {
  239.                $Monitor_Array | Out-GridView
  240.            } else {
  241.                $Monitor_Array | Export-Csv $env:TEMP\mons.csv -NoTypeInformation
  242.                Invoke-Item $env:TEMP\mons.csv
  243.            }
  244.         
  245.     } #End ForEach Computer
  246.     } else {
  247.         [System.Windows.MessageBox]::Show("Unable to ping $ComputerName", "Timed Out", "OK", "Error")
  248.     }
  249. }
  250.  
  251. $Form.Add_Shown({
  252.     Hide-Console
  253. })
  254. [void]$Form.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement