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

 

實現過程:在本地建立一個local user並將該user添加到Administrators組中,而後在SharePoint指定site中添加該user,並賦予Full Control的權限。web

腳本以下: api

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站點均會有提示:ide

相關文章
相關標籤/搜索