% { Get-...

问题描述 投票:2回答:1
you are REALLY close, and the answer is about what you would expect :)when doing group you automatically get a count property. just use this.

edit:you could also do this that could be even faster if propegating through many objects. when doing select you can add a custom query and a label for that query.

$DClist = (Get-ADForest).Domains | % { Get-ADDomainController –Filter * -Server $_ } | Select Site, Name, Domain

or

Site        Name      Domain      
----        ----      ------      
Site-A      DC-123    acme.local
Site-A      DC-ABC    acme.local
Site-B      DC-XYZ    domain.local
Site-C      DC-YPT    domain.local 

if you want to shorten it.

Site        Count_of_Name        
----        ----       
Site-A      2
Site-B      1
Site-C      1

$DcList | Group-Object Site

我正在使用PowerShell在Active Directory环境中建立一些脚本,目前正在努力寻找一种计算对象的方法。我的基本搜索是。

$DcList | Group-Object Site, Name

它产生了以下输出。

现在我想计算 "名称 "列中的对象数量,并显示这样的结果。

我已经尝试了很多方法 目前最接近的方法是使用..:

$DClist | Group-Object -Property Site | ForEach-Object -Process {
    [PSCustomObject]@{
      Site = $_.Name
      DCs = ($_.Group.Site)
    }
} 

但不幸的是,这不是正确的方法,因为它只计算 "站点 "的数量,而 "忽略 "其余的。也试过这样做,但也没有达到我预期的效果。

请帮我弄清楚这其中的逻辑。
windows powershell object count active-directory
1个回答
1
投票

我终于可以来了,但我想不出从'站点'栏中统计对象的方法。

$DClist = (Get-ADForest).Domains | % { Get-ADDomainController –Filter * -Server $_ } | Select Site, Name, Domain
$dclist|Group-Object site|ForEach-Object{
    [PSCustomObject]@{
        site = $_.name
        DCs = $_.group
        count = $_.count
    }
}

请帮助我。我觉得我现在已经很接近一个解决方案了 :)@{name='fieldname';expression={$_.reference.to.object}} @{n='field';e={$_.expression}}

$dclist|Group-Object site|ForEach-Object{
    $_|select @{n='site';e={$_.name}},count,@{n='DCs';e={$_.group}}
}
我正在使用PowerShell在一个Active Directory环境中建立一些脚本,目前正在努力寻找一种计算对象的方法。我的基本搜索是: $DClist = (Get-ADForest).Domains。
© www.soinside.com 2019 - 2024. All rights reserved.