0

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.