此代码用于从嵌套组中获取所有用户。我希望它并行运行以获得更快的运行时间。最好的方法是什么?我试图在我的最后一个函数上对我的 foreach 进行并行以运行整个代码,但我无法在并行内调用该函数。提前感谢任何已经遇到这种逻辑的人。
function Get-CachedADUSER($AduserCached){
# $AduserCached = $resultHOlder
$cachedResults = Get-Variable -Scope Global -Name $AduserCached -ErrorAction SilentlyContinue
if($null -ne $cachedResults){
$existing = write-host "Check: i found existing"
return $cachedResults.value , $existing
}
else{
$searching = write-host "Check: Searching"
$results = get-aduser $AduserCached -properties enabled, displayname, EmployeeNumber, LastLogonDate, PasswordLastSet
Set-CachedADUSER -setAduserCached $AduserCached -value $results
return $results.value , $searching
}
}
Function Set-CachedADUSER($setAduserCached,$value){
Set-Variable -Scope Global -Name $setAduserCached -Value $value
return $value
}
function Get-ADUsers_cachedV2 {
param (
[Parameter(ValuefromPipeline = $true, mandatory = $true)][String] $GroupName
)
[int]$circular = $null
# result holder
$resultHolder = @()
$table = $null
$nestedmembers = $null
$adgroupname = $null
# get members of the group and member of
$ADGroupname = get-adgroup $groupname -properties memberof, members
# $ADGroupname = Get-CachedGroupMembership -GroupName $groupName -properties memberof, members
# list all members as list (no headers) and save to var
$memberof = $adgroupname | select -expand memberof
if ($adgroupname) {
if ($circular) {
$nestedMembers = Get-ADGroupMember -Identity $GroupName -recursive
$circular = $null
}
else {
$nestedMembers = Get-ADGroupMember -Identity $GroupName | sort objectclass -Descending
# if get adgroupmember returns nothing, it uses the members for ordinary getADGroup
if (!($nestedmembers)) {
$unknown = $ADGroupname |select -expand members
if ($unknown) {
$nestedmembers = @()
foreach ($member in $unknown) {
$nestedmembers += get-adobject $member #-ErrorAction SilentlyContinue
}
}
}
}
# loops through each member
ForEach($nestedmember in $nestedmembers){
# creates the properties into a custom object.
$Props = @{
Type = $nestedmember.objectclass;
Name = $nestedmember.name;
DisplayName = "";
ParentGroup = $ADgroupname.name;
# ParentGroup = $ADgroupname.name;
Enabled = "";
# Nesting = $nesting;
# DN = $nestedmember.distinguishedname;
# Comment = ""
EmployeeNumber = "";
LastLogonDate = "";
PasswordLastSet = "";
}
# if member object is a user
if ($nestedmember.objectclass -eq "user") {
# saves all the properties in the table.
$nestedADMember = Get-CachedADUSER -AduserCached $nestedmember.Name -properties enabled, displayname, EmployeeNumber, LastLogonDate, PasswordLastSet
$table = new-object psobject -property $props
$table.enabled = $nestedadmember.enabled
$table.name = $nestedadmember.samaccountname
$table.displayname = $nestedadmember.displayname
$table.EmployeeNumber = $nestedadmember.EmployeeNumber
$table.LastLogonDate = $nestedadmember.LastLogonDate
$table.PasswordLastSet = $nestedadmember.PasswordLastSet
#save all in 1 storage
$resultHOlder += $table | select type, name, displayname, parentgroup, enabled, EmployeeNumber, LastLogonDate, PasswordLastSet
}
# if member object is group
elseif ($nestedmember.objectclass -eq "group") {
$table = new-object psobject -Property $props
# if circular, meaning the groups member of list contains one of its members.
# e.g. if group 2 is a member of group 1 and group 1 is a member of grou 2
if ($memberof -contains $nestedmember.distinguishedname) {
$table.comment = "Circular membership"
$circular = 1
}
# for circular output
#$table | select type, name, displayname, parentgroup, nesting, enabled, dn, comment
#calling function itself
$resultHOlder += Get-ADUsers_cachedV2 -GroupName $nestedmember.distinguishedName
}
else {
if ($nestedmember) {
$table = new-object psobject -property $props
$resultHolder += $table | select type, name, displayname, parentgroup, enabled, EmployeeNumber, LastLogonDate, PasswordLastSet
}
}
}
}
return $resultHOlder
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++ + 这就是我想要进行并行处理以加快执行速度
function Get-NestedGroupUsers_cachedV2 {
param (
[Parameter(Mandatory = $true)][String]$FileName,
[Parameter(Mandatory = $true)][String]$searchFilePath
)
$storageHolder = @()
$groupList = Get-Content $searchFilePath
$groupList | ForEach-Object {
$allusers = Get-ADUsers_cachedV2 -GroupName $_
$storageHolder += $allusers
}
$storageHolder | select ParentGroup, Name, EmployeeNumber, Enabled, LastLogonDate, PasswordLastSet |Export-Csv -Path "C:\Users\***\****\$FileName.csv" -NoTypeInformation #-Force
}