I am trying to write a powershell script that will take users from our oracle DB export (CSV) and use that to either update info in Active Directory or create new accounts (with the Quest AD cmdlets, set-qaduser). The script I have is working, however it will not finish the foreach loop because it is running out of memory. The CSV has about 1,300 lines to loop through and the box has 12GB of ram.
I think there is an issue with my foreach loop and just processing it the most efficient way, so that is where I am looking for help. Script is below:
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
}