Link to home
Start Free TrialLog in
Avatar of MilesLogan
MilesLoganFlag for United States of America

asked on

Script failing if not all columns have data to be updated.

I was helped by an Expert on the script below .. I didn't plan on it but my CSV has blanks on some of the attributes... can someone
assist modifying this so if there is a blank in one of the fields?

so lets say a user only needs 3 of the 4 values below updated , the script below does not work .. it only works if all four fields have values to update.

Import-CSV -Path "$env:SystemDrive\Test\temp.csv" | % {
  Set-ADUser $_.Username -replace @{'msExchExtensionAttribute24' = $_.msExchExtensionAttribute24; 'msExchExtensionAttribute25' = $_.msExchExtensionAttribute25; 'msExchExtensionAttribute26' = $_.msExchExtensionAttribute26; 'msExchExtensionAttribute27' = $_.msExchExtensionAttribute27; }
}

Open in new window

Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

Probably somethig like this: (this is not tested)

Import-CSV -Path "$env:SystemDrive\Test\temp.csv" | % {

    $ShouldAdd=@()
    for($i=24; $i-lt 27; $i++){
       $"isNull$i"=  if(!($_."msExchExtensionAttribute$i")){$true}else{$false}
       $ShouldAdd+=$"isNull$i"
    }
    $objects=@{}

    foreach($Sa in $ShouldAdd){
        if($sa){
            $objects+= "'msExchExtensionAttribute$sa' = $_.msExchExtensionAttribute$sa; "
        }
    }

    Set-ADUser $_.Username -replace $objects
    
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MilesLogan

ASKER

Hi oBdA .. that worked exactly as expected and thank you for making the script easy to understand .