none
Using Add-Member and Export-CSV with a foreach statement will not update a CSV file properly RRS feed

  • Question

  • Hi all,

    I'm working on a part of a script that collects the Monitors and their respective Serial Numbers attached to a computer and export it to a CSV file. Since I won't know the number of monitors attached to every system, I am using a foreach statement to collect all the monitors.

    The problem is the CSV file won't update correctly. If I run the script on a computer with one monitor, the CSV file will show "Monitor 1" and "Monitor 1 Serial Number". If I run the script on a computer with two monitors and have it output to the same CSV file, it will only show "Monitor 1" and "Monitor 1 Serial Number", however I am expecting it to add "Monitor 2" and "Monitor 2 Serial Number". Can anyone offer some suggestions? Here's my script so far:

    # Get monitor info
    $MonitorNumber = 0
    $MonitorCounter1 = -3
    $MonitorCounter2 = -2
    $MonitorCounter3 = -1
    $Monitors = Get-WmiObject -Namespace "root\WMI" -Class "WMIMonitorID"
    
    function GetMonitorInfo {
      foreach ($Monitor in $Monitors) {
        ([System.Text.Encoding]::ASCII.GetString($Monitor.ManufacturerName)).Replace("$([char]0x0000)","")
        ([System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName)).Replace("$([char]0x0000)","")
        ([System.Text.Encoding]::ASCII.GetString($Monitor.SerialNumberID)).Replace("$([char]0x0000)","")
    
      }
    }
    
    # CSV properties
    $infoObject = New-Object PSObject
    
    foreach ($Monitor in $Monitors) {
      $MonitorNumber++
      $MonitorCounter1 = $MonitorCounter1 + 3
      $MonitorCounter2 = $MonitorCounter2 + 3
      $MonitorCounter3 = $MonitorCounter3 + 3
      $Monitor = GetMonitorInfo | Select-Object -Index $MonitorCounter1,$MonitorCounter2
      $MonitorSN = GetMonitorInfo | Select-Object -Index $MonitorCounter3
      $Monitor = $Monitor -join ' '
      Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "Monitor $MonitorNumber" -Value $Monitor -Force
      Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "Monitor $MonitorNumber Serial Number" -Value $MonitorSN -Force
    }
    
    $infoObject
    $infoColl += $infoObject
    
    # Export info to CSV file
    try {
      $infoColl | Export-Csv -Path "$pwd\Monitors.csv" -NoTypeInformation -Append -Force
      Write-Host -ForegroundColor Green "Inventory was successfully updated!"
      exit 0
    }
    catch {
    }
    throw "Unable to export to the CSV file. Please check the permissions on the file."

    Thank you!!!

    Friday, July 19, 2019 11:19 PM

All replies

  • Hi,

    Thanks for your question.

    Your script works fine for me.

    $Monitors = Get-WmiObject -Namespace "root\WMI" -Class "WMIMonitorID"
    $infoObject = New-Object PSObject
    $MonitorNumber = 0
    foreach($Monitor in  $Monitors)
    { 
      $MonitorNumber++
      Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "Monitor $MonitorNumber" -Value $(([System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName)).Replace("$([char]0x0000)","")) -Force
      Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "Monitor $MonitorNumber Serial Number" -Value $(([System.Text.Encoding]::ASCII.GetString($Monitor.SerialNumberID)).Replace("$([char]0x0000)","")) -Force
    }
    try {
      $infoColl | Export-Csv -Path "$pwd\Monitors.csv" -NoTypeInformation -Append -Force
      Write-Host -ForegroundColor Green "Inventory was successfully updated!"
      exit 0
    }
    catch {
    }
    throw "Unable to export to the CSV file. Please check the permissions on the file."

    Best regards,

    Lee


    Just do it.

    Monday, July 22, 2019 6:52 AM
    Moderator
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee



    Just do it.

    Wednesday, July 31, 2019 7:11 AM
    Moderator
  • Thanks for your response. The problem is if you took the same exact CSV file, ran the script on a computer with three monitors, the third monitor would not show up since the first computer you ran it on had two monitors. This appears to be a bug/limitation of the Export-CSV cmdlet, but I am not sure and am open to solutions or other options.
    Friday, August 16, 2019 5:59 PM