Powershell:如何在获得22个ADGroup和210个成员时将运行时间从3分钟减少到

7gyucuyw  于 2023-01-02  发布在  Shell
关注(0)|答案(1)|浏览(135)

我正在生成一个AD组及其用户子集的报告,但对我来说,脚本需要很长时间才能完成。我已设法将运行时间从以前版本的13分钟左右减少到4分钟左右。
下面给出了脚本及其输出。我这样设计脚本,以便只需要对服务器进行一次调用。我在Get-ADGroup调用中收集了所有想要的数据,并在循环中获取属性。计时表明,获取所有组大约需要13秒,而打印过程几乎需要160秒。我希望打印非常快。不会超过一秒钟。
无论组中有多少成员,内部循环(即遍历组成员的循环)大约需要7秒:

# 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
        # Credits: https://stackoverflow.com/a/67503277/6218849
        $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 ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 6
--------------------------------------
Time printing members: 7.2661961 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 2
--------------------------------------
Time printing members: 7.1176709 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 1
--------------------------------------
Time printing members: 7.0904087 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 3
--------------------------------------
Time printing members: 7.2423924 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 28
--------------------------------------
Time printing members: 7.3510665 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 8
--------------------------------------
Time printing members: 7.0845282 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 8
--------------------------------------
Time printing members: 7.0749375 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 54
--------------------------------------
Time printing members: 7.1177984 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 0
--------------------------------------
Time printing members: 7.0871232 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 10
--------------------------------------
Time printing members: 7.103549 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 7
--------------------------------------
Time printing members: 7.0192055 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 1
--------------------------------------
Time printing members: 7.0753799 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 14
--------------------------------------
Time printing members: 7.0894799 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 13
--------------------------------------
Time printing members: 7.0864959 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 0
--------------------------------------
Time printing members: 7.0609977 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 5
--------------------------------------
Time printing members: 7.0782862 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 6
--------------------------------------
Time printing members: 7.0750823 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 5
--------------------------------------
Time printing members: 7.2413143 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 4
--------------------------------------
Time printing members: 8.1748071 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 14
--------------------------------------
Time printing members: 7.4144964 s
------------ End of group ------------
----------- Start of group -----------
Group Name        :
Group Description :
Group Members     :
Population        : 11
--------------------------------------
Time printing members: 7.0616258 s
------------ End of group ------------
Time printing group: 158.3620035 s
Total run time     : 173.1715267 s

绘图中使用的数据:

[[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]]

绘图脚本:

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")
g2ieeal7

g2ieeal71#

根据@Santiago Squarzon和@Ranadip Dutta的建议,我研究了多线程。下面的解决方案是对this answer的直接修改。理想的线程数似乎是4,这将总运行时间减少到大约28秒。

# Get all AD groups along with description and member data
# Using multithreading
# Credits: https://stackoverflow.com/a/43685953/6218849

# Get all AD groups with members and descriptions
$adgroups = Get-ADGroup -server "<myserver>" -Filter 'Name -like "Prefix*"' -Properties member,description

# Define block to be distributed over threads
$block = {
  Param ($adgroup)
  Write-Output "----------- Start of group -----------"
  Write-Output "Group Name   : $($adgroup.name)"
  Write-Output "Group Desc   : $($adgroup.description)"
  Write-Output "Group Members: "

  # Iterate over AD group members
  foreach ($member in $adgroup.member) {
    # Extract the `CN` value in the member string by converting to a hash table
    # Credits: https://stackoverflow.com/a/67503277/6218849
    $name = ($member -Split "," | ConvertFrom-StringData).CN
    Write-Output "    $name"
  }
  "------------ End of group ------------"
  Write-Output ""
}

# Clear job list
Get-Job | Remove-Job

# Spawn threads
$maxThreads = 4
foreach ($adgroup in $adgroups) {
  While ($(Get-Job -State Running).count -ge $maxThreads) {
    Start-Sleep -Milliseconds 3
  }
  Start-Job -ScriptBlock $block -ArgumentList $adgroup | Out-Null
}

# Wait for jobs to finish
While ($(Get-Job -State Running).count -gt 0) {
  Start-Sleep 1
}

# Get the data for each job
foreach ($job in Get-Job) {
  $data = Receive-Job -Id ($job.Id)
  Write-Output $data
}

# Clear job list
Get-Job | Remove-Job

时间:

================
N   Runtime (s)
----------------
1   141.9
2    63.4
4    27.8
8    29.1
16   52.4
================

相关问题