在 Powershell 的 csv 文件中获取用户计数和实际用户数据

Get user count and actual userdata in a csv file in Powershell

我试图通过 get-aduser 获取用户数和实际用户信息,但失败得很惨。

    Get-ADUser -Server $test -Credential cred -Filter{enabled -eq $true} | Where-Object { $_.DistinguishedName -notlike '*OU=.Service Accounts,*' -and $_.samaccountname -notlike '*health*' } | Select-object Samaccountname,surname,givenname | Where { $excludedusers -NotContains$_.Samaccountname } | format-list > 'C:\Scripts\Test\enabled_users_and count.csv'

是当前代码。我可以像这样在 format-list 之前添加一个 .count:

(Get-ADUser -Server $test -Credential cred -Filter{enabled -eq $true} | Where-Object { $_.DistinguishedName -notlike '*OU=.Service Accounts,*' -and $_.samaccountname -notlike '*health*' } | Select-object Samaccountname,surname,givenname | Where { $excludedusers -NotContains$_.Samaccountname }).count

但我只得到用户数,如前所述,我需要两者。

非常感谢您的帮助。

你需要两个不同的东西,Count不需要是csv中的一个字段,你可以通过最终输出的行数得到它

您可能需要控制台使用的计数,无论如何将其保存在最终输出中在逻辑上是不正确的。 (如果我没理解错的话)

您可以将其保存到变量中,然后进行导出或计数检查...

$Users = Get-ADUser -Server $test -Credential cred -Filter{enabled -eq $true} | 
Where-Object { $_.DistinguishedName -notlike '*OU=.Service Accounts,*' -and $_.samaccountname -notlike '*health*' } |
Where { $excludedusers -NotContains $_.Samaccountname }

导出:

$Users | Select-object Samaccountname,surname,givenname | 
Export-CSV 'C:\Scripts\Test\enabled_users_and count.csv'

检查计数:

$Users.Count