soMuch2Learn
soMuch2Learn

Reputation: 187

Export-csv showing system.object[] instead of values

Have the following script that should return Exchange versions of the servers on a domain. However, the output shows the Name columns/field properly, but the Version and Role just have "System.Object[]" in each.

Import-Module ActiveDirectory
$domain = read-host "Enter the domain"
$ServerArray =@()
Get-ADComputer -SearchBase "OU=Servers,DC=$($domain),dc=local" -Filter {Name -like '*MBX*'} | Select -Exp Name | sort |
% { 
Write-Host "Processing $_"    
$obj = "" | Select "Name","Version","ServerRole"
$obj.Name = $_
$obj.Version = (Get-ExchangeServer).AdminDisplayVersion
$obj.ServerRole = (Get-ExchangeServer).ServerRole
$ServerArray += $obj
$obj = $null
}
$ServerArray | export-csv c:\users\$env:username\desktop\"$domain-ExchVer".csv -NoType

Upvotes: 0

Views: 3312

Answers (1)

Matt
Matt

Reputation: 46730

Get-ExchangeServer is clearly returning an array of objects here. You have the potential to have more than one Exchange server so I would expect to have more than one result. Also there is no need to call the cmdlet every loop pass. The information is not likely to change.

You on the other hand possibly on have the one server? You need to break the array and just get the string back. Not the only approach...

$exchangeServerDetails = Get-ExchangeServer | Select -First 1

Get-ADComputer -SearchBase "OU=Servers,DC=$($domain),dc=local" -Filter {Name -like '*MBX*'} | 
        Select-Object -ExpandProperty Name | 
        Sort-Object | ForEach-Object{
            Write-Host "Processing $_"    
            $obj = "" | Select "Name","Version","ServerRole"
            $obj.Name = $_
            $obj.Version = $exchangeServerDetails.AdminDisplayVersion
            $obj.ServerRole = $exchangeServerDetails.ServerRole
            # Pass the completed object down the pipeline. 
            $obj
} | Export-Csv "c:\users\$($env:username)\desktop\$domain-ExchVer.csv" -NoTypeInformation

Upvotes: 2

Related Questions