none
Export-Csv results in columns being in reverse order RRS feed

  • Question

  • Hi,

    I have this script working mostly the way I want it but when I open the CSV that is created, the column headers are in reverse order. See below.

    Build Version Computer Name
    OFFLINE COMPUTER1
    10.0.10586 COMPUTER2
    OFFLINE COMPUTER3
    10.0.10586 COMPUTER4
    OFFLINE COMPUTER5

    My preference is to have the Computer Name column first and then the Build Version column. I have struggled with this for awhile and I can't seem to figure out why the column headers are being reversed.

    Here is what I have so far:

    $results = @()
    
    #Filename to process
    $computerList = "version.txt"
    
    #Input list of computers into array
    $computers = Get-Content -Path $computerList
    
    #Process each computer in the list
    foreach ($computer In $computers) {
    	#Remove any spaces in the computer name
    	$computer = $computer.Trim()
    	
    	#Test each computer if online and collect result
    	if (Test-Connection -ComputerName $computer -BufferSize 16 -Count 1 -ea 0 -Quiet) {
    	    #Computer is responding - output to console in Green
    	    $OS = Get-WmiObject -Computer $computer -Class Win32_OperatingSystem
                $results += New-Object PSObject -Property @{"Computer Name"=$computer;"Build Version"=$OS.Version}
    
    	} else {
    	    #Computer is responding - output to console in Red
                $results += New-Object PSObject -Property @{"Computer Name"=$computer;"Build Version"="OFFLINE"}
    	}
    }
    
    $results | Export-Csv -Path "BuildVersions.csv" -NoTypeInformation

    This may produce some errors where the RPC server is unavailable or access is denied for some other reason. I haven't gotten that far yet.

    Thank you!

    Rob


    • Edited by robwm1 Friday, April 13, 2018 6:58 PM
    Friday, April 13, 2018 6:57 PM

Answers

  • Column order can be resolved like this:

    $results += [pscustomobject][ordered]@{
              'Computer Name'=$computer
              'Build Version'='OFFLINE'
          }

    OR by selecting:

    | select 'ComputerName', Version | Export-Csv versions.csv -NoTypeInformation


    \_(ツ)_/


    • Edited by jrv Friday, April 13, 2018 7:18 PM
    • Marked as answer by robwm1 Friday, April 13, 2018 7:25 PM
    Friday, April 13, 2018 7:17 PM

All replies

  • Why connect to WMI? You can get operatingSystemVersion from AD without having to hit each computer. You also don't need to build an array and repeatedly append objects; use the pipeline. Example:


    Get-Content "version.txt" | ForEach-Object {
      Get-ADComputer $_.Trim() -Properties operatingSystemVersion | Select-Object Name,operatingSystemVersion
    } | Export-Csv "versions.csv" -NoTypeInformation
    


    -- Bill Stewart [Bill_Stewart]

    Friday, April 13, 2018 7:10 PM
    Moderator
  • Column order can be resolved like this:

    $results += [pscustomobject][ordered]@{
              'Computer Name'=$computer
              'Build Version'='OFFLINE'
          }

    OR by selecting:

    | select 'ComputerName', Version | Export-Csv versions.csv -NoTypeInformation


    \_(ツ)_/


    • Edited by jrv Friday, April 13, 2018 7:18 PM
    • Marked as answer by robwm1 Friday, April 13, 2018 7:25 PM
    Friday, April 13, 2018 7:17 PM
  • Hi Bill,

    That does seem to work but I will have to add logic to look in all domains in the forest which shouldn't be too hard. The script as written found some of the computers but when the computer being looked up was in a different domain, the console threw errors for each. Just some feedback on what I saw. Your version is seriously faster than my method as well, by a longshot.

    Do you have an idea of the likelihood that the data from AD is accurate? The reason I ask is I was writing some queries against the SCCM database using two different tables to get version information and discovered one table didn't always have accurate data.

    -Rob

    Friday, April 13, 2018 7:21 PM
  • jrv,

    using:

    | select 'ComputerName', Version | Export-Csv versions.csv -NoTypeInformation

    Solves my issue.

    Thank you!

    Rob

    Friday, April 13, 2018 7:26 PM
  • Bill's method is easier and faster and the data is as accurate.  SCCM databases can become out-of-date.

    For any method you will get errors if you are not an admin on the target domain.


    \_(ツ)_/

    Friday, April 13, 2018 7:28 PM
  • I plan to make Bill's idea my v2 for sure. I did a comparison and it is an exact match to my WMI method. I do like his idea for two reasons: 1) It is way faster than my idea and 2) It gives me an opportunity to get data even if the device is offline or unreachable.

    Thanks again!

    Friday, April 13, 2018 7:44 PM