arrays - Powershell Problems - Add-ADGroupMember variable not working -


so have wonderfully difficult problem i'm unable work through. have list of computernames i'm pulling sql txt. no worries there, import variable (string array assume?) , each computer name distinguished name (dn) of appropriate computer. reason regular names not matching in ad, when attempted add computers group regular hostname.

here's sample of code:

    $computernames = get-content  .\computernames.txt      foreach ($computername in $computernames)          {         get-adcomputer -ldapfilter "(cn=$computername)" -searchscope subtree | select-object -property distinguishedname | export-csv -notypeinformation -append dnnames.csv         }      $dnnames = get-content .\dnnames.csv     foreach ($dnname in $dnnames)         {         add-adgroupmember -identity <sam_groupname> -members $dnname -passthru         } 

right unable results $dnnames variable, parse value properly, ends this:

    add-adgroupmember : cannot find object identity: '"cn=<computername>,ou=workstations,dc=<dc>,dc=com"' under:      'dc=<dc>,dc=com'.     @ line:3 char:5     +     add-adgroupmember -identity <sam_groupname> -members $dnname -passt ...     +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~         + categoryinfo          : objectnotfound: ("cn=<computername>-...c=<dc>,dc=com":adprincipal) [add-adgroupmember], adidentitynotfoundexception         + fullyqualifiederrorid : setadgroupmember.validatemembersparameter,microsoft.activedirectory.management.commands.addadgroupmember 

so i'm not i'm doing wrong. i've tried setting variable $dnname manually string value , works charm. why heck get-content on csv (basically comma-seperated text string) problem?

any appreciated.

thanks!

i think problem coming using out-csv get-content instead of import-csv. csv content has quotes around distinguishednames, , think that's what's causing error directly.

the simplest change, suggest using -expandproperty out-file (and calling dnnames.txt).

$computernames = get-content .\computernames.txt foreach ($computername in $computernames)      {     get-adcomputer -ldapfilter "(cn=$computername)" -searchscope subtree | select-object -expandproperty distinguishedname | out-file -append dnnames.txt     }  $dnnames = get-content .\dnnames.txt foreach ($dnname in $dnnames)     {     add-adgroupmember -identity <sam_groupname> -members $dnname -passthru     } 

that stands chance of working haven't tried it. seem lot of code does, though; how avoiding ldapfilter, not writing file reading it, , switching using add-adprincipalgroupmembership can take multiple input pipeline?

that gives this:

$computers = get-content .\computernames.txt | foreach { get-adcomputer $_ } $dnnames = $computers | select-object -expandproperty distinguishedname $dnnames | add-adprincipalgroupmembership -memberof "yourgroup" 

which cut middle-men out, , squash down to:

gc computernames.txt | %{ get-adcomputer $_ } | select -expand distinguishedname | add-adprincipalgroupmembership -memberof "yourgroup" 

edit: shorter version:

gc computernames.txt | %{ (get-adcomputer $_).sid.value } | add-adprincipalgroupmembership -memberof "yourgroup" 

nb. "the regular names not matching in ad, when attempted add computers group regular hostname" - samaccountname computer defaults hostname $ after it, e.g. win005$, , add-adgroupmember can work version. somehow get-adcomputer works me finding hostname though says needs samaccountname.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -