如何解决结束,未调用循环外函数,添加运行计数
所以,我有一个包含 8 个函数的长脚本。最后,在执行部分,我在 ForEach 循环中调用这些函数,其中两个。我想做的是在循环之后和循环之外调用其中之一。
5 Log-Write
行就是我要说的。现在,如果我运行我的脚本,只有第 5 行会出现在日志文件中。我知道我的语法被提升了,但我不知道如何在循环处理后的脚本最后以摘要方式调用该函数。
代码如下:
#---------------------------------------------------------[Initializations]--------------------------------------------------------
Param (
[Parameter(Mandatory=$false)]
[Switch]$logonly
)
# Dot Source required Function Libraries
#. "\\server\scripts\Logging_Functions.ps1"
. "c:\users\documents\powershell\Functions\Logging_Functions.ps1"
# Error Action
$ErrorActionPreference = 'silentlycontinue'
# Debug preference
$global:DebugPreference = "continue"
# WhatIf Preference,uncomment to run script in a logging only function
#$WhatIfPreference = $true
#----------------------------------------------------------[Declarations]----------------------------------------------------------
# Script Version
$sScriptVersion = "1.0"
Import-Module ActiveDirectory
# Log File Info
$sLogPath = "C:\Users\Documents\powershell\Logs"
#$sLogPath = "\\server\e$\Logs"
$sLogName = "Set-LitmosGroups_$(get-date -f yyyy-MM-dd_HH-mm-ss).log"
$slogonlyPath = "C:\Users\Documents\powershell\Logs"
$slogonlyName = "\Set-LitmosGroups (Log Only)_$(get-date -f yyyy-MM-dd_HH-mm-ss).log"
$sLogFile = Join-Path -Path $sLogPath -Childpath $sLogName
$slogonlyFile = Join-Path -Path $slogonlyPath -Childpath $slogonlyName
$LogLine = $null
#$logonly = $null
# Variable Initializations
# Org Unit where the target groups reside (Litmos)
$OU = "ou=test_litmos,ou=test accounts,ou=domain,dc=net"
# Org unt containing the All Managers security group
$OU2 = "CN=All Managers,OU=Organizational,OU=Groups,OU=domain,DC=net"
# Get member of the 'ALL Managers' security group
$Managers = Get-ADGroupMember -identity $OU2 | Select-Object -expandproperty samaccountname
# Get AD groups with Report to in the name in $ou
$ReportsTo = Get-adgroup -searchbase $ou -filter "Name -like 'Report to *'" |
Select-Object -expandproperty name
$samecount = 0
$addgroupcount = 0
$addusercount = 0
$logonlyAdduserCount = 0
$logonlyGroupCount = 0
#----------------------------------------------------------[Functions]-------------------------------------------------------------
Function Get-DirectReport {
#requires -Module ActiveDirectory
[CmdletBinding()]
param(
[Parameter(
Mandatory = $false,ValueFromPipeline = $true,ValueFromPipelineByPropertyName = $true
)]
[string] $SamAccountName,[switch] $norecurse
)
BEGIN {}
PROCESS {
$UserAccount = Get-ADUser $SamAccountName -Properties DirectReports,displayName
$UserAccount | select -ExpandProperty DirectReports | ForEach-Object {
$User = Get-ADUser $_ -Properties DirectReports,displayName,Title,EmployeeID
if ($null -ne $User.EmployeeID) {
if (-not $norecurse) {
Get-DirectReport $User.SamAccountName
}
[PSCustomObject]@{
SamAccountName = $User.SamAccountName
UserPrincipalName = $User.UserPrincipalName
displayName = $User.displayName
Manager = $UserAccount.displayName
}
}
}
}
END {}
}
Function New-bhReportToGroup {
[CmdletBinding(SupportsShouldProcess)]
$Log1 = "New group for " + $manager + " has been created."
$Log2 = "Group for " + $manager + " already exists."
#From on when you see the below line $script:<variable> that sets the scope for that variable to the entire script which means other functions can use the value
$script:ReportsTo = $ReportsTo -replace ("Report to ","")
if ($manager -notin $ReportsTo) {
$LogLine = $Log1
$addgroupcount = $addcount + 1
Log-Write -LogPath $sLogFile -LineValue $LogLine
}
else {
$LogLine = $Log2
Log-Write -LogPath $sLogFile -LineValue $LogLine
}
}
Function New-bhReportToGroup_logonly {
#[CmdletBinding(SupportsShouldProcess)]
$Log1 = "New group for " + $manager + " would have been created in $OU."
$Log2 = "Group for " + $manager + " already exists in $OU."
$script:ReportsTo = $ReportsTo -replace ("Report to ","")
if ($manager -notin $ReportsTo) {
$Script:addcount = $addcount +1
$LogLine = $Log1
$logonlyGroupCount = $logonlyGroupCount + 1
Log-Write -LogPath $logonlyFile -LineValue $LogLine
}
else {
$Script:samecount = $samecount + 1
$LogLine = $Log2
Log-Write -LogPath $logonlyFile -LineValue $LogLine
}
}
Function Get-bhDReports {
[CmdletBinding(SupportsShouldProcess)]
$directreports = Get-Directreport $manager -norecurse | Select-Object samAccountName
if ($null -ne $directreports) {
$LogLine = "Gathering direct reports for " + $manager
Log-Write -LogPath $sLogFile -LineValue $LogLine
}
else {
$LogLine = $manager + " has no reports."
Log-Write -LogPath $sLogFile -LineValue $LogLine
}
}
Function Set-bhRTGmembers {
[CmdletBinding(SupportsShouldProcess)]
# Get manager's 'report to <manager>' group again to update members
$managerReportToGroup = Get-ADGroup -SearchBase $OU -Filter "Name -like 'Report to $Manager'"
$directreports = Get-Directreport $manager -norecurse | Select-Object samAccountName
if ($managerReportToGroup) {
Add-ADGroupMember -identity $managerReportToGroup.Name -members $DirectReports
Add-ADGroupMember -identity $managerReportToGroup.name -members $Manager
$LogLine = "Report to " + $Manager + " updated."
$Addusercount = $Addusercount +1
Log-Write -LogPath $sLogFile -LineValue $LogLine
}
else {
$LogLine = "Could not find group for " + $Manager
Log-Write -LogPath $sLogFile -LineValue $LogLine
}
}
Function Set-bhRTGmembers_logonly {
# Get manager's 'report to <manager>' group again to update members
$managerReportToGroup = Get-ADGroup -SearchBase $OU -Filter "Name -like 'Report to $Manager'"
if ($managerReportToGroup) {
$logonlyAdduserCount = $logonlyAddUserCount +1
$LogLine = "Report to " + $Manager + " would be updated with "+ $logonlyAddUserCount + " " + $DirectReports.name
Log-Write -LogPath $logonlyFile -LineValue $LogLine
}
else {
$LogLine = "Could not find group for " + $Manager
Log-Write -LogPath $logonlyFile -LineValue $LogLine
}
}
Function Remove-bhOOSGroups {
[CmdletBinding(SupportsShouldProcess)]
$report = $report -replace ("Report to ","")
if ($Report -notin $managers) {
Remove-ADGroup -Identity "Report to $Report" -confirm:$false
$LogLine = $report + " user has fell out of scope,Report group removed."
Log-Write -LogPath $sLogFile -LineValue $LogLine
}
else {
Continue
}
}
Function Remove-bhOOSGroups_logonly {
$report = $report -replace ("Report to ","")
if ($Report -notin $managers) {
$LogLine = $report + " user has fell out of scope,Report group would be removed."
Log-Write -LogPath $slogonlyFile -LineValue $LogLine
}
else {
Continue
}
}
#----------------------------------------------[ Execution ]------------------------------------------------
#Managers | Foreach-Object -Parallel {
Foreach ($Manager in $Managers) {
if (-not $logonly) {
$Directreports = Get-Directreport $manager -norecurse | Select-Object -expand samAccountName
Ttime = (Get-Date).ToString('T')
Log-Write -LogPath $sLogFile -LineValue $Manager + "'s" + "Direct reports are: " $Directreports
New-bhReportToGroup
Get-bhDReports
Set-bhRTGmembers
Log-Write -LogPath $sLogFile -LineValue "========================[$Time ]==============================="
} else {
New-bhReportToGroup_logonly
Get-bhDReports
Set-bhRTGmembers_logonly
Log-Write -LogPath $logonlyFile -LineValue "========================[ logonly ]==============================="
}
}
Foreach ($Report in $ReportsTo) {
If (-not $logonly){
Remove-bhOOSGroups
} else {
#ForEach ($Report in $ReportsTo) {
Remove-bhOOSGroups_logonly
}
}
#}
Log-Write -LogPath $sLogPath -Linevalue $addgroupcount + " New groups added"
Log-Write -Logpath $sLogPath -Linevalue $AddUserCount + " New users added to groups"
Log-Write -Logpath $slogonlyPath -Linevalue $logonlyAdduserCount + " Users who would be added"
Log-Write -Logpath $slogonlyPath -Linevalue $logonlyGroupCount + " Groups that would be added"
Log-Write -LogPath $sLogPath -Linevalue "====[END]====="
解决方法
这是一个猜测,因为我看不到您的日志写入功能,但由于只有第 5 行有效,我敢打赌您可以通过将字符串传递给 linevalue 参数来显示其他行(通过将整行放在双引号中,因此它采用变量值)。像这样:
Log-Write -Logpath $sLogOnlyPath -Linevalue "$LogOnlyGroupCount Groups that would be added"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。