简体   繁体   English

如何改善此AD更新Powershell脚本?

[英]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. 这是非常主观的事情。

  1. What do you mean by optimize? 您所说的优化是什么意思?
  2. What do you mean by make it run quicker? 您是什么意思使其更快运行?

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种方法

Coding for speed 编码速度

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.

相关问题 共享文件区 - 如何改进我的 PowerShell 脚本? - Shared file area - how can I improve my PowerShell script? 如何使用PowerShell和CSV导入更新AD用户? - How can I update AD users using PowerShell and CSV import? 如何在 php 中调用 PowerShell 脚本以从 AD 中的两个域控制器获取上次登录数据 - How can i call PowerShell script in php to get the lastlogin data from two Domain Controllers in AD 如何在我的 Azure Z86408593C34AF726FDD9 应用程序中的 PowerShell 脚本中获取 Azure AD 用户列表 - How can I get a list of Azure AD Users in my PowerShell script within my Azure Function App? 使用Popen在Python中执行Powershell脚本,如何获取Powershell脚本的输出并将其更新到网页? - Use Popen to execute a Powershell script in Python, how can I get the Powershell script's output and update it to web page? 在Powershell中,如何在gridview中垂直列出AD计算机属性? - In powershell how can I list AD computer properties vertically in gridview? 如何通过PowerShell提示输入选项来创建AD用户? - How can I create an AD user via PowerShell prompting for options? 如何将Powershell中的AD信息拆分为excel文档? - How can I split AD information in Powershell into a excel document? 如何使用PowerShell在AD中移动用户? - How can I use PowerShell to move a user in AD? 如何使用 Powershell 提取 AD 中经理的多个属性? - How can I extract multiple attributes of manager in AD using Powershell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM