Last foreach loop in PowerShell fails but the previous loop works fine -


this script meant migrate 1 domain another. i'm looking of ad security groups, pull members of each group , create text file name of group , members of group.

i want create new set of text files include information in users mailnickname field each group, called nid (new id). use mailnickname store nid new domain can push nid proper groups in new domain.

in below script works except last foreach loop, not sure why; looks correct , i've searched internet answers no reason why shouldn't work.

i don't error $b variable ends being same value $igroup variable. new text file nid never created.

$iscript = "getadgroups" $strpath = "c:\temp\$iscript" $strfile = "$strpath\$iscript.txt" if(!(test-path $strpath)){new-item -itemtype directory -path $strpath}  $ou1 = "ou=users,ou=oc,dc=domain,dc=com" $ou2 = "ou=users,ou=amc,dc=domain,dc=com" $ou3 = "ou=users,ou=fi,dc=domain,dc=com"  get-adgroup -filter 'groupcategory -eq "security"' -searchbase $ou1 | ft name -hidetableheaders | out-file "$strfile" get-adgroup -filter 'groupcategory -eq "security"' -searchbase $ou2 | ft name -hidetableheaders | out-file "$strfile" -append get-adgroup -filter 'groupcategory -eq "security"' -searchbase $ou3 | ft name -hidetableheaders | out-file "$strfile" -append  (gc $strfile) | ? {$_.trim() -ne ""} | set-content $strfile $groups = (gc $strfile) $groups | foreach{$_.trimend()} | set-content $strfile $groups = (gc $strfile)  foreach($member in $groups){     foreach($a in $member){         if(test-path "$strpath\$a.members.txt"){remove-item -path "$strpath\$a.members.txt" -force}         write-host $imember         $imember = get-adgroupmember "$a" | select-object samaccountname         $imember | ft -hidetableheaders | out-file "$strpath\$a.members.txt" -append         (gc "$strpath\$a.members.txt") | ? {$_.trim() -ne ""} | set-content "$strpath\$a.members.txt"         (gc "$strpath\$a.members.txt") | foreach{$_.trimend()} | set-content "$strpath\$a.members.txt"         gci "$strpath\$a.members.txt" | {$_.length -lt 1} | remove-item         }     }  $groupname = (gci "$strpath\*.members.txt" -name) foreach($igroup in $groupname){     foreach($b in $igroup){         if(test-path "$strpath\$b.nid.txt"){remove-item -path "$strpath\$b.nid.txt" -force}         write-host $b         get-aduser -filter {samaccountname -eq "$b"} -properties mailnickname | select-object mailnickname | set-content "$strpath\$b.nid.txt"         }     } 

  1. your foreach($b in $igroup) loops single item, equal. demo:

    $groupname = "group1.members.txt","group2.members.txt","group3.members.txt" foreach($igroup in $groupname){     foreach($b in $igroup){         write-host $b      } } group1.members.txt group2.members.txt group3.members.txt 

    sure don't want read file? ex.

    foreach($group in (gci "$strpath\*.members.txt")){     write-host $group.name     foreach($user in (get-content -path $group.fullname)){         write-host $user      } } 
  2. when exporting , importing objects, want use format-*-cmdlets meant displaying data in console , breaks objects. because of this, end having cleanup text-file every time. highly recommend csv-files using export-csv , import-csv or custom textfiles (if so, use select-object -expandproperty name show groupname).

  3. you wasting time on reading, modifying , removing files. rewrite this:

    $iscript = "getadgroups" $strpath = "c:\temp\$iscript" $strfile = "$strpath\$iscript.txt" #-force create missing parents-folders if(!(test-path $strpath)){new-item -itemtype directory -path $strpath -force}  $ous = "ou=users,ou=oc,dc=domain,dc=com", "ou=users,ou=amc,dc=domain,dc=com", "ou=users,ou=fi,dc=domain,dc=com" $groups = $ous | foreach-object { get-adgroup -filter 'groupcategory -eq "security"' -searchbase $_ } #save groupnames file if need them $groups | select-object -expandproperty name | set-content -path $strfile  foreach($group in $groups){     write-host $group     $members = @(get-adgroupmember -identity $group)      #if group contains members     if($members.count -gt 0) {         #create list of members         $members | select-object -expandproperty samaccountname | set-content -path "$strpath\$group.members.txt"          #create list new ids (is other list necessary?)         $members | get-aduser -properties mailnickname | select-object -expandproperty mailnickname | set-content -path "$strpath\$group.nid.txt"     } } 

    and preferably merge members , nid-list single csv-file username , mailnickname linked using single row. ex:

    #if group contains members if($members.count -gt 0) {     #create csv-list of members current username (samaccountname) , new id (mailnickname)     $members | get-aduser -properties mailnickname | select-object -property samaccountname, mailnickname | export-csv -path "$strpath\$group.csv" -notypeinformation } 

    or take 1 step further , have 1 csv-file group-memberships.

    $iscript = "getadgroups" $strpath = "c:\temp\$iscript" $strfile = "$strpath\$iscript.txt"  $ous = "ou=users,ou=oc,dc=domain,dc=com", "ou=users,ou=amc,dc=domain,dc=com", "ou=users,ou=fi,dc=domain,dc=com" $groups = $ous | foreach-object { get-adgroup -filter 'groupcategory -eq "security"' -searchbase $_ } #save groupnames file if need them (if you're migrating empty groups) $groups | select-object -expandproperty name | set-content -path $strfile  $groups | foreach-object {     $group = $_     write-host $group     get-adgroupmember -identity $group |     #getting aduser object missing property     get-aduser -properties mailnickname |     select-object -property @{n="group";e={$group}}, samaccountname, mailnickname } | export-csv -path "$strpath\groupmembers.csv" -notypeinformation 

    your csv this, can imported, grouped etc. later import-csv when adding members new groups:

    group,samaccountname,mailnickname group1,user1,mailnickforuser1 group2,user1,mailnickforuser1 group2,user2,mailnickforuser2 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -