我有另一个脚本将已终止的用户移动到Active Directory中的“已终止”OU中。它在说明中添加了一个注释,并将当前日期添加到“公司”字段中。然后,此脚本在该OU中查找超过60天的任何内容并删除AD帐户,并删除其他服务器上的某些关联文件夹。
它在运行时往往会慢慢运行并最大限度地释放内存。任何人都可以看到这种情况会发生什么原因吗?也许这不是最有效的方法吗?
提前感谢您的帮助!
Import-Module ActiveDirectory
$users = Get-ADUser -SearchBase "ou=Terminated,dc=domain,dc=com" -Properties "company", "description" -Filter * | where {$_.description -like "Terminated*" }
$Date = Get-Date
$Today = $Date.DayOfYear
$CSVDate = Get-Date -Format yyyy-MM-dd
$TermList = @()
foreach($user in $users)
{
$DN = $user.DistinguishedName
$Homefolder = ("\\v12-dfs-home\homefolders$\" + $user.samaccountname)
$Profile = ("\\v12-dfs-pro\XA65Profiles\" + $user.samaccountname)
$Desktop = ("\\v12-dfs-home\desktops\" + $user.samaccountname)
$Term = $user.company
$Result = ($Today - $Term)
$altResult = ($Today + 365) - $Term
If ($Result -gt 60)
{
$TermList += $user | %{Get-ADUser $_.DistinguishedName -properties displayName, employeeID, samaccountname, title, Description | select displayName, employeeID, samaccountname, title, Description}
Remove-ADUser -Identity $DN -Confirm:$false
Remove-Item $Homefolder -Force -Recurse -ErrorAction SilentlyContinue -Confirm:$false
Remove-Item $Profile -Force -Recurse -ErrorAction SilentlyContinue -Confirm:$false
Remove-Item $Desktop -Force -Recurse -ErrorAction SilentlyContinue -Confirm:$false
}
ElseIf ($Result -lt 0)
{
If ($altResult -gt 60)
{
$TermList += $user | %{Get-ADUser $_.DistinguishedName -properties displayName, employeeID, samaccountname, title, Description | select displayName, employeeID, samaccountname, title, Description}
Remove-ADUser -Identity $DN -Confirm:$false
Remove-Item $Homefolder -Force -Recurse -ErrorAction SilentlyContinue -Confirm:$false
Remove-Item $Profile -Force -Recurse -ErrorAction SilentlyContinue -Confirm:$false
Remove-Item $Desktop -Force -Recurse -ErrorAction SilentlyContinue -Confirm:$false
}
}
}
# Output CSV of accounts being deleted
$TermList | Export-Csv c:\Results\Deleted_$CSVDate.csv -NoType
答案 0 :(得分:0)
活动目录检索大数据可能会有问题。在尝试:
时要小心我可能错了,但这些是代码中唯一可能会减慢脚本速度并消耗RAM的两件事。
以下是一些最佳做法
理想的PowerShell脚本: