在函数中添加计数过程
Adding a count process to a function
所以我需要计算函数中使用的变量的值。我不能 运行 一个简单的 $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 中 SamAccountName 的数量,我该怎么做?
如果您需要 $DirectReports
中包含的项目数,您可以简单地使用其别名 属性 Count
(如果它是一个集合)或 Measure-Object。 Measure-Object
与 $DirectReports
.
中包含的项目数量无关
($DirectReports | Measure-Object).Count
所以我需要计算函数中使用的变量的值。我不能 运行 一个简单的 $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 中 SamAccountName 的数量,我该怎么做?
如果您需要 $DirectReports
中包含的项目数,您可以简单地使用其别名 属性 Count
(如果它是一个集合)或 Measure-Object。 Measure-Object
与 $DirectReports
.
($DirectReports | Measure-Object).Count