Monday, March 26, 2018

Update Group Membership – PowerShell Script


If you have multiple domains or performing a user or group migration, you may need to manually update (depend on your scenario) the source or target group membership.  This script can be used to update group membership based on source user’s group membership.  The input for this script the user name (sAMAccountName) and it assumes that the source and target sAMAccountName are the same. 
Input file (Users.csv) Format:















Script validates users in the source domain and collect “memberof” details and then add the target user (migrated user) to the same group. At the end of the operation, the source user and the target user (migrated user) will be part of same security group in the source domain. 
You can see some other “Update Group Membership” script here - http://portal.sivarajan.com/2014/01/update-group-membershippowershell-script.html
Script:
#
# Update Group Membership
# Santhosh Sivarajan (Santhosh@Sivarajan.Com)
#
Clear
Import-Module ActiveDirectory
$userN = ""
$GroupDetails = ""
$Group = ""
$GroupsDN = ""
$uValidation = ""
$tagetDomain = "labanddemo.com"
$Cdate = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")


        $SGBeforeUpdateFile = New-Item -type file -force "C:\Temp\Groups_Before_$Cdate.csv"
        $SGAfterUpdateFile = New-Item -type file -force "C:\Temp\Groups_After_$Cdate.csv"
        Import-CSV "C:\Temp\Users.csv" | % {
        $userN = $_.userName
        $sourceDomain = $_.Domain

               
                   $uValidation = Get-ADUser -filter {sAMAccountName -eq $userN} -Server $tagetDomain
                  
                       If($uValidation -eq $Null)
                        {
                           Write-Host "User $userN Doesn't Exist in $tagetDomain Domain"
                           $errorFile = New-Item -type file -force "C:\Temp\Error_$Cdate.csv"
                           "User $userN Doesn't Exist in $tagetDomain Domain"| Out-File $errorFile -encoding ASCII -append
                        }
                        Else
                        {
                           $userN | Out-File $SGBeforeUpdateFile -encoding ASCII -append
                           $GroupDetails = get-aduser -Server $sourceDomain -identity $userN -Properties memberof
                           $GroupsDN = $GroupDetails.memberof
                           $GroupsDN | Out-File $SGBeforeUpdateFile -encoding ASCII -append
                           foreach ($Group in $GroupsDN)
                              {
                               $MigrateduserN = Get-ADUser $userN -Server $tagetDomain -Properties DistinguishedName
                               Write-host "Adding User -> $MigrateduserN"
                               Write-host "To Group -> $Group"
                               Add-ADGroupmember -Server $sourceDomain -Identity $Group -Members $MigrateduserN
                               $members = Get-ADGroupmember -Server $sourceDomain -Identity $Group
                               $GroupName = Get-ADGroup -Server $sourceDomain $Group
                               $GroupName.Name | Out-File $SGAfterUpdateFile -encoding ASCII -append
                               $members.distinguishedName | Out-File $SGAfterUpdateFile -encoding ASCII -append
                               Write-host "....Done!" -ForegroundColor Green
                               Write-host ""
                               }
                        }

        }

Download:
You can also download the script from the following locations:

  1. OneDrive
  2. TechNet Gallery 


35 comments:

This comment has been removed by the author.

feel regret I did not know this before I read your article.
superfighters
y8

Nice post. I learn something totally new and challenging on websites I stumbleupon every day. It's always useful to read through articles from other authors and practice something from other sites.
check this out FMWhatsApp Apk

hi, your post is very helpful for me. Finally, I found exactly what i want. If need information regarding printers then you can visit our site Xerox Printer ondersteuning for help.

hi, Your post is very helpful for me, If you want to know more about antivirus then you can visit our site Canon Printer contacteren for help.


hi, Your post is very helpful for me,finally i got exactly what I want. If you want to know more about antivirus then you can visit our site Bitdefender belgie help.


Commenting as lilyloo180@gmail.com
Comment as:

I thank you for the information trực tiếp bóng đá! I was looking for and could not find. You helped me!

Thanks for sharing it is important for me. I also searched for that from here. Visit our site Contacteren Epson Printer

The first obvious benefit is the flexibility of the deal. There's practically no mission you can't purchase at our site. We provide documents, research papers, dissertations, theses, laboratory reports, personal announcements, data tasks, scheduling assignment help services, PowerPoint presentations, and also each other endeavor you may possibly require.

The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year

Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project

Domains for CSE
technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies

during planning of software projects and improvement programmes in Final Year Projects for CSE.



Software management seeks for decision support to identify technologies like JavaScript that meet best the goals and characteristics of a software

project or improvement programme. JavaScript Training in

Chennai
Accessible experiences and repositories that effectively guide that technology selection are still lacking.



Aim of technology domain analysis is to describe the class of context situations (e.g., kinds of JavaScript software projects) in which a software

engineering technology JavaScript Training in Chennai can be applied successfully



The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing,

and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

Being an understudy you should be very much familiar that tasks are the crucial markers of an understudy's exhibition and potential, executed by instructive coach's the world over. So it is truly sort of a required endeavor that must be paid attention to by all understudies. Focusing on these online scholarly composing Best Assignment administrations doesn't in any capacity implies that you are trifling with your work or not focusing on it, it really is a more astute strategy for beating your shortcomings. The profoundly paced world we are a piece of, requires such an alarm minds as there is a bad situation for shortcomings here.

a number of people who use Canon printers in their day by day work may experience printer error that is "canon printer is in error state". Likewise, they frequently look for help to fix this frustrating issue. To handle down these kinds of issues, you need a particular PC just as printer information.
my canon printer always in error state

I learned absolutely everything about my question when I read this post, thanks to the author for the detailed description. I wrote my review on the myperfectpaper you can go in and read. Thank you very much for your attention and your time.


Very wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this

on my followers. Many thanks!

bulletintech
whizzherald
talesbuzz
alternativestips
techwithgeeks
shindigweb
dns server not responding

Really nice post ,i have learn something new from your article it’s very innovative and creative which is so wonderful and if you want to look these site visit their site
error 1723 quickbook visit this sites for all information about it

Thanks for sharing it was great keep updating us. Also check Download GBWhatsApp APK

I am very impressed with your post, thanks fir sharing this. Would love to see more. COC MOD Apk

This was an excellent article. Thank you for sharing it.
WhatSender


I hope this post is beneficial for viewers. Many thanks for the shared this informative and interesting post with us.
vce-exam-simulator-pro

Thank you so much for sharing this. Would love to see more of these in the future. Keep up the good work! Keep sharing such awesome stuff.

Very awesome!!! When I seek for this I found this website at the top of all blogs in search engine.
data science training

avast secureline torrent
m3 raw drive recovery full crack
save wizard cracked
Thanks for this informative blog and forgiving us and the opportunity to share our views.

This script is so helpful for upload the data. Thanks a lot for sharing it. fmwhatsapp

Thanks for this informative article this the very very informative and helpful for me and once again thankyou.
from: Digitizing Services USA

Prime Property in Bandra East: Explore a diverse range of prime properties in Bandra East, including apartments, townhouses, and penthouses. Each property is thoughtfully designed to cater to different preferences and lifestyles.

I'm confident a lot of readers will find this piece helpful. Keep up the fantastic work! I'm forward to see more fascinating facts presented from your perspective in the future. I appreciate what you've done so much.
divorcio nueva jersey

Amazing, Your blogs are really good and informative. I got a lots of useful information in your blogs. The input for this script the user name (sAMAccountName) and it assumes that the source and target sAMAccountName are the same abogados de accidentes. Keeps sharing more useful information in your blogs..

Retaining your motorbike helmet is paramount for protection. This involves everyday cleansing, right garage, and adherence to producer tips. Regular inspections are critical, and it's encouraged to replace your helmet every 5 years or after accidents to make sure it delivers the utmost protection even as using on the road. More info here Helmet in Bangladesh

It's critical that you buckshot roulette keep your motorcycle helmet on for safety. This entails regular cleaning, appropriate storage, and following manufacturer recommendations.

charlottesville virginia personal injury lawyers
The PowerShell script for updating group membership is praised for its simplicity and effectiveness, making it a valuable tool for managing group memberships. It is praised for its precision and ease, making it a must-have for IT professionals dealing with Active Directory. The script's clear code and detailed comments make it user-friendly, even for those less experienced with scripting. The efficiency of the script saves time and reduces errors, making it a game-changer. The documentation provided makes implementation seamless, making it a powerful tool that simplifies complex tasks.

Post a Comment

Popular Posts

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More