PowerShell script to set attribute (and skip) but not error out if one is missing from the CSV

I need to Set-ADGroup attributes like "ManagedBy" etc.. but my CSV looks something like this (notice the missing ManagedBy for the ROW: Group04,FALSE,  )

Identity,HiddenFromAddressListsEnabled,ManagedBy
Group01,FALSE,John
Group02,FALSE,Frank
Group03,TRUE,John
Group04,FALSE,
Group05,TRUE,John

Open in new window


Effectively, I am looking for PowerShell that will assign all attributes listed in the CSV but skip over the one attribute that is blank.

Thank you so much for your time in advance.
-K
LVL 9
K BAsked:
Who is Participating?

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

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

Will SzymkowskiSenior Solution ArchitectCommented:
You are using the wrong cmdlet Set-ADUser is not used for setting the -HiddenFromAddressListsEnabled. You need to use Set-DistributionGroup

See below script.
$Data = import-csv "c:\test.csv"
ForEach ($Group in $Data)
     {
        Set-DistributionGroup -Identity $Group.Identity -HiddenFromAddressListsEnabled $Group.HiddenFromAddressListsEnabled -ManagedBy $Group.managedby
        
 }

Open in new window


Even if the values are black in the CSV it will not error and it will continue through the list.

Tested and works.

Will.

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
K BAuthor Commented:
Will,

Thank you for your reply!

In this scenario, I do not have that cmdlet as I am migrating from a hosted exchange (so I am back-filling AD for DirSync to match with the upcoming migration batch)

So let's just take HiddenFromAddressListsEnabled out of the picture.  This is the script without HiddenFromAddressListsEnabled (using Set-ADGroup)

$Data = import-csv "C:\scripts\groups.csv"
ForEach ($Group in $Data)
     {
        Set-ADGroup -Identity $Group.Identity -ManagedBy $Group.managedby
        
 }

Open in new window


This is the error that I get:

Set-ADGroup : Identity info provided in the extended attribute: 'ManagedBy' could not be resolved. Reason: 'Cannot find an object with identity: '' under: 'DC=CONTOSO,DC=COM'.'.

Open in new window


It sets any ManagedBy attributes that have values however.
Will SzymkowskiSenior Solution ArchitectCommented:
In the Set-ADGroup cmdlet try using -errorAction SilentContinue That should suppress any error messages that you encounter.

Will.
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

K BAuthor Commented:
Unfortunately I get the same error.
Will SzymkowskiSenior Solution ArchitectCommented:
So you say that you get an error message, but it continues anyway? So do you just want something that's cleaner? You could do like a try/catch statement and if the value is not present then write-out "value does not exist" or something like that.

If all other groups are modified why do you care? Is this just an ascetics thing?

Will.
K BAuthor Commented:
You know what. You are right!! Yes it was.
Will SzymkowskiSenior Solution ArchitectCommented:
Sorry I missed that? What was I right about?

Will.
K BAuthor Commented:
It was just aesthetics. it works just fine for what I need it to do. So thank you!
Will SzymkowskiSenior Solution ArchitectCommented:
Kewl, glad to help!

Will.
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
Powershell

From novice to tech pro — start learning today.