Skip to content

Instantly share code, notes, and snippets.

@SharePointX
Created November 29, 2016 10:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SharePointX/776b260febebc4d938eb8e982829ea7a to your computer and use it in GitHub Desktop.
Save SharePointX/776b260febebc4d938eb8e982829ea7a to your computer and use it in GitHub Desktop.
PowerShellでSharePointの権限設定
# サイトURLを指定
$webUrl = "http://yamasp2016/sites/es2_20160726"
# ページURLを指定
$pageUrl = "Pages/page1.aspx"
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
#サイトを取得
$web = Get-SPWeb $webUrl
#ページ(SPFile)取得
$page = $web.GetFile($pageUrl)
#ページに該当するライブラリのアイテム(SPListItem)取得
$item = $page.Item
#既存の権限を(コピーして)残すか?
$copyRoleAssignments = $false
#権限の継承を中止
$item.BreakRoleInheritance($copyRoleAssignments)
#権限を割り当てるグループかユーザーを取得(1件づつ)
$ownerGroup = $web.AssociatedOwnerGroup #サイトの所有者グループ
#$memberGroup = $web.AssociatedMemberGroup #サイトのメンバーグループ
#$visitorGroup = $web.AssociatedVisitorGroup #サイトの閲覧者グループ
#$someGroup = $web.Groups.GetByName("任意のグループ名")
#$syetemUser = $web.Site.SystemAccount #シスアカ
#$someUser = $web.EnsureUser("ログイン名")
#権限割り当ての器を生成
$roleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($ownerGroup)
#割り当てる権限(サイトに定義されたアクセス許可レベル)を取得して割り当て(複数指定可能)
#1個目
$roleDefinition1 = $web.RoleDefinitions.GetByType([Microsoft.SharePoint.SPRoleType]::Administrator) #タイプで指定
$roleAssignment.RoleDefinitionBindings.Add($roleDefinition1) #権限を権限割り当ての器に追加
#2個目
$roleDefinition2 = $web.RoleDefinitions["閲覧"] #名前で指定
$roleAssignment.RoleDefinitionBindings.Add($roleDefinition2) #権限を権限割り当ての器に追加
$item.RoleAssignments.Add($roleAssignment)
#権限を割り当てるグループかユーザーがほかにもある場合は上記を1件づつ繰り返し
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment