

Write-Host 'Done! Please check the Log File' -ForegroundColor Green $results | Export-Csv -NoTypeInformation -Path $file "Operation failed !!, Error :" + $error.Exception | Out-File $logfile -Append Write-Host 'Unexpected Error, check log for more details.' -ForegroundColor Red Check whether domain specified correctly in CSV." | Out-File $logfile -Append "$user : Operation failed !! Invalid Domain. Write-Host 'Error - Invalid domain!! Please verify domain details in input CSV.' -ForegroundColor Red Check whether user details specified correctly in CSV." | Out-File $logfile -Append "$user : User not found or does not exist.

Write-Host 'Error - Invalid User!! Pls verify user details in input CSV.' -ForegroundColor Red "$user : Attribute value Failed to be set! Error : " + $error.Exception | Out-File $logfile -Append Write-Host 'Error - User Attribute Value Failed to be set' -ForegroundColor Red "$user : Attribute value updated successfully!" | Out-File $logfile -Append Write-Host 'User Attribute Value updated successfully' -ForegroundColor Gray Set-ADUser -Identity $_ -Replace -eq $null)

Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "DC=mydomain,DC=contoso,DC=com" -SearchScope Subtree -Server "ServerName"

Select Name,SamAccountName,My-Attribute,distinguishedName $results += Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "DC=mydomain,DC=contoso,DC=com" -SearchScope Subtree -Server "ServerName" If(Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "DC=mydomain,DC=contoso,DC=com" -SearchScope Subtree -Server "ServerName")
#Quotindex of update#
#Search in specified domain OU and Update existing attributes # Loop through CSV and update users if they exist in CSV file $users = Import-Csv -Path D:\staff\users1.csv # Import CSV into variable = (Get-Date -Format yyyy-MM-dd-hhmm) Write-Host 'Starting to update AD Attributes.' -NoNewline -ForegroundColor Blue But yes it works except for the glitch I am seeking help for :-)
#Quotindex of code#
It's a beginner code and hence I was a bit hesitant in sharing the same. It, though probably not the most accurate. Line below it, as it would jump straight to the catch.ĮDIT: Just to clarify, $error is never actually $null, it is just an empty ArrayList at the start, so position 0 can't be accessed as it doesn't exist, me saying that at the start of a PowerShell session $error = $null was a way of explaining You're using a try.catch block, so even if there was no problem with your $error variable and an error occurred in the line Set-ADUser., you'd never get to the if condition one Now there's the question of what you're trying to achieve with this. Would run successfully because $error does not get reset and $error already exists. This also means that subsequent runs of the same script on ISE (or even on an open PowerShell window) Obviously this generates an error - which gets saved to position $error as it's the first error -, allowing subsequent users to be processed directly. You're attempting to set $error = $null before errors got generated, so the index does not exist. As errors get generated, the $error object is created as an ArrayList. When a session starts, it is not defined, so it is the same as saying $error = $null when you start. $error is a "system" variable that holds the errors generated for your current session. Must be non-negative and less than the size of the collection.Īt _Item(Int32 index, Object value)Īt CallSite.Target(Closure, CallSite, Object, Int32, Object )Īt 3(CallSite site, T0 arg0, T1 arg1, T2 arg2)Īt .DynamicInstruction`4.Run(InterpretedFrame frame)Īt .EnterTr圜atchFinallyInstruction.Run(InterpretedFrame frame) System.ArgumentOutOfRangeException: Index was out of range. Also, the next time the script executed again from the already open ISE, works absolutely fine. On every first instance, the script's catch block logs the below error for the first user in the input csv but then works absolutely fine for the remaining users. The script then logs the value of a particular attribute for users specified in the csv and then updates that attribute value with a new value specified in the input csv. The script queries the samaccountname from the input csv and then checks the domain specified in the csv, for which it should search the user. I have put together a powershell script which takes input from a csv file.
