0

Im currently working on a script that will add a AD group to multiple sites via csom powershell where the sites are loaded from a CSV file. After what i have found online, im trying to use the Add-ADGroupToSP function but i am now stuck. See script below:

$Username =$cred.UserName.ToString()
$Password = $cred.GetNetworkCredential().Password
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$SecurePassword)

$Sites = Import-Csv 'c:\PS\sposites.csv'
$ADGroupName = "SGR SP Servicemarked"
$SPGroupName = "permtest Visitors"


Function Add-ADGroupToSP($Site,$ADGroupName,$SPGroupName)
{

    foreach($site in $Sites.URL)
    {

    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
    $Ctx.Credentials = $Creds

    $Web = $Ctx.Web
    $Group = $Web.SiteGroups.GetByName($SPGroupName)

    $ADGroup = $web.EnsureUser($ADGroupName)

    $Result = $Group.Users.AddUser($ADGroup)
    $Ctx.Load($Result)
    $Ctx.ExecuteQuery()

    Add-ADGroupToSP -SiteURL $Site -ADGroupName $ADGroupName -SPGroupName $SPGroupName

    write-host  -f Green "Active Directory Group '$ADGroupName' has been added to '$SPGroupName'"

    }

}

The foreach loop is essential to make the script go through all sites in the CSV, so i want to keep this. This script does not return any error, but nothing else happens either. Its most likely where i call the function, and it is likely misplaced but i cannot figure out how to make this function ( Function Add-ADGroupToSP) loop with the rest of the code under "foreach".

Thanks for all tips and replies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.