我正在尝试使用PowerShell添加新组并将用户添加到SharePoint中的组。到目前为止,我有这个完成任务的脚本。我想修改脚本,以便它可以从具有这种格式的组和用户的文本文件中读取(或推荐另一种方式)。
Group1
User1,User2,User3 etc.
Group2
User5,User7 etc.
有人可以告诉我如何才能完成这项任务吗?
Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
$SiteUrl = "http://www.dev.com"
$Web = Get-SPWeb $SiteUrl
$description = "Group with contribute access."
$permissionLevel = "Contribute"
$groups = "1", "2"
foreach($groupName in $groups)
{
$web.SiteGroups.Add($groupName, $web.SiteUsers["Test\data"], $web.SiteUsers["Test\data"], $description)
$group = $web.SiteGroups[$groupName]
$loc = Get-Location
$Users = "Test\data"
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
$roleDefinition = $web.Site.RootWeb.RoleDefinitions[$permissionLevel]
$roleAssignment.RoleDefinitionBindings.Add($roleDefinition)
$web.RoleAssignments.Add($roleAssignment)
$web.Update()
foreach ($User in $Users) {
$Web.EnsureUser($User)
Set-SPUser -Identity $User -Web $SiteUrl -Group $group
}
}
$Web.Dispose();
答案 0 :(得分:0)
除非您有大量输入文件,否则我会以多种方式拆分文件:
这样的事情应该做:
(Get-Content 'C:\temp\test.txt' -Raw) -split "`r`n`r`n`r`n" | ? { $_ } | % {
$group, $userlist = $_ -split "`r`n`r`n"
$users = $userlist -split ','
# do stuff with $group and $users
}
请注意,参数-Raw
需要PowerShell v3或更高版本。对于旧版本,请在-join
之前使用-split
:
(Get-Content 'C:\temp\test.txt') -join "`r`n" -split "`r`n`r`n`r`n" | ...