user1896053
user1896053

Reputation: 1

PowerShell to add Security AD Account into SharePoint Group

I am very new to Powershell scripting and have drafted a script with the aim of adding an AD Security group into the SharePoint Admin group. The script is supposed to loop through all site collections within my webapplication and add the Security group into the pre-existing SharePoint group:

###########Add Content Team as member of Admin Group 

Add-PSSnapin Microsoft.SharePoint.PowerShell
$webApp = Get-SPWebApplication [http://mysite]

function CT{
    param( [string]$siteColl, $itemID, $GroupAdmin )  

        try
        {                   

            new-SPUser -UserAlias "UK\g-content-team" -Web $siteColl -Group $GroupAdmin

           # StatusMessageUpdate $itemID "Add CT into Admin group"

        } 
        catch
        {
            #StatusMessageUpdate $itemID "Fail Add User to Owner Group"
            StatusUpdate $itemID "Error"
            $global:isError = "1"
        }  
}

 CT $site $itemID $adminGroup    

$web.Dispose()

I have commented out the Status Message update as this was creating errors. Without it, there are no errors, but it's not adding the group into any of my sites. I know there is a huge chunk of code missing - I just need some assistance with getting the code to loop. Any help very appreciated

Upvotes: 0

Views: 4494

Answers (1)

user1913699
user1913699

Reputation:

    if($spgroup){
        $spuser = $spweb.EnsureUser("$domain\$adgroupname")
        if($spuser){
            $spgroup.AddUser($spuser)
            $spgroup.Update()
        }
    }

Upvotes: 1

Related Questions