Results 1 to 6 of 6
  1. #1
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts

    Another Perplexing PS Problem

    Hey Y'all,

    Another puzzling PS problem
    When I run code to determine the monitors attached to my computer (2) It only shows one monitor the 1st time the program is run but then shows the second monitor when it is run again?

    I thought this might be due to variables remaining in memory and picking up a monitor on each pass so I used the Remove-Variable cmdlet to get rid of the variable in question w/o change in the results. Any Ideas?

    Code:
    # First run after starting PS
    
    PS> G:\BEKDocs\Scripts\CMsPCInfo\GetMonitorInfo.ps1
    Monitor Information:
    
    Monitor Name         Serial Number   Ascii     
    ------------         -------------   -----     
                                                   
    VX2370 SERIES        TA9132205040    -         
    
    
    MonTable:
    
    
    # Immediate Second Run 
    PS> G:\BEKDocs\Scripts\CMsPCInfo\GetMonitorInfo.ps1
    Monitor Information:
    
    Monitor Name         Serial Number   Ascii     
    ------------         -------------   -----     
    E2340                SerialNumber    -         
    VX2370 SERIES        TA9132205040    -         
    
    
    MonTable:
    
    PS>
    Here's the code that I ran:
    Code:
    #Monitor Info 
      "Monitor Information:" 
      #Turn off Error Messages 
      $ErrorActionPreference_Backup = $ErrorActionPreference 
      $ErrorActionPreference = "SilentlyContinue" 
     
      $keytype=[Microsoft.Win32.RegistryHive]::LocalMachine
             
      if($reg=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($keytype,"")){ 
         #Create Table To Hold Info 
         $montable = New-Object system.Data.DataTable "Monitor Info" 
         #Create Columns for Table 
         $moncol1 = New-Object system.Data.DataColumn Name,([string]) 
         $moncol2 = New-Object system.Data.DataColumn Serial,([string]) 
         $moncol3 = New-Object system.Data.DataColumn Ascii,([string]) 
         #Add Columns to Table 
         $montable.columns.add($moncol1) 
         $montable.columns.add($moncol2) 
         $montable.columns.add($moncol3) 
    
         $fmt = @{Expression={$_.Name};Label="Monitor Name";width=20},
                @{Expression={$_.Serial};Label="Serial Number";width=15},
                @{Expression={$_.Ascii};Label="Ascii";width=10}
     
         $regKey= $reg.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum\DISPLAY" ) 
         $HID = $regkey.GetSubKeyNames() 
    
         foreach($HID_KEY_NAME in $HID){ 
           $regKey = `
              $reg.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum\\DISPLAY\\$HID_KEY_NAME")  
           $DID = $regkey.GetSubKeyNames() 
                        
           foreach($DID_KEY_NAME in $DID){ 
             $regKey= $reg.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum\\DISPLAY\\$HID_KEY_NAME\\$DID_KEY_NAME\\Device Parameters" ) 
             $EDID = $regKey.GetValue("EDID") 
                          
             foreach($int in $EDID){ 
               $EDID_String = $EDID_String+([char]$int) 
             } 
                         
             #Create new row in table 
             $monrow=$montable.NewRow() 
                         
             #MonitorName 
             $checkstring = `
               [char]0x00 + [char]0x00 + [char]0x00 + [char]0xFC + [char]0x00            
             $matchfound = $EDID_String -match "$checkstring([\w ]+)" 
             if($matchfound){
               $monrow.Name = [string]$matches[1]} 
             else {$monrow.Name = '-'} 
                              
             #Serial Number 
             $checkstring = `
                [char]0x00 + [char]0x00 + [char]0x00 + [char]0xFF + [char]0x00            
             $matchfound =  $EDID_String -match "$checkstring(\S+)" 
             if($matchfound){
               $monrow.Serial = [string]$matches[1]} 
             else {$monrow.Serial = '-'} 
                                                  
             #AsciiString 
             $checkstring = `
                [char]0x00 + [char]0x00 + [char]0x00 + [char]0xFE + [char]0x00            
             $matchfound = $EDID_String -match "$checkstring([\w ]+)" 
             if($matchfound){
               $monrow.Ascii = [string]$matches[1]} 
             else {$monrow.Ascii = '-'}          
                                   
             $EDID_String = '' 
                          
             $montable.Rows.Add($monrow) 
    
           }   # End - foreach($DID_KEY_NAME in $DID)
                        
         } # End - foreach($HID_KEY_NAME in $HID)
                    
         $montable | Select-object  -unique Serial,Name,Ascii | 
                     Where-Object {$_.Serial -ne "-"} | Format-Table $fmt
                     
      }   # End If
                
      else {  
             Write-Host "Access Denied - Check Permissions" 
      } 
    
     #Commands for Testing
    Remove-Variable montable -force
    "MonTable:"
    $montable
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  2. #2
    WS Lounge VIP access-mdb's Avatar
    Join Date
    Dec 2009
    Location
    Oxfordshire, UK
    Posts
    1,729
    Thanks
    148
    Thanked 156 Times in 149 Posts
    I hate to say this RG, but I ran your code and it picked up both my monitors. Unfortunately I don't know why your code didn't, but would assume that it's something on your set-up causing the problem. If you can suggest any parameters that might be causing your problem I can have a look at mine. I wonder how many people have a multi screen set-up?

    Cheers

    Powershell Noob

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Access,

    Now I'm more perplexed than I was before!

    Here's a shot of my registry entries:
    DisplayRegistry.JPG
    Note: The monitor that always shows up is the one with 2 entries?

    Thanks for testing.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  4. #4
    WS Lounge VIP access-mdb's Avatar
    Join Date
    Dec 2009
    Location
    Oxfordshire, UK
    Posts
    1,729
    Thanks
    148
    Thanked 156 Times in 149 Posts
    Here's my bit of the registry - not sure what it proves. No, I haven't a clue what it proves!
    display.PNG

  5. #5
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Access,

    Just out of curiosity how are your monitors connected? I have one DVI (that is the one that always shows up) and one HDMI (the one that only shows up after the first run).
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  6. #6
    WS Lounge VIP access-mdb's Avatar
    Join Date
    Dec 2009
    Location
    Oxfordshire, UK
    Posts
    1,729
    Thanks
    148
    Thanked 156 Times in 149 Posts
    DVI and VGA. My PC is a few years old and only came with a card with those two sockets.

    I had a thread a while back because I confused myself by connecting to the onboard VGA and card VGA sockets. The good people of the forum put me right on that one (including you if memory serves!).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •