该脚本没有任何问题,但我不想使用自定义对象
####################
# Get AD Site List #
####################
Write-Verbose “Get AD Site List `r”
[array] $ADSites = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites
$ADSitesCount = $ADSites.Count
Write-Output “There are $ADSitesCount AD Sites `r”
ForEach ($Site in $ADSites)
{ ## OPEN ForEach Site in ADSites
$SiteName = $Site.Name
$SiteSubnets = $Site.Subnets
Write-Output “Site Name: $SiteName `r”
Write-Output “Site Servers: $SiteServers `r”
Write-Output ” `r”
}
更新的脚本:
#################### # Get AD Site List # ####################
Write-Verbose “Get AD Site List r”
$ADSites = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites
#$adsites= @{}
$ADSitesCount = $ADSites.Count
Write-Output “There are $ADSitesCount AD Sites r”
$properties = @{'SiteName'=$ADSites.Name; 'SiteServers'=$ADSites.server; }
$object = New-Object –TypeName PSObject –Prop $properties
Write-Output $object
答案 0 :(得分:0)
不确定你是否可能过度思考这个问题,但这只会使用Select-Object
只获取你想要的属性,以表格格式提供相同的信息
$ADSites = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites
Write-Output “There are $($ADSites.Count) AD Sites `r”
$ADSites | Select-Object name,servers
混淆输出
There are 4 AD Sites
Name Servers
---- -------
SITE1 {SERVER1.DOMAIN.NET, SERVER2.DOMAIN.NET}
STE345 {SERVER3.DOMAIN.NET}
LOCCOMP {SERVER4.DOMAIN.NET}
THING {SERVER9720.DOMAIN.NET, SERVER7.DOMAIN.NET}
答案 1 :(得分:0)
问题出在这一行:
$properties = @{'SiteName'=$ADSites.Name; 'SiteServers'=$ADSites.server; }
$ ADSites是一个数组。它包含所有的网站名称,因此您将创建的内容如下所示:
Sites
----
{a, b, c}
解决方案是创建一个包含多个对象的数组,并为每个站点添加一个对象:
$collection= @()
foreach($site in $ADSites){
$properties = @{ "Name" =$site.Name; "SiteServers"=$site.Server}
$collection +=(New-Object –TypeName PSObject –Prop $properties )
}
$collection
写输出用于输出到管道而不是用于产生屏幕输出