Need help filtering out lines with blank entries

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Patrick Hefner Patrick Hefner 2 years, 1 month ago.

  • Author
    Posts
  • #19781
    Profile photo of Patrick Hefner
    Patrick Hefner
    Participant

    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
    }

  • #19784
    Profile photo of Ondrej Zilinec
    Ondrej Zilinec
    Participant

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

  • #19788
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You can use Where-Object to filter those objects out. In this case, I'm using [b]-match '\S'[/b], 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
    }
    
  • #19818
    Profile photo of Patrick Hefner
    Patrick Hefner
    Participant

    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
    }

  • #19820
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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:
    
    % {
    
  • #19821
    Profile photo of Patrick Hefner
    Patrick Hefner
    Participant

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

  • #19832
    Profile photo of Patrick Hefner
    Patrick Hefner
    Participant

    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: (:) [Set-ADUser], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser

You must be logged in to reply to this topic.