Wednesday, 22 January 2014

Powershell to restore a site collection without changing the existing security group members

Recently I had an requirement to update a wiki site collection from live, using PowerShell backup and restore, to a training environment.  The requirement further stated that the memberships of the existing training site should be kept.

Though this seems like an fairly easy requirement it turned out not to be something that could be done with Backup-SPSite and Restore-SPSite code, so I developed the code below, which works in a test environment I created.


Add-PSSnapin Microsoft.SharePoint.Powershell

$oldm = Get-SPuser -Web "http://win-l31vicrc3b5/" -Group "Team Site Members"
$oldo = Get-SPuser -Web "http://win-l31vicrc3b5/" -Group "Team Site Owners"
$oldv = Get-SPuser -Web "http://win-l31vicrc3b5/" -Group "Team Site Visitors"

Restore-SPSite "http://win-l31vicrc3b5/" -Path C:\backup\back.back -Force
$newm = Get-SPuser -Web "http://win-l31vicrc3b5/" -Group "Team Site Members"
$newo = Get-SPuser -Web "http://win-l31vicrc3b5/" -Group "Team Site Owners"
$newv = Get-SPuser -Web "http://win-l31vicrc3b5/" -Group "Team Site Visitors"

foreach ($spuser in $newm){Remove-SPUser -Web "http://win-l31vicrc3b5/" -Identity $spuser -Group "Team Site Members" -confirm:$false}
foreach ($spuser in $newo){Remove-SPUser -Web "http://win-l31vicrc3b5/" -Identity $spuser -Group "Team Site Owners" -confirm:$false}
foreach ($spuser in $newv){Remove-SPUser -Web "http://win-l31vicrc3b5/" -Identity $spuser -Group "Team Site Visitors" -confirm:$false}


foreach ($spuser in $oldm){Set-SPUser -Web "http://win-l31vicrc3b5/" -Identity $spuser -Group "Team Site Members" -confirm:$false}
foreach ($spuser in $oldo){Set-SPUser -Web "http://win-l31vicrc3b5/" -Identity $spuser -Group "Team Site Owners" -confirm:$false}
foreach ($spuser in $oldv){Set-SPUser -Web "http://win-l31vicrc3b5/" -Identity $spuser -Group "Team Site Visitors" -confirm:$false}

write-host "Done"

No comments:

Post a Comment