Tuesday 1 September 2015

 ##SharePoint Copy Users between Site Colletions to same Groups

$SourceUrl = "http://";
$DestinationUrl = "http://";
 ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  #
 ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  #
cls
if ((Get-PSSnapin -Name Microsft.SharePoint.PowerShell -ErrorAction SilentlyContinue)) {
    Add-PSSnapin Microsoft.SharePoint.Powershell
    Write-Host "Loaded Powershell" -ForegroundColor gray
}

function CopyUsersBetweenSiteColetions( $SourceUrl, $DestinationUrl){
$SourceWeb = Get-SPWeb $SourceUrl -ErrorAction SilentlyContinue
    $DestWeb = Get-SPWeb $DestinationUrl -ErrorAction SilentlyContinue
    if (!$SourceWeb) {
        Write-Host "Not able to get site with url : $SourceUrl" -ForegroundColor Red
        return
    }
    if (!$DestWeb) {
        Write-Host "Not able to get site with url : $DestinationUrl" -ForegroundColor Red
        return
    }


    if ($DestWeb) {
    $DestWeb.AllowUnsafeUpdates = $true
        Write-Host " Getting site groups from the Site : " $SourceWeb.Title -ForegroundColor Yellow
        foreach($SourceGrp in $SourceWeb.SiteGroups) {
            $desGrp = $DestWeb.SiteGroups[$SourceGrp.Name]
                if ($desGrp) {
                    Write-Host "Comparing Group Members of the Group:- " $desGrp.Name -ForegroundColor Cyan
                    foreach($SourceMembr in $SourceGrp.Users) {
                        $DesMember = $DestWeb.EnsureUser($SourceMembr)
                            if ($DesMember) {
                           $exist = $desGrp.Users | Where{ $_.LoginName -eq $DesMember.LoginName  }

                            if(!$exist)
                            {
                             Write-Host " Adding User" $DesMember.LoginName "to the destination web" -ForegroundColor Gray
                                $desGrp.AddUser($DesMember)                          
                                }
                             
                            } else {
                                Write-Host $SourceMembr.Email " User Not existing in destination web" -ForegroundColor Yellow
                            }
                    }
                } else {
                    Write-Host "Group not exists in Destination site : " $SourceGrp.Name -ForegroundColor Gray
                }
        }
        $DestWeb.AllowUnsafeUpdates = $false
        $SourceWeb.Dispose()
        $DestWeb.Dispose()
        Write-Host "Opeartion complted." -ForegroundColor DarkYellow
    }
 }
 CopyUsersBetweenSiteColetions $SourceUrl $DestinationUrl

No comments:

Post a Comment