locked
Removal of DL membership RRS feed

  • Question

  • Hi All,

    I have a script which removes all DL membership of mentioned users. Below is the script:

    #importing a csv file which consists of Userprincipalname

    Import-Module ActiveDirectory
    $source = $null

    while ($source -eq $null){
    $source = read-host "Enter source file name with filename & extension"
    if (-not(test-path $source)){
        Write-host "Invalid file path, re-enter."
        $source = $null
        }
    elseif ((get-item $source).psiscontainer){
        Write-host "Source must be a file, re-enter."
        $source = $null
        }
    }
    $x = Import-Csv $source | % {$_.userprincipalname}
    $samaccntnm = $x -replace ".{12}$"
    foreach($samname in $samaccntnm){
    $DGs = $samname | Get-ADUser -Properties memberof | Select-Object -ExpandProperty memberof | Get-ADGroup -Properties *| Where-Object{$_.mail -ne $null} | Select samaccountname
    foreach($dg in $DGs){
    $groupname = $dg.samaccountname

    if(Remove-ADGroupMember -Identity $groupname -Members $samname -PassThru){
        Write-Host "Removing $samname from $groupname "
        New-Object PSObject -Property @{
        "Name" = $samname 
        "Group" = $groupname} | Select-Object Name,Group | Export-Csv -Append "$($env:userprofile)\desktop\dlmemberremoval.csv"
        }
        else{ Write-Host "$samname will remain as memberof $groupname "}
    }
    }

    PROBLEM: Though I run this script, sometimes I receive "Get-ADGroup : A referral was returned from the server", any idea how this can be taken care?

    Many Thanks,

    Niri.


    Niranjan

    Thursday, July 20, 2017 3:47 PM

Answers

  • We cannot guide you testing.  You will have to contact a trained and certified tech to help you troubleshoot your network.

    Start by isolating the names that cause issues.  Validate that the names are correct.


    \_(ツ)_/

    Thursday, August 3, 2017 4:30 PM

All replies

  • Where did you find this script?

    Can you please format it correctly and post using the code posting tool.  It is very hard to read in most browsers.


    \_(ツ)_/


    • Edited by jrv Thursday, July 20, 2017 3:59 PM
    Thursday, July 20, 2017 3:55 PM
  • Hi Jrv,

    My apologies for that:

      #importing a csv file which consists of Userprincipalname
      Import-Module ActiveDirectory
      $source = $null

      while ($source -eq $null){
      $source = read-host "Enter source file name with filename & extension"
      if (-not(test-path $source)){
          Write-host "Invalid file path, re-enter."
          $source = $null
         }
      elseif ((get-item $source).psiscontainer){
          Write-host "Source must be a file, re-enter."
          $source = $null
         }
      }
      $x = Import-Csv $source | % {$_.userprincipalname}
      $samaccntnm = $x -replace ".{12}$"
      foreach($samname in $samaccntnm){
      $DGs = $samname | Get-ADUser -Properties memberof | Select-Object -ExpandProperty memberof | Get-ADGroup -Properties *| Where-Object{$_.mail -ne $null} | Select samaccountname
      foreach($dg in $DGs){
      $groupname = $dg.samaccountname

      if(Remove-ADGroupMember -Identity $groupname -Members $samname -PassThru){
           Write-Host "Removing $samname from $groupname "
           New-Object PSObject -Property @{
           "Name" = $samname 
           "Group" = $groupname} | Select-Object Name,Group | Export-Csv -Append "$($env:userprofile)\desktop\dlmemberremoval.csv"
          }
       else{ Write-Host "$samname will remain as memberof $groupname "}
    }
    }

    I guess this should be fine.

    Thanks,

    Niri


    Niranjan

    Monday, July 24, 2017 2:31 PM
  • Please post with the code posting tool provided.  The way you are posting causes lines to be broken incorrectly.  Don't make us fix your code.


    \_(ツ)_/

    Monday, July 24, 2017 4:51 PM
  • #importing a csv file which consists of Userprincipalname
      Import-Module ActiveDirectory
      $source = $null
    
      while ($source -eq $null){
      $source = read-host "Enter source file name with filename & extension"
      if (-not(test-path $source)){
          Write-host "Invalid file path, re-enter."
          $source = $null
         }
      elseif ((get-item $source).psiscontainer){
          Write-host "Source must be a file, re-enter."
          $source = $null
         }
      }
      $x = Import-Csv $source | % {$_.userprincipalname}
      $samaccntnm = $x -replace ".{12}$"
      foreach($samname in $samaccntnm){
      $DGs = $samname | Get-ADUser -Properties memberof | Select-Object -ExpandProperty memberof | Get-ADGroup -Properties *| Where-Object{$_.mail -ne $null} | Select samaccountname
      foreach($dg in $DGs){
      $groupname = $dg.samaccountname
    
      if(Remove-ADGroupMember -Identity $groupname -Members $samname -PassThru){
           Write-Host "Removing $samname from $groupname "
           New-Object PSObject -Property @{
           "Name" = $samname 
           "Group" = $groupname} | Select-Object Name,Group | Export-Csv -Append "$($env:userprofile)\desktop\dlmemberremoval.csv"
          }
       else{ Write-Host "$samname will remain as memberof $groupname "}
    }
    }
    My Apologies I missed using the code block insertion both the times.

    Niranjan

    Thursday, August 3, 2017 7:34 AM
  • Since there is only oe command that can cause this it is clear that you are asking for a name that does not exist.  There is no guarantee that UPN is based n SamAccountName.  You will have to get the names that failed and test them to troubleshoot this.

    Run in debugger to view variable contents.


    \_(ツ)_/

    Thursday, August 3, 2017 7:40 AM
  • Thanks for your reply JRV, can you hint us on the testing, like what testing needs to be done?

    Many Thanks,

    Niranjan


    Niranjan

    Thursday, August 3, 2017 11:22 AM
  • We cannot guide you testing.  You will have to contact a trained and certified tech to help you troubleshoot your network.

    Start by isolating the names that cause issues.  Validate that the names are correct.


    \_(ツ)_/

    Thursday, August 3, 2017 4:30 PM