New-Object to CSV problem

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Peter Jurgens Peter Jurgens 3 weeks, 5 days ago.

  • Author
    Posts
  • #56905
    Profile photo of Anthony Regnier
    Anthony Regnier
    Participant

    Hi Guys,

    I have this script that queries remote machines to get all java version installed.
    There is one query for 32 bits and another one for 64 bits as i need them to be separated in my csv.

    It works absolutely fine if a machine has one 32 bits version and/or a 64 bits too.
    However, if the machine has more than one 32 bits or 64 bits, the wmi query outputs 2 lines, and in my csv i get System.object[].

    Current CSV output:

    
    Computer Name	Statut	Java 32 bits version	Java 64 bits version
    GVA-GUEST-08	Online	Java 8 Update 91	
    GVA-GUEST-09	Online	System.Object[]	        Java 8 Update 40 (64-bit)
    
    

    Sorry for the Where-Object verylong lines, piping was the only way i found to add multiple conditions.

    Any help ?

    My code:

    $ComputerList = Read-Host -Prompt 'Enter path to computer list'
    $ComputerName = Get-Content $ComputerList
    
    Function Get-JavaVersion{
        Foreach ($C in $ComputerName) {
            If (-not (Test-Connection -cn $C -quiet -Count 1)){
                $OffHashTable=[ordered]@{
                    'Computer Name'=$C
                    'Statut'='Offline'
                    }
                New-Object PSObject -Property $OffHashTable
            }
    
            Else {
                $Java32 = gwmi -cn $C Win32_Product | ? Name -Like "Java*Update*" | ? Name -NotLike "*64*" | ? Name -NotLike "*Auto*" | ? Name -NotLike "*Development*" -ErrorAction SilentlyContinue
                $Java64 = gwmi -cn $C Win32_Product | ? Name -Like "Java*Update*64*" | ? Name -NotLike "*Auto*"| ? Name -NotLike "*Development*" -ErrorAction SilentlyContinue
            
                $OnHashTable=[ordered]@{
                    'Computer Name'=$C
                    'Statut'='Online'
                    'Java 32 bits version'=$Java32.caption
                    'Java 64 bits version'=$Java64.caption
                    }
                New-Object PSObject -Property $OnHashTable
            }       
        }
    }  
    
    Get-JavaVersion | Export-Csv C:\Javaversion.csv -NoTypeInformation
    
  • #56911
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    CSV's are all string values. Your captions should be arrays of captions, so you can join them and convert them to a string. Try:

    Get-JavaVersion | 
    Select "Computer Name", 
           Statut, 
           @{Name="Java 32 bits version";Expression={$Java32.caption -join ","}},
           @{Name="Java 64 bits version";Expression={$Java64.caption -join ","}} |
    Export-Csv C:\Javaversion.csv -NoTypeInformation
    
  • #56914
    Profile photo of Anthony Regnier
    Anthony Regnier
    Participant

    Sweeet !

    Just added -join "," to both $Java32.Caption and $Java64.Caption and it works !

    Thanks a lot Rob

  • #56930
    Profile photo of Alexandru
    Alexandru
    Participant

    you can also shorten the

    $Java32 = gwmi -cn $C Win32_Product | ? Name -Like "Java*Update*" | ? Name -NotLike "*64*" | ? Name -NotLike "*Auto*" | ? Name -NotLike "*Development*" -ErrorAction SilentlyContinue
    $Java64 = gwmi -cn $C Win32_Product | ? Name -Like "Java*Update*64*" | ? Name -NotLike "*Auto*"| ? Name -NotLike "*Development*" -ErrorAction SilentlyContinue

    with

    #this will match if Name ends with a digit like "Java 8 Update 91"
    $Java32 = gwmi -cn $C Win32_Product | ? Name -match "java.*\d$" 
    #this will match if Name ends with a parenthesis character like "Java 8 Update 91 (64-bit)"
    $Java64 = gwmi -cn $C Win32_Product | ? Name -match "java.*\)$"

    however , i'm not sure if this is constant in java versioning

  • #56978
    Profile photo of Peter Jurgens
    Peter Jurgens
    Participant

    Just want to point out if you do some further googling you will come across many articles that advise against querying the Win32_Product class as querying it may cause something like a reinstall of some applications or something like that. Most professionals will recommend querying the registry for such information over wmi.

You must be logged in to reply to this topic.