如何从PS命令获取计数

时间:2016-10-19 13:28:05

标签: windows powershell scripting

我有一个powershell命令:

Get-ADGroupMember -identity "CCVGHCFinReadOnly" -Recursive | foreach{ get-aduser -properties employeeid $_} | select SamAccountName,objectclass,name,employeeid | Export-Csv "C:\Temp\AD Membership\GovReadOnly.csv

此脚本非常适合返回AD组的成员。我还想为审计目的添加计数。因此,脚本应列出所有广告用户,然后列出底部的计数。

现在如果我使用|测量它给了我一个数,但这就是我得到的。所以我可以获得AD组中的实际用户或计数。如何使用这一个命令同时获得这两个命令?

2 个答案:

答案 0 :(得分:1)

$members = Get-ADGroupMember ... | select SamAccountName,objectclass,name,employeeid

$members | Export-Csv "C:\Temp\AD Membership\GovReadOnly.csv"
@($members).Count | Add-Content "C:\Temp\AD Membership\GovReadOnly.csv"

答案 1 :(得分:1)

Off the top of my head side effects come to mind. In this example I am incrementing a variable outside of the pipeline from within the pipeline. I admit it feels kind of gross. Maybe somebody else can suggest a solution that does not rely on this sort of side effect.

$count = 0
Get-ADGroupMember -identity "CCVGHCFinReadOnly" -Recursive `
| foreach { 
    $count++
    get-aduser -properties employeeid $_} `
| select SamAccountName,objectclass,name,employeeid `
| Export-Csv "C:\Temp\AD Membership\GovReadOnly.csv

Now you can do whatever you want with $count.