在Get cmdlet中筛选出空参数值

时间:2018-10-12 11:58:16

标签: windows powershell server active-directory

我试图找出正确的语法以及是否使用Select-Object或Where-Object来完成我想要的事情。

基本上,如果我运行一个基本的Getcmdlet以表格式返回对象的参数列表及其值,我想用空值隐藏参数。

例如,我想要此输出:(输出为示例)

AmirOU            Count
----------------- -----
tegdg               0
wfw                 0
Amir                7
wtw                 58
wretret             0
ergre               2
iubuibfr            0
User                593
wetwe               0
wdqdd               50
wetwe               0
Groups              0
wtw                 0
ddewe               17
wetwe               0
wetwe               72
wf                  11
fdgd                1
fdbd                53
rete                0
sf4t                0
4tg                 0
sgsdt               0
dsfet               0
ergre               0

看起来像这样:

AmirOU            Count
----------------- -----

Amir                7
wtw                 58
ergre               2
User                593
5t5                 50
grtg                17
wetwe               72
wf                  11
fdgd                1
fdbd                53

这是我的代码:

$total = $null
Get-ADOrganizationalUnit -filter * -SearchBase 'OU=Amir,OU=test,DC=sina,DC=local' | 

foreach {

    if($_.distinguishedname -notmatch 'DisabledUsers|Exchange'){



        $users=Get-ADUser -filter * -searchbase $_.distinguishedname -searchscope Onelevel | where-object enabled -eq true 

        $test=($users | measure-object).count

        $total += $test



        New-Object psobject  -Property @{

            Count= $test

            AmirOU = $_.Name;

        }

    }

}

Write-Host "`nTotalt: $total"

1 个答案:

答案 0 :(得分:2)

您可以在其中简单地添加一个测试,以查看.count是否不为0,像这样:

$total = 0

Get-ADOrganizationalUnit -filter * -SearchBase 'OU=Amir,OU=test,DC=sina,DC=local' | 
    ForEach-Object {
         if ($_.distinguishedname -notmatch 'DisabledUsers|Exchange') {
            $users = Get-ADUser -filter * -searchbase $_.distinguishedname -searchscope Onelevel | Where-Object { $_.Enabled -eq $true }
            $test = ($users | Measure-Object).count

            if ($test) {
                $total += $test

                New-Object psobject -Property @{
                    Count = $test
                    AmirOU = $_.Name
                }
            }
        }
    }

Write-Host "`nTotal: $total"