Below is a powershell script that I use to put information from a comma separated text file (called ISO.txt), into particular fields in a users Active Directory account. The data is dependent on the user having an email address as the key identifier, then the remaining two fields are populated into two other AD attributes for that specific user.
The issue that I have, is that this script (which has new entries on each line, beginning with the $mail field) will take the info from a line with a blank $mail field, and overwrite the previous lines $ID and $ISO data, in Active Directory. So basically you have entry A, with a complete $mail $ID $ISO entry, being overwritten by the next entry which has no $mail field entry, but has entries for $ID and $ISO. So now entry A has the $ID and $ISO data for entry B.
How do I tell this powershell script to completely ignore any line entries that have an empty $mail field?
You can use Where-Object to filter those objects out. In this case, I’m using -match ‘\S’, which means it must contain at least one non-whitespace character. (empty strings, null, and whitespace-only values would be filtered out):
Close: the % sign is an alias for ForEach-Object (when it’s used as a command), so you don’t need both. Generally accepted best practice is to use full cmdlet names in scripts, rather than aliases like % .
Also, is there any way that I can have powershell give me feedback on what entry is throwing a particular error? Here is what I get in Powershell ISE when I run the script.
This message is all in red, and it shows up multiple times, but I don’t know which entries are throwing these errors.
Set-ADUser : Cannot validate argument on parameter ‘Identity’. The argument is null. Supply a non-null argument and try the c
ommand again.
At E:\ID_Drops\card_import_delta_script.ps1:12 char:11