question

DHoss-6479 avatar image
0 Votes"
DHoss-6479 asked DHoss-6479 answered

Need some help with this scripts

I have compiled this scripts with help of Bhupinder and Thom McKiernan script from internet and all working fine except I do not get the 2nd monitor info if there is 2nd monitor. I would really appreciate if someone can help me with this. I am not good at the pow3rshell but learning.... Thanks in Advance.

Blockquote

$ArrComputers = read-host -Prompt "Enter Computer Name"

function Decode {
If ($args[0] -is [System.Array]) {
[System.Text.Encoding]::ASCII.GetString($args[0])
}
Else {
"Not Found"
}
}

Clear-Host
foreach ($Computer in $ArrComputers)
{

$wmi_os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $Computer | select CSName,Caption,Version,OSArchitecture,LastBootUptime
switch($wmi_os.Version){
'10.0.10240'{$wmi_build="1507"}
'10.0.10586'{$wmi_build="1511"}
'10.0.14393'{$wmi_build="1607"}
'10.0.15063'{$wmi_build="1703"}
'10.0.16299'{$wmi_build="1709"}
'10.0.17134'{$wmi_build="1803"}
'10.0.17686'{$wmi_build="1809"}
'10.0.18362'{$wmi_build="1903"}
'10.0.18363'{$wmi_build="1909"}
'10.0.19042'{$wmi_build="2009"}
}
$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
$computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
$computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
$DNName = Get-ADComputer -Filter "Name -like '$Computer'" | select -ExpandProperty DistinguishedName

$computerCPU = get-wmiobject Win32_Processor -Computer $Computer
$computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
$Monitors = Get-WmiObject WmiMonitorID -Namespace root\wmi -ComputerName $Computer
#$Monitors = Get-WmiObject WmiMonitorID -RELPATH root\wmi -ComputerName $Computer
ForEach ( $Monitor in $Monitors) {
$Manufacturer = Decode $Monitor.ManufacturerName -notmatch 0
$Name = Decode $Monitor.UserFriendlyName -notmatch 0
$Serial = Decode $Monitor.SerialNumberID -notmatch 0
$YofM = $Monitor.YearOfManufacture
}

  write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
  "-------------------------------------------------------"
  "Manufacturer    : " + $computerSystem.Manufacturer
  "Model           : " + $computerSystem.Model
  "Serial Number   : " + $computerBIOS.SerialNumber
  "CPU             : " + $computerCPU.Name
  "HDD Capacity    : "  + "{0:N0}" -f ($computerHDD.Size/1GB) + "GB"
  "HDD Space       : " + "{0:P0}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N0}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
  "RAM             : " + "{0:N0}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
  "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
  "Build Version   : " + $wmi_build
  "User logged In  : " + $computerSystem.UserName
  "OU Location     : " + $DNName
  "Last Reboot     : " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
  "Monitor Mfgr    : " + $($Manufacturer)
  "Monitor Name    : " + $($Name)
  "Monior S/N      : " + $($Serial)
  "Monitor Year    : " + $($YofM)
        
  ""
  "-------------------------------------------------------"

}

Blockquote


windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT edited

Hi,

The issue should have been fixed in the last script. Please check if the below one works. I just replaced the Get-CimInstance with Get-WmiObjcet.

 $ArrComputers = read-host -Prompt "Enter Computer Name"
        
 function Decode {
     If ($args[0] -is [System.Array]) {
         [System.Text.Encoding]::ASCII.GetString($args[0])
     }
     Else {
         "Not Found"
     }
 }
        
 Clear-Host
 foreach ($Computer in $ArrComputers)
 {  
     $wmi_os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer | select CSName,Caption,Version,OSArchitecture,LastBootUptime
     switch($wmi_os.Version){
         '10.0.10240'{$wmi_build="1507"}
         '10.0.10586'{$wmi_build="1511"}
         '10.0.14393'{$wmi_build="1607"}
         '10.0.15063'{$wmi_build="1703"}
         '10.0.16299'{$wmi_build="1709"}
         '10.0.17134'{$wmi_build="1803"}
         '10.0.17686'{$wmi_build="1809"}
         '10.0.18362'{$wmi_build="1903"}
         '10.0.18363'{$wmi_build="1909"}
         '10.0.19042'{$wmi_build="2009"}
     }
     $computerSystem = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer
     $computerBIOS = Get-WmiObject -Class Win32_BIOS -ComputerName $Computer
     $computerOS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer  
     $DNName = Get-ADComputer -Filter "Name -like '$Computer'" | select -ExpandProperty DistinguishedName
     $computerCPU = Get-WmiObject -Class Win32_Processor -ComputerName $Computer
     $computerHDD = Get-WmiObject -Class Win32_LogicalDisk -Filter drivetype=3 -ComputerName $Computer
     $Monitors = Get-WmiObject -Class WmiMonitorID -Namespace root\wmi -ComputerName $Computer
     $Manufacturer = @()
     $Name = @()
     $Serial = @()
     $YofM = @()
     foreach ( $Monitor in $Monitors) {
         $Manufacturer += Decode $Monitor.ManufacturerName -notmatch 0
         $Name += Decode $Monitor.UserFriendlyName -notmatch 0
         $Serial += Decode $Monitor.SerialNumberID -notmatch 0
         $YofM += $Monitor.YearOfManufacture
     }
                
     write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
         "-------------------------------------------------------"
         "Manufacturer    : " + $computerSystem.Manufacturer
         "Model           : " + $computerSystem.Model
         "Serial Number   : " + $computerBIOS.SerialNumber
         "CPU             : " + $computerCPU.Name
         "HDD Capacity    : "  + "{0:N0}" -f ($computerHDD.Size/1GB) + "GB"
         "HDD Space       : " + "{0:P0}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N0}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
         "RAM             : " + "{0:N0}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
         "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
         "Build Version   : " + $wmi_build
         "User logged In  : " + $computerSystem.UserName
         "OU Location     : " + $DNName
         "Last Reboot     : " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
         "Monitor Mfgr    : " + $($Manufacturer)
         "Monitor Name    : " + $($Name)
         "Monior S/N      : " + $($Serial)
         "Monitor Year    : " + $($YofM)
                
         ""
         "-------------------------------------------------------"
 }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered DHoss-6479 edited

Hi,

The variable $Manufacturer, $Name, $Serial and $YofM are overwritten. You can use the operator "+=" instead of "=".

 $ArrComputers = read-host -Prompt "Enter Computer Name"
    
 function Decode {
 If ($args[0] -is [System.Array]) {
 [System.Text.Encoding]::ASCII.GetString($args[0])
 }
 Else {
 "Not Found"
 }
 }
    
 Clear-Host
 foreach ($Computer in $ArrComputers)
 {
    
 $wmi_os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $Computer | select CSName,Caption,Version,OSArchitecture,LastBootUptime
 switch($wmi_os.Version){
 '10.0.10240'{$wmi_build="1507"}
 '10.0.10586'{$wmi_build="1511"}
 '10.0.14393'{$wmi_build="1607"}
 '10.0.15063'{$wmi_build="1703"}
 '10.0.16299'{$wmi_build="1709"}
 '10.0.17134'{$wmi_build="1803"}
 '10.0.17686'{$wmi_build="1809"}
 '10.0.18362'{$wmi_build="1903"}
 '10.0.18363'{$wmi_build="1909"}
 '10.0.19042'{$wmi_build="2009"}
 }
 $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
 $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
 $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
 $DNName = Get-ADComputer -Filter "Name -like '$Computer'" | select -ExpandProperty DistinguishedName
    
  $computerCPU = get-wmiobject Win32_Processor -Computer $Computer
  $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
  $Monitors = Get-WmiObject WmiMonitorID -Namespace  root\wmi -ComputerName $Computer
  #$Monitors = Get-WmiObject WmiMonitorID -RELPATH  root\wmi -ComputerName $Computer
      ForEach ( $Monitor in $Monitors) {
   $Manufacturer += Decode $Monitor.ManufacturerName -notmatch 0
   $Name += Decode $Monitor.UserFriendlyName -notmatch 0
   $Serial += Decode $Monitor.SerialNumberID -notmatch 0
   $YofM += $Monitor.YearOfManufacture
   }
            
      write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
      "-------------------------------------------------------"
      "Manufacturer    : " + $computerSystem.Manufacturer
      "Model           : " + $computerSystem.Model
      "Serial Number   : " + $computerBIOS.SerialNumber
      "CPU             : " + $computerCPU.Name
      "HDD Capacity    : "  + "{0:N0}" -f ($computerHDD.Size/1GB) + "GB"
      "HDD Space       : " + "{0:P0}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N0}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
      "RAM             : " + "{0:N0}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
      "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
      "Build Version   : " + $wmi_build
      "User logged In  : " + $computerSystem.UserName
      "OU Location     : " + $DNName
      "Last Reboot     : " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
      "Monitor Mfgr    : " + $($Manufacturer)
      "Monitor Name    : " + $($Name)
      "Monior S/N      : " + $($Serial)
      "Monitor Year    : " + $($YofM)
            
      ""
      "-------------------------------------------------------"
 }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Ian
Thanks for your reply. Yes now it gives me the multiple monitor info but the $YofM += $Monitor.YearOfManufacture is not returning right value. Any idea! Also one more thing would like to know if there are other way of writing this to make it faster process. I am not saying that it is not working but just exploring if there is any other faster way.

0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT edited

Hi,

Please see if this works better. This will reuse one existing session, not create a new session in every WMI query.

 $ArrComputers = read-host -Prompt "Enter Computer Name"
        
 function Decode {
     If ($args[0] -is [System.Array]) {
         [System.Text.Encoding]::ASCII.GetString($args[0])
     }
     Else {
         "Not Found"
     }
 }
        
 Clear-Host
 foreach ($Computer in $ArrComputers)
 {  
     $session = New-CimSession –ComputerName $Computer
     $wmi_os = Get-CimInstance -ClassName Win32_OperatingSystem -CimSession $session | select CSName,Caption,Version,OSArchitecture,LastBootUptime
     switch($wmi_os.Version){
         '10.0.10240'{$wmi_build="1507"}
         '10.0.10586'{$wmi_build="1511"}
         '10.0.14393'{$wmi_build="1607"}
         '10.0.15063'{$wmi_build="1703"}
         '10.0.16299'{$wmi_build="1709"}
         '10.0.17134'{$wmi_build="1803"}
         '10.0.17686'{$wmi_build="1809"}
         '10.0.18362'{$wmi_build="1903"}
         '10.0.18363'{$wmi_build="1909"}
         '10.0.19042'{$wmi_build="2009"}
     }
     $computerSystem = Get-CimInstance -ClassName Win32_ComputerSystem -CimSession $session
     $computerBIOS = Get-CimInstance -ClassName Win32_BIOS -CimSession $session
     $computerOS = Get-CimInstance -ClassName Win32_OperatingSystem -CimSession $session  
     $DNName = Get-ADComputer -Filter "Name -like '$Computer'" | select -ExpandProperty DistinguishedName
     $computerCPU = Get-CimInstance -ClassName Win32_Processor -CimSession $session
     $computerHDD = Get-CimInstance -ClassName Win32_LogicalDisk -Filter drivetype=3 -CimSession $session
     $Monitors = Get-CimInstance -ClassName WmiMonitorID -Namespace root\wmi -CimSession $session
     $Manufacturer=@()
     $Name=@()
     $Serial=@()
     $YofM=@()
     foreach ( $Monitor in $Monitors) {
         $Manufacturer += Decode $Monitor.ManufacturerName -notmatch 0
         $Name += Decode $Monitor.UserFriendlyName -notmatch 0
         $Serial += Decode $Monitor.SerialNumberID -notmatch 0
         $YofM += $Monitor.YearOfManufacture
     }
                
     write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
         "-------------------------------------------------------"
         "Manufacturer    : " + $computerSystem.Manufacturer
         "Model           : " + $computerSystem.Model
         "Serial Number   : " + $computerBIOS.SerialNumber
         "CPU             : " + $computerCPU.Name
         "HDD Capacity    : "  + "{0:N0}" -f ($computerHDD.Size/1GB) + "GB"
         "HDD Space       : " + "{0:P0}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N0}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
         "RAM             : " + "{0:N0}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
         "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
         "Build Version   : " + $wmi_build
         "User logged In  : " + $computerSystem.UserName
         "OU Location     : " + $DNName
         "Last Reboot     : " + $computerOS.LastBootUpTime
         "Monitor Mfgr    : " + $($Manufacturer)
         "Monitor Name    : " + $($Name)
         "Monior S/N      : " + $($Serial)
         "Monitor Year    : " + $($YofM)
                
         ""
         "-------------------------------------------------------"
 }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DHoss-6479 avatar image
0 Votes"
DHoss-6479 answered DHoss-6479 edited

Hi Ian
No it did not work as the winRm service is not enable in our environment so CimSession does not work on remote machine. As I said your previous modification works except the year of mfg. Also one more thing I just notice that if I ran the scripts 2nd time for a different computer, it also out the previous computer monitor info with the current one. Your answer would much be appreciated.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DHoss-6479 avatar image
0 Votes"
DHoss-6479 answered

Hi Ian
Thank you so much for all the help. Yes it is working and again I really appreciate all your effort.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DHoss-6479 avatar image
0 Votes"
DHoss-6479 answered

Hi Ian,
One more quick things if you don't mind. when I do this....

write-output "System Information for: " $computerSystem.Name

The result come like 94112-image.png
Is there a way to keep them in one line.


image.png (2.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DHoss-6479 avatar image
0 Votes"
DHoss-6479 answered

HI Ian,
Please disregard my last questions, I have figured it out and all are good. Thanks again for all the help.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.