Skeer
Skeer

Reputation: 173

Adding a count process to a function

So I need to tally up the values of a variable used in a function.. I can't run a simple $count++ because the value of that variable rarely equals 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 
    }
}

Line 7 is trying to count the number of SamAccountNames in $DirectReports, how can I do this?

Upvotes: 0

Views: 120

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25001

If you need the number of items contained in $DirectReports, you can simply use its alias property Count (if it is a collection) or Measure-Object. Measure-Object works no matter the number of items contained within $DirectReports.

($DirectReports | Measure-Object).Count

Upvotes: 1

Related Questions