如何解决向脚本添加 -WhatIf 或“仅记录”功能?
所以我有下面的脚本.. 在过去的几周里,你们中的许多人都帮助了我。无论如何,我的老板希望我为此添加 -whatif 功能。或者某种模式运行但只记录 whatif 部分。无论如何,我对整个函数还是陌生的,但是从我收集的信息来看,如果您使用的是高级函数,那么您的函数可以访问其他可用的 Powershell 开关。但是如何对包含多个函数的脚本实现 -whatif 呢?是否像给每个函数添加参数一样简单? 这是我的清理代码:
#---------------------------------------------------------[Initializations]--------------------------------------------------------
#Dot Source required Function Libraries
#. "\\server\e$\scripts\Logging_Functions.ps1"
. "c:\users\documents\powershell\Logging_Functions.ps1"
#Error Action
$ErrorActionPreference = 'silentlycontinue'
#Debug preference
$global:DebugPreference = "continue"
#----------------------------------------------------------[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"
$sLogFile = Join-Path -Path $sLogPath -Childpath $sLogName
$LogLine = $null
#Variable Initializations
#Org Unit where the target groups reside (Litmos)
$OU = "ou=test_litmos,ou=test accounts,ou=domain,dc=company,dc=net"
#Org unt containing the All Managers security group
$OU2 = "CN=All Managers,OU=Organizational,OU=Groups,OU=domain,DC=company,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
#----------------------------------------------------------[Functions]-------------------------------------------------------------
Function Get-DirectReport {
#requires -Module ActiveDirectory
<#
.SYnopSIS
This script will get a user's direct reports recursively from ActiveDirectory unless specified with the norecurse parameter.
It also uses the user's EmployeeID attribute as a way to exclude service accounts and/or non standard accounts that are in the reporting structure.
.NOTES
Name: Get-DirectReport
Author: theSysadminChannel
Version: 1.0
DateCreated: 2020-Jan-28
.LINK
https://thesysadminchannel.com/get-direct-reports-in-active-directory-using-powershell-recursive -
.ParaMETER SamAccountName
Specify the samaccountname (username) to see their direct reports.
.ParaMETER norecurse
Using this option will not drill down further than one level.
.EXAMPLE
Get-DirectReport username
.EXAMPLE
Get-DirectReport -SamAccountName username -norecurse
.EXAMPLE
"username" | Get-DirectReport
#>
[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()]
$script:ReportsTo = $ReportsTo -replace ("Report to ","")
if ($manager -notin $ReportsTo) {
new-adgroup -name "Report to $manager" -groupscope global -path $ou
#write-host "Report to $manager"
$LogLine = "New group for " + $manager + " has been created."
Log-Write -LogPath $sLogFile -LineValue $LogLine
}
else {
#write-host "group for $manager already exists"
$LogLine = "Group for " + $manager + " already exists."
Log-Write -LogPath $sLogFile -LineValue $LogLine
}
}
Function Get-bhDReports {
[CmdletBinding()]
$script:directreports = Get-Directreport $manager -norecurse | Select-Object samAccountName
if ($null -ne $directreports) {
#write-host "Got reports for $manager"
$LogLine = "Gathering direct reports for " + $manager
Log-Write -LogPath $sLogFile -LineValue $LogLine
} else {
#write-host "$manager has no reports"
$LogLine = $manager + " has no reports."
Log-Write -LogPath $sLogFile -LineValue $LogLine
}
}
Function Set-bhRTGmembers {
[CmdletBinding()]
#
# Get manager's 'report to <manager>' group again to update members
$managerReportToGroup = Get-ADGroup -SearchBase $OU -Filter "Name -like 'Report to $Manager'"
if ($managerReportToGroup) {
Add-ADGroupMember -identity $managerReportToGroup.Name -members $DirectReports
Add-ADGroupMember -identity $managerReportToGroup.name -members $Manager
#write-host "Report to $manager updated"
$LogLine = "Report to " + $Manager + " updated."
Log-Write -LogPath $sLogFile -LineValue $LogLine
}
else {
#write-host "Couldnt find group for $manager"
$LogLine = "Could not find group for " + $Manager
Log-Write -LogPath $sLogFile -LineValue $LogLine
}
}
Foreach ($Manager in $Managers) {
New-bhReportToGroup
Get-bhDReports
Set-bhRTGmembers
}
Foreach ($Report in $ReportsTo) {
$report = $report -replace ("Report to ","")
if ($Report -notin $managers)
{Remove-ADGroup -Identity "Report to $Report" -confirm:$false
write-host "$report removed"
$LogLine = $report + " user has fell out of scope,Report group removed."
Log-Write -LogPath $sLogFile -LineValue $LogLine
} else {
$LogLine = "No groups deleted.`n"
Log-Write -LogPath $sLogFile -LineValue $LogLine
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。