Modify Distribution Group Members (Scripting)

HudRah
HudRah used Ask the Experts™
on
Hi, I am attempting to script the code presented in article  RE: 3669 "Modify Distribution Group Members" to read in a file with DL names and then upgrade them populating the managed by field. I attempted to do this but get an error on the following line, if ran as part of script:
>>>
foreach($g in $groups) { $user = $g.user.tostring().substring($g.user.tostring().indexof("\") + 1);

ERROR: You cannot call a method on a null-valued expression.
At C:\scripts\NEW\DL\NEW\DLmodification-FORLIST-u_test.ps1:32 char:50
+ foreach($g in $groups) { $user = $g.user.tostring <<<< ().substring($g.user.tostring().indexof("\") + 1);
    + CategoryInfo          : InvalidOperation: (tostring:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull


>>>
When I run it manually, it runs fine. Am I missing something? Any Advice would be greatly appreciated.

Attached is my attempted script.

Thanks,
SCRIPT.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
You are writing    $groups = ... | format-table
and then using   $groups   as if it still were a collection of full-fleshed objects.   format-table   creates an array of String objects, dropping any further properties the source objects had. So, when you execute  $g.user, the  user  property is not known, and returns $null.

Just remove the  | format-table part.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial