Our Man in Bananas
Our Man in Bananas

Reputation: 5981

Parsing an array of ojects into strings in PowerShell

I am calling Get-AdComputer like this:

[string[]] $computer = Get-ADComputer -properties * -filter {(operatingsystem -like "*Windows 7*")} |
    Where-Object {$_.name -like "*-*"} |
    Where-Object {$_.name -NotLike "V7-*"} |
    Where-Object {$_.name -NotLike "*-NONE"} |
    Where-Object {$_.name -NotLike "*-ONCALL"} |
    Where-Object {$_.name -NotLike "*-BLACKBAUD"} |
    Where-Object {$_.name -NotLike "SC-WIN7-1"} |
    Where-Object {$_.name -NotLike "UT-SWCLIENT-01"} |
    Select-Object Name, LastLogonDate

This creates a string array and I can access it like this:

$computer[1]

which returns

@{Name=W7-9HQPR3J; LastLogonDate=05/08/2017 09:45:13}

I need to output the data to a csv file in two columns Name, LastLogonDate

Unfortunately when I call $Computer | out-file -filepath $ServiceTagsPath -Force -Width 200 -Encoding ascii

I get one column with the headings on each line: enter image description here

My other requirement is to be able to use the Name column in web-service calls to get warranty information...

so how could I do that?

Upvotes: 0

Views: 1053

Answers (1)

pandemic
pandemic

Reputation: 1195

I would not cast the output to array of strings [string[]] but leave the casting to powershell. Then you can use command Export-Csv which works better with array of objects.

$computer = Get-ADComputer -properties * -filter {(operatingsystem -like "*Windows 7*")} |
    Where-Object {$_.name -like "*-*"} |
    Where-Object {$_.name -NotLike "V7-*"} |
    Where-Object {$_.name -NotLike "*-NONE"} |
    Where-Object {$_.name -NotLike "*-ONCALL"} |
    Where-Object {$_.name -NotLike "*-BLACKBAUD"} |
    Where-Object {$_.name -NotLike "SC-WIN7-1"} |
    Where-Object {$_.name -NotLike "UT-SWCLIENT-01"} |
    Select-Object Name, LastLogonDate

 $computer | Export-Csv "test.csv" -NoTypeInformation

Upvotes: 4

Related Questions