I am trying to fix an issue with one of my power shell scripts. I am getting this error.

    Exception calling "GetUserProfileByIndex" with "1" argument(s): "Server was unable to process request. ---> Attempted to perform an unauthorized operation."
At line:1 char:1
+ $UserProfileService.GetUserProfileByIndex($UserProfileResult.NextValue)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SoapException

I believe it has something to do with rights, but that does not make sense... I have sharepoint rights and part of the user management role. Please help. The code I am having trouble with is with actually getting the list of onedrive accounts. I have modified the code to fit my needs.

function One_drive_Lists ($username , $password)
    # Specifies the URL for your organization's SPO admin service
    $AdminURI = "https://hfu-admin.sharepoint.com"

    # Specifies the User account for an Office 365 global admin in your organization
    $AdminAccount = $username
    $AdminPass =   $password
    # Specifies the location where the list of MySites should be saved
    $LogFile = 'C:\Onedrive_reports\Reports\ListOfMysites.txt'

    # Begin the process

    $loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
    $loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
    $loadInfo3 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")

    # Convert the Password to a secure string, then zero out the cleartext version ;)
    $sstr = ConvertTo-SecureString -string $AdminPass -AsPlainText -Force
    $AdminPass = ""

    # Take the AdminAccount and the AdminAccount password, and create a credential

    $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminAccount, $sstr)

    # Add the path of the User Profile Service to the SPO admin URL, then create a new webservice proxy to access it
    $proxyaddr = "$AdminURI/_vti_bin/UserProfileService.asmx?wsdl"
    $UserProfileService= New-WebServiceProxy -Uri $proxyaddr -UseDefaultCredential False
    $UserProfileService.Credentials = $creds

    # Set variables for authentication cookies
    $strAuthCookie = $creds.GetAuthenticationCookie($AdminURI)
    $uri = New-Object System.Uri($AdminURI)
    $container = New-Object System.Net.CookieContainer
    $container.SetCookies($uri, $strAuthCookie)
    $UserProfileService.CookieContainer = $container

    # Sets the first User profile, at index -1
    $UserProfileResult = $UserProfileService.GetUserProfileByIndex(-1)

    wl -value "Starting- This could take a while."

    $NumProfiles = $UserProfileService.GetUserProfileCount()
    $i = 1

    # As long as the next User profile is NOT the one we started with (at -1)...
    "email" | Out-File $LogFile  -Force
    While ($UserProfileResult.NextValue -ne -1) 
        wl -value "Examining profile $i of $NumProfiles"

        # Look for the Personal Space object in the User Profile and retrieve it
        # (PersonalSpace is the name of the path to a user's OneDrive for Business site. Users who have not yet created a 
        # OneDrive for Business site might not have this property set.)
        $Prop = $UserProfileResult.UserProfile | Where-Object { $_.Name -eq "UserName" } 
        $Url= $Prop.Values[0].Value
        wl -value $url
        # If "PersonalSpace" (which we've copied to $Url) exists, log it to our file...
        if ($Url) 
            $Url | Out-File $LogFile -Append -Force

        # And now we check the next profile the same way...
        $UserProfileResult = $UserProfileService.GetUserProfileByIndex($UserProfileResult.NextValue)


1 Answer 1


You need to be a "SharePoint administrator". By this, I mean you need to have this role assigned in the manage user screen in the O365 admin portal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.