首页 > 解决方案 > 在 Powershell 的 csv 文件中获取用户计数和实际用户数据

问题描述

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

    Get-ADUser -Server $test -Credential $1cred -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'

是当前代码。我可以像这样在格式列表之前添加一个 .count :

(Get-ADUser -Server $test -Credential $1cred -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

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

非常感谢您的帮助。

标签: powershellpowershell-2.0powershell-3.0powershell-4.0

解决方案


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

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

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

$Users = Get-ADUser -Server $test -Credential $1cred -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

推荐阅读