Forum Discussion

null null's avatar
null null
Iron Contributor
Oct 23, 2017

adding domain user to site collection admin for a users od4b site collection

I am trying to write a powershell script to change the Locale from En-US to EN-UK for which i have written powershell script to achieve that. However it does not execute as the the admin needs to to be made site collection secondary admin at least for the one drive for business site. so in the middle of the script before the last execute method i need to add the AdminUser to its site collection administrators. As you know i have used CSOM below; i want to achieve that using CSOM;

I have 

 

tenant url, admin, user od4b url.

 

 

 #Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")






        #Specify tenant admin
        <$AdmiUser = "first.Lastname@domain.abc.uk">
        $Password = Read-Host -Prompt "Please enter your password" -AsSecureString
        $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminUser,$Password)

        #Configure MySite Host URL
       < $SiteURL = "https://tenant-my.sharepoint.com/">


        #Bind to MySite Host Site Collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Context.Credentials = $Creds

#Identify users in the Site Collection
$Users = $Context.Web.SiteUsers
$Context.Load($Users)
$Context.ExecuteQuery()

#Create People Manager object to retrieve profile data
$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Context)

    $UserProfile = $PeopleManager.GetPropertiesFor('<sample user>'')
    $Context.Load($UserProfile)
    $Context.ExecuteQuery()
    If ($UserProfile.Email -ne $null)
        {

        
        Write-Host "Updating OD4B site for User:" $User.LoginName -ForegroundColor Green
        #Bind to OD4B Site and change locale
        $OD4BSiteURL = $UserProfile.PersonalUrl
        $Context2 = New-Object Microsoft.SharePoint.Client.ClientContext($OD4BSiteURL)

         Foreach ($User in $OD4BSiteURL)
        {
        $Context2.Credentials = $Creds
        $Context2.ExecuteQuery()
        #$Context2.Load($web);
        #$Context2.Load($RegionalSettings);

        Write-Host $User 




        $Context2.Web.RegionalSettings.LocaleId = "2057"
        $Context2.Web.Update()
        $Context2.ExecuteQuery()
        }  

        }


Resources