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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroEE Solution Guide - CEO Faru Bonon ITCommented:
Probably somethig like this: (this is not tested)

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

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

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

    Set-ADUser $_.Username -replace $objects

Open in new window

Try it like this:
$attributeList = 'msExchExtensionAttribute24', 'msExchExtensionAttribute25', 'msExchExtensionAttribute26', 'msExchExtensionAttribute27'
Import-Csv -Path "$env:SystemDrive\Test\temp.csv" | ForEach-Object {
	$replace = @{}
	ForEach ($attribute in $attributeList) {
		If ($_.$attribute) {
			$replace[$attribute] = $_.$attribute
	Set-ADUser -Identlity $_.Username -Replace $replace

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MilesLoganAuthor Commented:
Hi oBdA .. that worked exactly as expected and thank you for making the script easy to understand .
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.