I am trying to extract the owner of a sharepoint site with a powershell script but I always encounter this erroe,I have given all the permisions required

Khadija Ewonye Yakubu 0 Reputation points
2024-12-04T09:57:23.7766667+00:00

Param(

[Parameter(Mandatory = $true)]

[string] $SiteURL

)

$SiteURL="https://xxxxx.sharepoint.com/sites/xxxx"

$env:PNPPOWERSHELL_UPDATECHECK = 'Off'

$AdminSiteURL = "https://xxxxx-admin.sharepoint.com"

$ClientId = "xxxxxxx"

$TenantId = "xxxxxxx"

$ClientSecret = "xxxxxxxx"

Connect-PnPOnline -Url $AdminSiteURL -ClientId $ClientId -ClientSecret $ClientSecret -Tenant $TenantId

$Site = Get-PnPTenantSite -Identity "https://xxxxx.sharepoint.com/sites/xxxx" -Detailed

if (-not $Site) {

Write-Error "The site with URL 'https://xxxx.sharepoint.com/sites/xxxx' could not be found."

return

}

Write-Host -ForegroundColor Green "Site Owner(s) of the site: $($Site.Owner)"

Connect-PnPOnline -Url $Site.Url -ClientId $ClientId -ClientSecret $ClientSecret -Tenant $TenantId

If ($Site.Template -like 'GROUP*') {

if ($Site.GroupId) {

$GroupOwners = (Get-PnPMicrosoft365GroupOwners -Identity $Site.GroupId | Select-Object -ExpandProperty Email) -join ";"

} else {

$GroupOwners = "No Group ID found for this site."

}

} Else {

$GroupOwners = $Site.Owner

}

Output Site Owners

Write-Host "Type of Owners Variable: $($GroupOwners.GetType())"

Write-Output $GroupOwners Error received System.Management.Automation.ParameterBindingException: Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided. at System.Management.Automation.CmdletParameterBinderController.ThrowAmbiguousParameterSetException(UInt32 parameterSetFlags, MergedCommandParameterMetadata bindableParameters) at System.Management.Automation.CmdletParameterBinderController.ValidateParameterSets(Boolean prePipelineInput, Boolean setDefault) at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParametersNoValidation(Collection1 arguments) at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParameters(Collection1 arguments) at System.Management.Automation.CommandProcessor.BindCommandLineParameters() at System.Management.Automation.CommandProcessor.Prepare(IDictionary psDefaultParameterValues) at System.Management.Automation.CommandProcessorBase.DoPrepare(IDictionary psDefaultParameterValues) at System.Management.Automation.Internal.PipelineProcessor.Start(Boolean incomingStream) at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input) --- End of stack trace from previous location --- at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input) at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext) at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline commands which uses the -Url argument to connect. The site with URL 'https://xxxxxxx.sharepoint.com/sites/xFinance' could not be found.

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,152 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,706 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ling Zhou_MSFT 19,935 Reputation points Microsoft Vendor
    2024-12-05T06:10:01.56+00:00

    Hi @Khadija Ewonye Yakubu,

    Hope you have a great day.

    The error "Parameter set cannot be resolved using the specified named parameters" typically occurs when there is a conflict or an incorrect combination of parameters in your PowerShell command.

    We ran a test where the above error occurs after specifying -Tenant. Since -Tenant is not a required parameter, we can omit it. Refer to the documentation for the Connect-PnPOnline command.

    I made some changes based on the PowerShell command you provided to extract the owner of the SharePoint site. Please try to see if the PowerShell command I provided works.

    #Parameters
    $AdminSiteURL = "https://contoso-admin.sharepoint.com"
    $CSVPath = "C:\SiteOwners.csv"
    $ClientId = "xxxxxxx"
    $ClientSecret = "xxxxxxxx"
    #Connect to Admin Center Site
    Connect-PnPOnline -Url $AdminSiteURL -ClientId $ClientId -ClientSecret $ClientSecret  
    #Get All Site collections
    $SiteCollections = Get-PnPTenantSite
    $SiteOwners = @()
     
    #Loop through each site collection
    ForEach($Site in $SiteCollections)
    {
        Write-host -F Green "Site Owner(s) of the site: " $Site.Url
        Connect-PnPOnline -Url $Site.Url -ClientId $ClientId -ClientSecret $ClientSecre
         
        If($Site.Template -like 'GROUP*')
        {
            #Get Group Owners
            $Owners = (Get-PnPMicrosoft365GroupOwners -Identity ($Site.GroupId)  | Select -ExpandProperty Email) -join "; "
        }
        Else
        {
            #Get Site Owner
            $Owners = $Site.Owner
        }
         #Collect Data
        $SiteOwners += New-Object PSObject -Property @{
        'Site Title' = $_.Title
        'URL' = $_.Url
        'Owner(s)' = $Owners
        }
    }
    #Get Site Owners
    $SiteOwners
      
    #Export Site Owners report to CSV
    $SiteOwners | Export-Csv -path $CSVPath -NoTypeInformation
    

    Reference: SharePoint Online: Get the Site Owner using PowerShell

    Non-official, just for reference.

    If you have any questions, please do not hesitate to contact me.

    Moreover, if the issue can be fixed successfully, please click "Accept Answer" so that we can better archive the case and the other community members who are suffering the same issue can benefit from it.

    Your kind contribution is much appreciated.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.