Return computer names from a "foreach" statement

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Daniel Krebs Daniel Krebs 1 year, 3 months ago.

  • Author
    Posts
  • #30138
    Profile photo of Rocky Cabral
    Rocky Cabral
    Participant

    I'm trying to retrieve all of our Dell monitor serial numbers, I found a script that accomplishes that for a single computer name but needed to tweak it to accept a list of computers. I got the output to display but doesn't show the computers that are associated with their attached monitors.
    I'm sure this is a simple tweak of the script to provide the computer name and the monitors attached to that computer. But, just can't get it.

    Here is the script:

    $PCs = gc d:\test.txt

    $PCs | foreach {

    $ActiveMonitors = Get-WmiObject -Namespace "ROOT\WMI" -Query "SELECT * FROM WmiMonitorID WHERE Active='True'"
    $monitorInfo = @()

    foreach ($monitor in $ActiveMonitors) {
    $mon = @{}
    $manufacturer = $null
    $product = $null
    $serial = $null
    $name = $null
    $week = $null
    $year = $null

    # These are returned as ascii values, '00' padded.
    # We don't want the padding because we can't .Trim() it.
    foreach($ch in $monitor.ManufacturerName) {
    if($ch -ne '00') {
    $manufacturer += [char]$ch
    }
    }
    foreach($ch in $monitor.ProductCodeID) {
    if($ch -ne '00') {
    $product += [char]$ch
    }
    }
    foreach($ch in $monitor.SerialNumberID) {
    if($ch -ne '00') {
    $serial += [char]$ch
    }
    }
    foreach($ch in $monitor.UserFriendlyName) {
    if($ch -ne '00') {
    $name += [char]$ch
    }
    }

    $mon.Add("Manufacturer",$manufacturer)
    $mon.Add("ProductCode",$product)
    $mon.Add("SerialNumber",$serial)

    # These few are normal strings
    #Adding "PCs" variable wasn't the right answer but thought I try.
    $mon.Add("Name",$PCs)
    $mon.Add("Week",$monitor.WeekOfManufacture)
    $mon.Add("Year",$monitor.YearOfManufacture)

    $monitorInfo += $mon
    }
    $monitorInfo
    }

    Thanks for the help!

  • #30139
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Try below updated script:

    $PCs = gc d:\test.txt
    
    $PCs | foreach {
    
        $ComputerName = $_
        $ActiveMonitors = Get-WmiObject -Namespace "ROOT\WMI" -Query "SELECT * FROM WmiMonitorID WHERE Active='True'" -ComputerName $ComputerName
        
        $monitorInfo = @{
            ComputerName = $ComputerName
            Monitors = @()
        }
    
        foreach ($monitor in $ActiveMonitors) {
            $mon = @{ }
            $manufacturer = $null
            $product = $null
            $serial = $null
            $name = $null
            $week = $null
            $year = $null
    
            # These are returned as ascii values, '00' padded.
            # We don't want the padding because we can't .Trim() it.
            foreach($ch in $monitor.ManufacturerName) {
                if($ch -ne '00') {
                $manufacturer += [char]$ch
                }
            }
            foreach($ch in $monitor.ProductCodeID) {
                if($ch -ne '00') {
                $product += [char]$ch
                }
            }
            foreach($ch in $monitor.SerialNumberID) {
                if($ch -ne '00') {
                $serial += [char]$ch
                }
            }
            foreach($ch in $monitor.UserFriendlyName) {
                if($ch -ne '00') {
                $name += [char]$ch
                }
            }
    
            $mon.Add("Manufacturer",$manufacturer)
            $mon.Add("ProductCode",$product)
            $mon.Add("SerialNumber",$serial)
    
            # These few are normal strings
            #Adding "PCs" variable wasn't the right answer but thought I try.
            $mon.Add("Name",$PCs)
            $mon.Add("Week",$monitor.WeekOfManufacture)
            $mon.Add("Year",$monitor.YearOfManufacture)
    
            $monitorInfo.Monitors += $mon
        }
        $monitorInfo
    }
    
  • #30140
    Profile photo of Rocky Cabral
    Rocky Cabral
    Participant

    Thanks Daniel...that worked!

    I did make another change on this line:
    $mon.Add("Name",$PCs)
    to this....
    $mon.Add("Name",$_)

  • #30154
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Cool. Sorry I missed that line. You probably don't need it because the monitorInfo hash table already returns the computer name.

You must be logged in to reply to this topic.