SharePoint自动化系列——Create a local user and add to SharePoint

转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/

实现过程:在本地创建一个local user并将该user添加到Administrators组中,然后在SharePoint指定site中添加该user,并赋予Full Control的权限。

脚本如下: 

function AddUserToSPSite
{
    param($siteUrl,$userName,$pwd,$fullName,$dspt)
    #Create a local user and add to a local group.
    try{
        $computer = [ADSI]"WinNT://$Env:COMPUTERNAME,Computer"
        $user = $Computer.Create("User", $userName)
        $user.SetPassword($pwd)
        $user.SetInfo()
        $user.FullName = $fullName
        $user.Description = $dspt
        # ADS_UF_PASSWD_CANT_CHANGE + ADS_UF_DONT_EXPIRE_PASSWD
        $user.UserFlags = 64 + 65536 
        $user.SetInfo()
        $group = [ADSI]"WinNT://./Administrators,group"
        $group.Add("WinNT://$userName,user")
    }catch
    {
        Write-Warning "User exists in local, no need to create new."
    }
    #Add the new created local user to a SharePoint site.
    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
    {
        Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    }
    $site = Get-SPSite $siteUrl
    $web = $site.rootWeb
    $SPUserName = $env:COMPUTERNAME+"\"+$userName
    $user = New-SPUser -UserAlias $SPUserName -DisplayName $userName -Web $web
    Set-SPUser -Identity $user -Web $web -AddPermissionLevel "Full Control"
}
AddUserToSPSite -siteUrl "http://xxx" -userName "xxx" -pwd "xxx" -fullName "xxx" -dspt "xxx"

如果本地已经存在欲添加的user以及成功添加user到SharePoint站点均会有提示:

posted @ 2015-12-08 18:40  天外归云  阅读(502)  评论(0编辑  收藏  举报