具有以下脚本,该脚本应返回域上服务器的Exchange版本。但是,输出正确显示Name列/字段,但Version和Role在每个中都有“System.Object []”。
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
答案 0 :(得分:2)
Get-ExchangeServer
显然是在这里返回一个对象数组。您有可能拥有多个Exchange服务器,因此我希望有多个结果。此外,无需每次循环调用cmdlet。该信息不太可能更改。
$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