向函数添加计数过程

问题描述

所以我需要计算函数中使用的变量的值。我不能运行简单的 $count++,因为该变量的值很少等于 1。

Function Set-bhRTGmembers_logonly {
    [CmdletBinding(SupportsShouldProcess)]
    $DirectReports = Get-Directreport $manager -norecurse  | Select-Object -expand samAccountName
    #  Get manager's 'report to <manager>' group again to update members
    $managerReportToGroup = Get-ADGroup -SearchBase $OU -Filter "Name -like 'Report to $Manager'"
    if ($managerReportToGroup) {
        $script:logonlyAddUserCount++
        $LogLine = "Report to $Manager would be updated with $DirectReports"
        Log-Write -LogPath $slogonlyFile -LineValue $LogLine 
    }
    else {
        $LogLine = "Group for $Manager not found,would be updated with $DirectReports"
        Log-Write -LogPath $slogonlyFile -LineValue $LogLine 
    }
}

第 7 行试图计算 $DirectReports 中 SamAccountNames 的数量,我该怎么做?

解决方法

如果您需要 $DirectReports 中包含的项目数,您可以简单地使用其别名属性 Count(如果它是一个集合)或 Measure-Object。无论 Measure-Object 中包含多少项,$DirectReports 都有效。

($DirectReports | Measure-Object).Count