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站点均会有提示:

转载于:https://www.cnblogs.com/LanTianYou/p/5030613.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值