[英]How can I improve this AD update powershell script?
I wrote this Powershell script years ago for updating AD from a CSV file. 多年前,我编写了此Powershell脚本,用于从CSV文件更新AD。 I'd like to try to optimize to run quicker and without errors... but I'm not sure where to start / what could improve it.
我想尝试优化以更快地运行并且没有错误...但是我不确定从哪里开始/有什么可以改善它的。 Any suggestions are welcome.
欢迎任何建议。
$users = Import-Csv -Path C:\Scripts\Employees.csv
foreach ($user in $users) {
Get-ADUser -ErrorAction SilentlyContinue -Filter "EmployeeID -eq '$($user.EmployeeID)'" -Properties * -SearchBase "ou=Logins,dc=domain,dc=com" |
Set-ADUser -EmployeeNumber $($user."EmployeeNumber") -Department $($user."Department") -Title $($user."Title") -Office $($user."office") -StreetAddress $($user."Address") -City $($user."City") -State $($user."State") -PostalCode $($user."PostalCode") -Company $($user."Company") -OfficePhone $($user."telephone") -Mobile $($user."cell") -Fax $($user."Fax")
Get-ADUser -ErrorAction SilentlyContinue -Filter "EmployeeID -eq '$($user.EmployeeID)'" -Properties * -SearchBase "ou=Logins,dc=domain,dc=com" |
Set-ADUser -Replace @{ExtensionAttribute1=($user.custom1); ExtensionAttribute2=($user.custom2); ExtensionAttribute3=($user.custom3); ExtensionAttribute4=($user.custom4)}
}
Just my 2cents worth on the topic... many can and will have different takes on it. 就这个话题而言,只有我的2美分价值...许多人可以而且会对此有不同的看法。 So, always good to wait for other opinions.
因此,总是很高兴等待其他意见。
As for your baseline query... 至于您的基准查询...
I'd like to try to optimize to run quicker and without errors
我想尝试优化以更快地运行并且没有错误
This is a very subjective thing. 这是非常主观的事情。
Why do you feel it is not optimal? 为什么您觉得它不是最佳的? Why do you feel it's not quick enough?
您为什么觉得速度不够快? What metrics did you use or what did you compare it against?
您使用了哪些指标或将其与哪些指标进行了比较?
So, you need to decide what the above means to you and then be able to take a route that works. 因此,您需要确定以上内容对您意味着什么,然后才能采用可行的路线。 Because depending on what you are doing, and how, it will be fluid.
因为这取决于您的操作以及操作方式,所以它会很流畅。
References: 参考文献:
How to speed up PowerShell operations on AD objects 如何加快对AD对象的PowerShell操作
Weekend Scripter: PowerShell Speed Improvement Techniques 周末脚本:PowerShell速度改进技术
Slow Code: Top 5 Ways to Make Your PowerShell Scripts Run Faster 慢速代码:使PowerShell脚本运行更快的5种方法
Why cant PowerShell run loops fast ? 为什么PowerShell无法快速运行循环?
All that being said... 这么说...
I cannot come up with a reason, why you'd use Get-ADUser / Set-ADUser twice for the same getter data only your setter is different. 我不能提出一个原因,为什么您要为相同的getter数据使用两次Get-ADUser / Set-ADUser,只是您的setter不同。 Do the get and set once.
进行获取并设置一次。 For large recordsets / data, loops are just slow.
对于大型记录集/数据,循环速度很慢。 Remember, ADDS imposes limits on the number of records it will return at a time.
请记住,ADDS对一次将返回的记录数施加了限制。 If you are going after more than the default, you need to change that AD setting / request.
如果您追求的不是默认值,则需要更改该广告设置/请求。 If it is a large recordset, it's better to chunk it and break than into parallell operations on a smaller recordset.
如果它是一个大的记录集,最好对它进行分块和拆分,而不要对一个较小的记录集进行并行操作。 Take a look at parraller processing / workflows / runspaces for how to do that.
看一下Parraller处理/工作流程/运行空间中的操作方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.