0

I need help here with powershell, as im not that good in, im only starting, but i need already to make a powershell script but will run thru all collaborative spaces and modify 4 groups by setting different group owners.

Here is what i have:

function RetrieveSites()
{
    $cs = Get-CsWebAppUrl
    Get-SPSite -WebApplication $cs -Limit All | % {
        Write-host "Retrieving $($Site.Url)"
        ModifyGroupOwners -Site $_
    } 
}

function ModifyGroupOwners([Microsoft.SharePoint.SPSite] $Site)
{
    $spgroup = Get-SPGroup -Site $site -Limit All | % {
        if((spgroup.name -eq "Approver") -or ($spgroup.name -eq "Designer") -or ($spgroup.name -eq "Hierarchy Managers") -or ($spgroup.name -eq "Style Resource Readers") -or ($spgroup.name -eq "Restricted readers"))
        {
            $owner=$spgroup.name + " Managers"
            $spgroup.owner=$spgroup[$owner]
            $spgroup.update
            Write-host "$($spgroup.name) was updated"
        }
    }
}

function Get-CsWebAppUrl()
{
    $Farm = (Get-SPFarm)
    $CsWebAppUrl = $farm.Properties[$global:CsKey]
    if ([string]::IsNullOrEmpty($CsWebAppUrl))
    {
        write-error "Unable to find group Url, please check the portal map configuration"
        exit 1
    }
    return $CsWebAppUrl
}

So the big problem is for retrieving the groups and modifying them, i wrote some ps code but im not sure and i want to have some help and guidance.

Thanks alot for help in advance.

1 Answer 1

1

This little and simple script will go through all site collections on all web application and set group owner for desired groups:

Add-PsSnapin Microsoft.SharePoint.PowerShell

$groupNames = @("Approver", "Designer", "Hierarchy Managers", 
                    "Style Resource Readers", "Restricted readers" )
$ownerUsername = "UserName"

$webApps = Get-SPWebApplication 
foreach ($webApp in $webApps) {
  $web = Get-SPWeb $webApp.Url 
  $spuser = $web.EnsureUser($ownerUsername)

  foreach ($groupName in $groupNames) {
      $group = $web.SiteGroups[$groupName]
      $group.Owner = $spuser
      $group.Update()
  }
}

I hope this is what are you looking for. And keep in mind this is just example, some logging and error handling would be useful.

Edited

To search only one site collection:

  ....

  $web = Get-SPWeb "http://mycoolsite/"
  $spuser = $web.EnsureUser($ownerUsername)

  foreach ($groupName in $groupNames) {
      $group = $web.SiteGroups[$groupName]
      $group.Owner = $spuser
      $group.Update()
  }
3
  • It is really cool example, but i dont need to cycle thru all the web application only one as i know of and there yes i need to iterate thru sites to adjust the owner of particular groups. But the owner should be the group that exists and the name of that group is mostly the name of the collaborative space + 'managers'
    – Igorok
    Commented Jan 25, 2012 at 22:13
  • mostly? is there any other condition that determines exact SiteGoup that needs to be modified? Commented Jan 26, 2012 at 10:46
  • thx for the help, it works
    – Igorok
    Commented Jan 26, 2012 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.