ysfibm
ysfibm

Reputation: 436

Connect-PnPOnline : The remote server returned an error: (403) Forbidden

I'm creating a new team using PowerShell then try to connect to it to attach it to the hub site using Connect-PnPOnline command.

Sometimes it works well but in some cases the team is created but I can't connect to it I have the error :

Connect-PnPOnline : The remote server returned an error: (403) Forbidden. at run.ps1: line 48 Here's my code:

#Tenant Infos
$tenant_id=  "TenantID"
$username =  "AdminLogin"
$Password =  "password"
$encpassword = convertto-securestring -String $Password -AsPlainText -Force
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $encpassword
$tenantPrefix = "tenantPrefix"
#Create group /  modern team site / team 
Connect-MicrosoftTeams -TenantId $tenant_id -Credential $credentials
$group = New-Team -Alias $SiteTitle -Description $sitedescription -DisplayName $SiteTitle -AccessType "private"
$siteUrl = $tenantPrefix + $SiteTitle
Start-Sleep -Seconds 60
Disconnect-MicrosoftTeams 
$connectionPnP = Connect-PnPOnline -Url $siteUrl -Credentials $credentials -ReturnConnection
Add-PnPHubSiteAssociation -Site $siteUrl -HubSite $hubsite
Disconnect-PnPOnline -Connection $connectionPnP

Note : I execute the script from both PowerShell console and Azure function.

Upvotes: 1

Views: 4944

Answers (1)

Murilo Santana
Murilo Santana

Reputation: 665

I am having troubles for some tenants when using the "-Credentials" parameter.

The solution for me is to use

$connectionPnP = Connect-PnPOnline -Url $siteUrl -UseWebLogin

Documentation

Connect-PnPOnline
       -Url <String>
                        -UseWebLogin [<SwitchParameter>]
                        [-ReturnConnection [<SwitchParameter>]]
                        [-MinimalHealthScore <Int>]
                        [-RetryCount <Int>]
                        [-RetryWait <Int>]
                        [-RequestTimeout <Int>]
                        [-CreateDrive [<SwitchParameter>]]
                        [-DriveName <String>]
                        [-Scopes <String[]>]
                        [-TenantAdminUrl <String>]
                        [-SkipTenantAdminCheck [<SwitchParameter>]]
                        [-IgnoreSslErrors [<SwitchParameter>]]
                        [-NoTelemetry [<SwitchParameter>]]

Upvotes: 1

Related Questions