[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2417
  • Last Modified:

Update SharePoint Group Owner using Powershell for SharePoint 2007

I'm trying to write a script that iterates through all Site Collection SharePoint groups and updates the Group Owner.  I'm able to traverse the various SharePoint Groups and retrieve the existing Group Owners, but my code doesn't actually update the Group Owner.  The following is my code:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = New-Object Microsoft.SharePoint.SPSite("https://mysharepointsite/sites/sitename")
$web = $site.OpenWeb()
$groups = $web.sitegroups
$groupowner = "New Group Name"
foreach ($grp in $groups){
"Group: " + $grp.name
"Name:" + $grp.Owner.Name
 $grp.Owner.Name = $groupowner
$grp.Update()
 }
$web.Update()
$site.Dispose()
0
peerlesslv
Asked:
peerlesslv
  • 3
  • 2
2 Solutions
 
prashanthdCommented:
The following should be helpful..
param([string]$url, [string]$Group, [string]$Role, [string]$Owner, [switch]$help)

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

function GetHelp() {


$HelpText = @"

DESCRIPTION:
NAME: Add-SPGroup
Adds a New SharePoint Group

PARAMETERS: 
-url		Url to SharePoint Site
-Group		Name of New Group
-Role		Role Of New Group
-Owner		Group Owner

SYNTAX:

Add-SPGroup -url http://moss -Group "New Group" -Role Read -Owner "powershell\administrator"

Adds the Group "New Group" to the Site.

Add-SPSGroup -help

Displays the help topic for the script

"@
$HelpText

}

function Get-SPSite([string]$url) {

	New-Object Microsoft.SharePoint.SPSite($url)
}

function Get-SPWeb([string]$url) {

	$SPSite = Get-SPSite $url
	return $SPSite.OpenWeb()
	$SPSite.Dispose()
}

function Add-SPGroup ([string]$url, [string]$Group, [string]$Role, [string]$Owner) {

	$SPSite = Get-SPSite $url
	$OpenWeb = Get-SPWeb $url

	$OpenWeb.SiteGroups.Add($Group,$SPSite.RootWeb.AllUsers[$Owner], $Null, $Group)
	$CurrentGroup = $OpenWeb.SiteGroups[$Group]
	$OpenWeb.Roles[$Role].AddGroup($CurrentGroup)

	$OpenWeb.Dispose()
}
if($help) { GetHelp; Continue }
if($url -AND $Group -AND $Role -AND $Owner) { Add-SPGroup -url $url -Group $Group -Role $Role -Owner $Owner }

Open in new window

0
 
peerlesslvAuthor Commented:
Thanks for the response.  Looks like the code you provided is used for adding new groups.  In my situation, I'm trying to iterate all SharePoint Groups within my site and set the owner to an existing SharePoint Group within the site.  I did try to leverage some of your code and came up with the following.  

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = New-Object Microsoft.SharePoint.SPSite("https://mysharepointsite/sites/sitename")
$web = $site.OpenWeb()

$groups = $web.sitegroups
$groupowner = $site.RootWeb.AllUsers["Existing SharePoint Group"];

foreach ($grp in $groups){
"Group: " + $grp.name
"Name:" + $grp.Owner.Name
 $grp.Owner.Name = $groupowner
$grp.Update()
 }

$web.Update()
$site.Dispose()

In doing so, I got the following error:
Exception setting "Name": "The parameter Name cannot be empty or bigger than 255 characters."

This tells me that this variable isn't getting set properly:
$groupowner = $site.RootWeb.AllUsers["Existing SharePoint Group"];

Any thoughts on what I could be doing wrong?


0
 
peerlesslvAuthor Commented:
Got it...turns out I can't set Group Owner with the following line:

$grp.Owner.Name = $groupowner

Instead, this worked:

$grp.Owner=$web.SiteGroups["Existing SharePoint Group"];
0
 
peerlesslvAuthor Commented:
I granted you the points.  While I didn't use any of your code, it got me to think that I couldn't assign a Group Owner a string, but rather a Site Group object.
0
 
prashanthdCommented:
Thanks!!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now