none
PS v2 + custom objects + multiple values + export-csv RRS feed

  • Question

  • I have a script that gets a list of patches from WSUS and exports a subset of their attributes to a .csv file by creating a custom object. When I run the script and have the output sent to the host, I see the KB field looks like this: {KB123456}, which is fine. When I run the script and use "Export-Csv", the KB column says, "System.Collections.Specialized.StringCollection".

    What do I need to change, in the code, to get the actual value(s) of the StringCollection into my CSV? I've seen the Join function, but haven't been able to make it work.

    Thanks.

    $PrimaryWSUSServer = 'wsus.company.local'
    
    Function Get-PatchesPendingApproval {
        $updateScope = New-Object Microsoft.UpdateServices.Administration.UpdateScope
        $updatescope.FromArrivalDate = (Get-Date).AddMonths(-2)
        $updatescope.ApprovedStates = [Microsoft.UpdateServices.Administration.ApprovedStates]::NotApproved
        $needApproval = $wsus.GetUpdates($updateScope)
        Return $needApproval
    }
    
    #Begin Script
    [void][reflection.assembly]::LoadWithPartialName(“Microsoft.UpdateServices.Administration”)
    $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($PrimaryWSUSServer,$False)
    
    $updatesToApprove = Get-PatchesPendingApproval
    
    Foreach ($item in $updatesToApprove) {
        $updateProps=@{
            'Title'=$item.Title
            'KB'=$item.KnowledgebaseArticles
            'ApprovalDate'=Get-Date
            'ArrivalDate'=$item.ArrivalDate
        }
        
        New-Object PsObject -Property $updateProps
    }


    • Edited by mhashemi Thursday, March 12, 2015 2:17 PM fixed grammar
    Thursday, March 12, 2015 2:15 PM

Answers

  • As soon as I wrote this, I had an epiphany. The object should look like this: 

    $updateProps=@{
            'Title'=$item.Title
            'KB' = (@($item.KnowledgebaseArticles) -join ',')
            'ApprovalDate'=Get-Date
            'ArrivalDate'=$item.ArrivalDate
        }

    Thanks.

    • Marked as answer by mhashemi Thursday, March 12, 2015 2:24 PM
    Thursday, March 12, 2015 2:24 PM