6.0 KiB
create (default) Groups
Mittwoch, 16. Januar 2019
16:00
https://www.hezser.de/blog/2015/04/30/create-the-default-groups-via-powershell/
$web = Get-SPWeb https://your.site.url
if ($web.AssociatedVisitorGroup -eq $null) {
Write-Host 'The Visitor Group does not exist. It will be created...' -ForegroundColor DarkYellow
$currentLogin = $web.CurrentUser.LoginName
if ($web.CurrentUser.IsSiteAdmin -eq $false){
Write-Host ('The user '+$currentLogin+' needs to be a SiteCollection administrator, to create the default groups.') -ForegroundColor Red
return
}
$web.CreateDefaultAssociatedGroups($currentLogin, $currentLogin, [System.String].Empty)
Write-Host 'The default Groups have been created.' -ForegroundColor Green
} else {
Write-Host 'The Visitor Group already exists.' -ForegroundColor Green
}
Aus <https://www.hezser.de/blog/2015/04/30/create-the-default-groups-via-powershell/>
Auf uns angepasst :
$web = get-spweb $SiteCollURL
if ($web.AssociatedVisitorGroup -eq $null) {
Write-Host 'The Default Groups does not exist. It will be created...' -ForegroundColor DarkYellow
$currentLogin = $web.CurrentUser.LoginName
if ($web.CurrentUser.IsSiteAdmin -eq $false){
Write-Host ('The user '+$currentLogin+' needs to be a SiteCollection administrator, to create the default groups.') -ForegroundColor Red
return
}
$web.CreateDefaultAssociatedGroups($SiteOwner1,$SiteOwner2, [System.String].Empty)
Write-Host 'The Default Groups have been created.' -ForegroundColor Green
} else {
Write-Host 'The Default Groups already exists.' -ForegroundColor Green
}
$SPGroup = $Site.SiteGroups["S-C-B-5 Visitors"]
$adgroup = "bkk-mobiloil\SG-SPT-SC-PTL-S-C-B-5-Besucher"
$sitecoll = https://portal-spt.bkk-mobiloil.de/sites/scb5
$spfriendlyAdname = $sitecoll |Get-SPUser |where {$_.name -like $adgroup}
Set-SPUser -Identity $spfriendlyAdname -Web $sitecoll -Group $SPGroup
Ist schon im Script vorhanden :
$url = "scb6"
$Name = "S-C-B-6"
$stage = "SPT"
$SiteCollURL = "https://portal-"+$stage+".bkk-mobiloil.de/sites/$url"
$preName = "SG-"+$stage+"-SC-PTL-"
$postName = "-Besitzer"
$SiteName = "$Name"
$adgroup1 = $preName + $sitename + $postName
Muss neu :
$sitecoll = Get-SPWeb $SiteCollURL #Erzeugen des WebObjektes
# Für Owners (Besitzer)
$adgroup = "bkk-mobiloil\"+$adgroup1
$sitecoll.EnsureUser($adgroup)
$spfriendlyAdname = $sitecoll |Get-SPUser |where {$_.name -like $adgroup}
$SPGroup = $sitecoll.SiteGroups |where {$_.Name -match "$name+ Owners"}
Set-SPUser -Identity $spfriendlyAdname -Web $sitecoll -Group $SPGroup
# Für Visitors (Besucher)
$adgroup = "bkk-mobiloil\"+$adgroup2
$sitecoll.EnsureUser($adgroup)
$spfriendlyAdname = $sitecoll |Get-SPUser |where {$_.name -like $adgroup}
$SPGroup = $sitecoll.SiteGroups |where {$_.Name -match "$name+ Visitors"}
Set-SPUser -Identity $spfriendlyAdname -Web $sitecoll -Group $SPGroup
Für Members (Mitglieder
$adgroup = "bkk-mobiloil\"+$adgroup3
$sitecoll.EnsureUser($adgroup)
$spfriendlyAdname = $sitecoll |Get-SPUser |where {$_.name -like $adgroup}
$SPGroup = $sitecoll.SiteGroups |where {$_.Name -match "$name+ Members"}
Set-SPUser -Identity $spfriendlyAdname -Web $sitecoll -Group $SPGroup
$sitecoll.EnsureUser($adgroup)
$SPGroup = $sitecoll.SiteGroups |where {$_.Name -match "$name+ Visitors"}
Add-PSSnapin Microsoft.SharePoint.PowerShell --ErrorAction SilentlyContinue
Custom Function to Create new SharePoint Group
function Create**-SPGroup**
{
param ($SiteURL, $GroupName, $PermissionLevel, $GroupDescription)
try
{
#Get the Web
$web = Get**-SPWeb** -Identity $SiteURL
if($web -ne $null)
{
#Check if Group Exists already
if ($web.SiteGroups[$GroupName] -ne $null)
{
write-Host "Group $GroupName exists Already!" -ForegroundColor Red
}
else
{
#Create SharePoint Group
$Web.SiteGroups.Add($GroupName, $web.Site.Owner, $web.Site.Owner, $GroupDescription)
#Get the newly created group and assign permission to it
$Group = $web.SiteGroups[$groupName]
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
$roleDefinition = $web.Site.RootWeb.RoleDefinitions[$permissionLevel]
$roleAssignment.RoleDefinitionBindings.Add($roleDefinition)
$web.RoleAssignments.Add($roleAssignment)
$web.Update()
write-Host "Group: $GroupName created successfully!" -ForegroundColor Green
}
$web.Dispose()
}
}
catch [System.Exception]
{
write-host $_.Exception.ToString() -ForegroundColor Red
}
}
Call the function to create Sharepoint group
Create**-SPGroup** "http://sales.crescent.com" "Sales Managers" "Edit" "Group for Sales Managers"
Aus <https://www.sharepointdiary.com/2015/01/create-sharepoint-group-using-powershell.html>