You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell优化:获取22个AD组及成员时缩短运行时长

问题:AD组成员报告打印耗时过长优化需求

已将生成AD组子集及其成员报告的脚本运行时长从13分钟优化至约4分钟,但仍耗时过长。当前脚本通过单次调用Get-ADGroup获取所有组的member和description属性,仅需约13秒即可获取数据,但打印过程耗时近160秒——其中遍历成员的内层循环,无论成员数量多少,耗时均约7秒。


获取AD组数据及打印的PowerShell脚本

# Get all AD groups along with description and member data

$start_get = Get-Date
$adgroups = Get-ADGroup -server "" -Filter 'Name -like "Prefix*"' -Properties member,description
$stop_get = Get-Date
$dt_get = ($stop_get - $start_get).TotalSeconds
Write-Output "Time fetching groups: $dt_get s"

# Iterate over AD groups
$gstart = Get-Date
foreach ($group in $adgroups){
    # Write group data
    Write-Output "----------- Start of group -----------"
    Write-Output "Group Name        : $($group.name)"
    Write-Output "Group Description : $($group.description)"
    Write-Output "Group Members     :"

    # Iterate over group members
    $mstart = Get-Date
    foreach ($member in $group.member){
        # Extract the `CN` value in the member string by converting to a hash table
        $name = ($member -split "," | ConvertFrom-StringData).CN
        Write-Output "    $name"
    }
    $mstop = Get-Date
    $dt_member = ($mstop - $mstart).TotalSeconds
    Write-Output "--------------------------------------"
    Write-Output "Time printing members: $dt_member s"
    Write-Output "------------ End of group ------------"
}
$gstop = Get-Date
$dt_print = ($gstop - $gstart).TotalSeconds
Write-Output "Time printing group: $dt_print s"
Write-Output "Total run time     : $($start_get + $gstop) s"

脚本运行输出示例

Time fetching groups: 14.8095232 s
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 10
--------------------------------------
Time printing members: 7.3385221 s
------------ End of group ------------
...(其余输出略)

成员数量与打印耗时的绘图数据

[[10, 7.3385221],
 [6, 7.2661961],
 [2, 7.1176709],
 [1, 7.0904087],
 [3, 7.2423924],
 [28, 7.3510665],
 [8, 7.0845282],
 [8, 7.0749375],
 [54, 7.1177984],
 [0, 7.0871232],
 [10, 7.103549],
 [7, 7.0192055],
 [1, 7.0753799],
 [14, 7.0894799],
 [13, 7.0864959],
 [0, 7.0609977],
 [5, 7.0782862],
 [6, 7.0750823],
 [5, 7.2413143],
 [4, 8.1748071],
 [14, 7.4144964],
 [11, 7.0616258]]

用于分析耗时的Python绘图脚本

import matplotlib.pyplot as plt
import pandas as pd

with open("timings.txt") as f:
    groups = f.read().split('----------- Start of group -----------\n')[1:]
    

data = []
for g in groups:
    p = int(g.splitlines()[3].split(":")[-1])
    t = float(g.splitlines()[5].split(":")[-1].split()[0])
    data.append([p, t])
    
df = pd.DataFrame(data, columns=["Population", "Timing"])
avg = df.groupby("Population").mean()

ax = avg.plot(figsize=(5, 3), marker=".", ms=2**4, mec="black", color="salmon", lw=4, mew=2, legend=None)
ax.set_xlabel("Number of members in group")
ax.set_ylabel("Runtime (seconds)")
ax.grid(ls=":", lw=0.75)
plt.tight_layout()
plt.savefig("Timings.png")

希望解决打印过程耗时过长的问题,进一步缩短脚本运行时长。


内容的提问来源于stack exchange,提问作者Yoda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 22:25:21