Need help filtering out lines with blank entries

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?

Thank you!

Import-module ActiveDirectory
Import-CSV E:\ID_Drops\Java\ISO.out | % {
$mail = $.mail
$ID = $
.employeeID
$ISO = $_.employeeNumber
$user = Get-ADUser -Filter {mail -eq $mail}
Set-ADUser $User.samaccountname -employeeID $ID -employeeNumber $ISO
}

Can you, please, give us one or two lines from ISO file?

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):

Import-CSV E:\ID_Drops\Java\ISO.out |
Where-Object { $_.mail -match '\S' } |
ForEach-Object {
    # etc
}

Dave,

Thank you, very much, for your reply. I have to admit that I’m a complete powershell n00b, and am not entirely familiar with the syntax.

Is this how my new code should look? Thank you!!

Import-module ActiveDirectory
Import-CSV E:\ID_Drops\Java\ISO.out |
Where-Object { $.mail -match ‘\S’ } |
ForEach-Object % {
$mail = $
.mail
$ID = $.employeeID
$ISO = $
.employeeNumber
$user = Get-ADUser -Filter {mail -eq $mail}
Set-ADUser $User.samaccountname -employeeID $ID -employeeNumber $ISO
}

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 % .

ForEach-Object % {

# Becomes:

ForEach-Object {

# Or:

% {

Awesome! I’ll run the script and report back. Thank you!!!

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

  • Set-ADUser <<<< $User.samaccountname -employeeID $ID -employeeNumber $ISO
    • CategoryInfo : InvalidData: (:slight_smile: [Set-ADUser], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser