【发布时间】:2015-01-16 00:14:44
【问题描述】:
我正在尝试编写一个 powershell 脚本,该脚本将从我们的 oracle DB 导出 (CSV) 中获取用户,并使用它来更新 Active Directory 中的信息或创建新帐户(使用 Quest AD cmdlet、set-qaduser)。我的脚本正在运行,但是它不会完成 foreach 循环,因为它内存不足。 CSV 有大约 1,300 行要循环,盒子有 12GB 的内存。
我认为我的 foreach 循环存在问题,只是以最有效的方式处理它,所以这就是我寻求帮助的地方。脚本如下:
Add-PSSnapin Quest.Activeroles.ADManagement
Import-Csv \\pathtocsv\importusers.csv | foreach {
[string]$upn=$_.FIRST_NAME[0]+$_.LAST_NAME+"."+$_.ASSOC+"@innout.corp"
#check to see if the AD account already exists, if not, create it
if (!(get-qaduser $upn))
{
#because there are some blank/null values for phone numbers we need to only put in the variable values that have data, otherwise the script will bomb out
if($_.HOME){
$homephone=$_.home}
else{
$homephone=" "}
if($_.CELL){
$cellphone=$_.cell}
else{
$cellphone=" "}
$mgr=Get-QADUser -IncludedProperties employeeid -oa @{employeeid=$_.mgr}
#Object attribute hashtable, ADattribute and the value you want to put.
$oa=@{
Department=$_.ctr_name;
division=$_.division;
employeeid=$_.assoc;
ExtensionAttribute10=$_.mgr;
ExtensionAttribute11=$_.region_name;
ExtensionAttribute12=$_.hire_date;
ExtensionAttribute13=$_.dob;
ExtensionAttribute14=$_.region;
ExtensionAttribute15=$_.mgr_name;
DepartmentNumber=$_.ctr
}
New-QADUser -ParentContainer "OU=StoreManagers,OU=Stores,DC=contoso,DC=com" -Name $_.full_name -title $_.title -DisplayName $_.full_name -firstname $_.first_name -lastname $_.last_name -upn $upn -homephone $homephone -mobilephone $cellphone -manager $mgr -telephonenumber $_.work -ObjectAttributes $oa
}
#this else statement is if the AD account already exists, then just come here and update the account.
else
{
if($_.HOME){
$homephone=$_.home}
else{
$homephone=" "}
if($_.CELL){
$cellphone=$_.cell}
else{
$cellphone=" "}
$mgr=Get-QADUser -IncludedProperties employeeid -oa @{employeeid=$_.mgr}
$oa=@{
Department=$_.ctr_name;
division=$_.division;
employeeid=$_.assoc;
ExtensionAttribute10=$_.mgr;
ExtensionAttribute11=$_.region_name;
ExtensionAttribute12=$_.hire_date;
ExtensionAttribute13=$_.dob;
ExtensionAttribute14=$_.region;
ExtensionAttribute15=$_.mgr_name;
DepartmentNumber=$_.ctr
}
set-qaduser -identity $upn -DisplayName $_.full_name -firstname $_.first_name -lastname $_.last_name -title $_.title -homephone $homephone -mobilephone $cellphone -manager $mgr -telephonenumber $_.work -ObjectAttributes $oa
}
}
#This section will disable/move/delete managers that have left the company or stepped down to a non-managment role.
$deletedusers=Import-Csv \\pathtocsv\importusers.csv
foreach ($deleteduser in $deletedusers) {
[string]$samdelete=$deleteduser.FIRST_NAME[0]+$deleteduser.LAST_NAME+"."+$deleteduser.ASSOC
Disable-QADUser $samdelete
Move-QADObject $samdelete -NewParentContainer "OU=ToBeDeleted,OU=StoreManagers,OU=Stores,DC=contoso,DC=com"
set-qaduser $samdelete
}
#This section sets all the DM Division numbers
$dmusers=Import-Csv \\pathtocsv\importusers.csv
foreach ($dmuser in $dmusers) {
$oa=@{
division=$dmuser.division
}
Get-QADUser -oa @{employeeid=$dmuser.assoc} | set-qaduser -oa $oa
}
【问题讨论】:
-
您是否尝试将 -DontUseDefaultIncludedProperties 添加到 Get-QADUser 调用中?
-
谢谢米奇。我确实尝试使用 -DontUseDefaultIncludedProperties,然后只包含了我需要设置的所有属性,但它仍在增长。我认为 Quest cmdlet 中可能存在内存泄漏。请参阅下面的评论。
标签: powershell memory-management foreach out-of-memory cmdlets