I am writing a PowerShell script to automate putting users on a leave of absence. As part of this process, we remove the user from their DLs if it is a long absence to stop them from coming back to thousands of emails.
I am planning to saving the list of members to the notes field under the telephone tab, so I can easily restore it manually when they get back (one automation step at a time)
I have no problem pulling a list of of distribution groups with:
Get-ADPrincipalGroupMembership $user | Where-Object GroupCategory -eq Distribution | select name
The part I run into trouble with is when I try to save it to the notes field with:
Set-ADUser $user -replace @{info=(Get-ADPrincipalGroupMembership $user | Where-Object GroupCategory -eq Distribution |select name)}
I get the error:
Set-ADUser : Invalid type 'System.Management.Automation.PSObject'.
I have tried massaging the data into shape with -split and -join, but either I'm not doing it right or these are not the right commands. ConvertTo-CSV "works", but adds a lot of extra cruft.
How can I transform this data so that the telephone tab will accept it? My prefered format is something I can use to copy/paste the groups back in afterwords ie. group1;group2;group3 etc. (I will also take comments that the way I am approaching this is all wrong and that method xyz is much better.)
Best Answer
Well, I don't mean to state the obvious, but you are trying to paste in a Powershell object, where the
info
attribute of an AD user will only accept strings. You need to convert the list of group memberships into a string first, and add the logic yourself for putting your delimiter of choice in between each group name, be it a comma, semicolon or newline character.