Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add-Type -AssemblyName System.Windows.Forms
- [System.Windows.Forms.Application]::EnableVisualStyles()
- # .Net methods for hiding/showing the console in the background
- Add-Type -Name Window -Namespace Console -MemberDefinition '
- [DllImport("Kernel32.dll")]
- public static extern IntPtr GetConsoleWindow();
- [DllImport("user32.dll")]
- public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
- '
- function Show-Console
- {
- $consolePtr = [Console.Window]::GetConsoleWindow()
- # Hide = 0,
- # ShowNormal = 1,
- # ShowMinimized = 2,
- # ShowMaximized = 3,
- # Maximize = 3,
- # ShowNormalNoActivate = 4,
- # Show = 5,
- # Minimize = 6,
- # ShowMinNoActivate = 7,
- # ShowNoActivate = 8,
- # Restore = 9,
- # ShowDefault = 10,
- # ForceMinimized = 11
- [Console.Window]::ShowWindow($consolePtr, 4)
- }
- function Hide-Console
- {
- $consolePtr = [Console.Window]::GetConsoleWindow()
- #0 hide
- [Console.Window]::ShowWindow($consolePtr, 0)
- }
- #region begin GUI{
- $Form = New-Object system.Windows.Forms.Form
- $Form.ClientSize = '400,135'
- $Form.text = "Attached Monitors"
- $Form.TopMost = $false
- $TextBox1 = New-Object system.Windows.Forms.TextBox
- $TextBox1.multiline = $false
- $TextBox1.width = 160
- $TextBox1.height = 20
- $TextBox1.location = New-Object System.Drawing.Point(37,46)
- $TextBox1.Font = 'Microsoft Sans Serif,10'
- $Button1 = New-Object system.Windows.Forms.Button
- $Button1.text = "Search"
- $Button1.width = 90
- $Button1.height = 30
- $Button1.location = New-Object System.Drawing.Point(213,40)
- $Button1.Font = 'Microsoft Sans Serif,10'
- $Label1 = New-Object system.Windows.Forms.Label
- $Label1.text = "Computer Name"
- $Label1.AutoSize = $true
- $Label1.width = 25
- $Label1.height = 10
- $Label1.location = New-Object System.Drawing.Point(37,20)
- $Label1.Font = 'Microsoft Sans Serif,10'
- $CheckBox1 = New-Object system.Windows.Forms.CheckBox
- $CheckBox1.text = "Show in Excel"
- $CheckBox1.AutoSize = $false
- $CheckBox1.width = 250
- $CheckBox1.height = 20
- $CheckBox1.location = New-Object System.Drawing.Point(37,86)
- $CheckBox1.Font = 'Microsoft Sans Serif,10'
- $Form.controls.AddRange(@($TextBox1,$Button1,$Label1,$CheckBox1))
- #region gui events {
- $TextBox1.Add_keydown($press_enter)
- #endregion events }
- #endregion GUI }
- $press_enter = [System.Windows.Forms.KeyEventHandler]{
- <#
- calls the PerformClick method on the 'Search' button
- whenever system detects the 'Enter' key being pressed
- #>
- if ($_.KeyCode -eq 'Enter') {
- $Button1.PerformClick()
- #Suppress sound from unexpected use of enter on keyPress/keyUp
- $_.SuppressKeyPress = $true
- }
- }
- $Button1.Add_Click({
- Get-Monitor -ComputerName $TextBox1.Text }
- )
- function Get-Monitor {
- [CmdletBinding()]
- param (
- [Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
- [String]$ComputerName = $env:ComputerName
- )
- if (Test-Connection -ComputerName $ComputerName -Count 1 -ErrorAction SilentlyContinue) {
- #List of Manufacture Codes that could be pulled from WMI and their respective full names. Used for translating later down.
- $ManufacturerHash = @{
- "AAC" = "AcerView";
- "ACR" = "Acer";
- "AOC" = "AOC";
- "AIC" = "AG Neovo";
- "APP" = "Apple Computer";
- "AST" = "AST Research";
- "AUO" = "Asus";
- "BNQ" = "BenQ";
- "CMO" = "Acer";
- "CPL" = "Compal";
- "CPQ" = "Compaq";
- "CPT" = "Chunghwa Pciture Tubes, Ltd.";
- "CTX" = "CTX";
- "DEC" = "DEC";
- "DEL" = "Dell";
- "DPC" = "Delta";
- "DWE" = "Daewoo";
- "EIZ" = "EIZO";
- "ELS" = "ELSA";
- "ENC" = "EIZO";
- "EPI" = "Envision";
- "FCM" = "Funai";
- "FUJ" = "Fujitsu";
- "FUS" = "Fujitsu-Siemens";
- "GSM" = "LG Electronics";
- "GWY" = "Gateway 2000";
- "HEI" = "Hyundai";
- "HIT" = "Hyundai";
- "HSL" = "Hansol";
- "HTC" = "Hitachi/Nissei";
- "HWP" = "HP";
- "IBM" = "IBM";
- "ICL" = "Fujitsu ICL";
- "IVM" = "Iiyama";
- "KDS" = "Korea Data Systems";
- "LEN" = "Lenovo";
- "LGD" = "Asus";
- "LPL" = "Fujitsu";
- "MAX" = "Belinea";
- "MEI" = "Panasonic";
- "MEL" = "Mitsubishi Electronics";
- "MS_" = "Panasonic";
- "NAN" = "Nanao";
- "NEC" = "NEC";
- "NOK" = "Nokia Data";
- "NVD" = "Fujitsu";
- "OPT" = "Optoma";
- "PHL" = "Philips";
- "REL" = "Relisys";
- "SAN" = "Samsung";
- "SAM" = "Samsung";
- "SBI" = "Smarttech";
- "SGI" = "SGI";
- "SNY" = "Sony";
- "SRC" = "Shamrock";
- "SUN" = "Sun Microsystems";
- "SEC" = "Hewlett-Packard";
- "TAT" = "Tatung";
- "TOS" = "Toshiba";
- "TSB" = "Toshiba";
- "VSC" = "ViewSonic";
- "ZCM" = "Zenith";
- "UNK" = "Unknown";
- "_YV" = "Fujitsu";
- }
- #Takes each computer specified and runs the following code:
- ForEach ($Computer in $ComputerName) {
- #Grabs the Monitor objects from WMI
- $Monitors = Get-WmiObject -Namespace "root\WMI" -Class "WMIMonitorID" -ComputerName $Computer -ErrorAction SilentlyContinue
- #Creates an empty array to hold the data
- $Monitor_Array = @()
- #Takes each monitor object found and runs the following code:
- ForEach ($Monitor in $Monitors) {
- #Grabs respective data and converts it from ASCII encoding and removes any trailing ASCII null values
- try {
- If ($null -ne [System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName)) {
- $Mon_Model = ([System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName)).Replace("$([char]0x0000)","")
- } else {
- $Mon_Model = $null
- }
- } catch {} #suppressing an error in the console
- $Mon_Serial_Number = ([System.Text.Encoding]::ASCII.GetString($Monitor.SerialNumberID)).Replace("$([char]0x0000)","")
- $Mon_Attached_Computer = ($Monitor.PSComputerName).Replace("$([char]0x0000)","")
- $Mon_Manufacturer = ([System.Text.Encoding]::ASCII.GetString($Monitor.ManufacturerName)).Replace("$([char]0x0000)","")
- #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.
- If ($Mon_Model -like "*800 AIO*" -or $Mon_Model -like "*8300 AiO*") {Break}
- #Sets a friendly name based on the hash table above. If no entry found sets it to the original 3 character code
- $Mon_Manufacturer_Friendly = $ManufacturerHash.$Mon_Manufacturer
- If ($Mon_Manufacturer_Friendly -eq $null) {
- $Mon_Manufacturer_Friendly = $Mon_Manufacturer
- }
- #Creates a custom monitor object and fills it with 4 NoteProperty members and the respective data
- $Monitor_Obj = [PSCustomObject]@{
- Manufacturer = $Mon_Manufacturer_Friendly
- Model = $Mon_Model
- SerialNumber = $Mon_Serial_Number
- AttachedComputer = $Mon_Attached_Computer
- }
- #Appends the object to the array
- $Monitor_Array += $Monitor_Obj
- } #End ForEach Monitor
- #Outputs the Array
- if ($CheckBox1.Checked -eq $false) {
- $Monitor_Array | Out-GridView
- } else {
- $Monitor_Array | Export-Csv $env:TEMP\mons.csv -NoTypeInformation
- Invoke-Item $env:TEMP\mons.csv
- }
- } #End ForEach Computer
- } else {
- [System.Windows.MessageBox]::Show("Unable to ping $ComputerName", "Timed Out", "OK", "Error")
- }
- }
- $Form.Add_Shown({
- Hide-Console
- })
- [void]$Form.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement