unable to apply PNP template due to MFA

Occasional Contributor

Hello Everyone,


I have MFA enabled due to which whenver I run PNP powershell I have to user -useweblogin. But this is also not working when I loop through several sites. 


requirement: 1.   Using PNP I have to create 50 Sitecolelction :SharePoint online

2. Apply PNPProvisioning Template
3. Associate each site to HUB site. 

4. Enable external Sharing for each. 


When I ran below command it will fail after creating first site.  So I have to remove "start-sleep -s 1-" from the script. If I do that, it will create all the sites but "Provisioning template" is applying early without waiting for site to be created properly. Due to which it will fail applying template after 2 sites. 



Not sure how to fix this. trying since 5days. no luck. 

$TenantUrl = "https://m365x539190-admin.sharepoint.com"
$myHubsite = "https://m365x539190.sharepoint.com/sites/testing"
        Connect-pnpOnline -url $TenantUrl -Credentials -useweblogin
         $siteCollectionList = Import-Csv -Path "C:\sites.csv"
    #Loop through csv and provision site collection from each csv entry
    foreach ($siteCollection in $siteCollectionList)
        $SharePointUrl = $siteCollection.Url
        $SiteOwner = $siteCollection.Owner
        $Title = $siteCollection.Title
        $Template = $siteCollection.SiteTemplate
        $TimeZone = $siteCollection.TimeZone
        #Create site collection based on values above        
        New-PnPTenantSite -Owner $SiteOwner -Title $Title -Url $SharePointUrl -Template $Template -TimeZone $TimeZone  -Wait
        write-host "Site Collection $($SharePointUrl) Created Successfully!" -foregroundcolor Green
        Start-Sleep -s 10

        #connect to each site that was created earlier and then apply PNP template
        Connect-PnPOnline -Url $SharePointUrl -Credentials -useweblogin
        Apply-PnPProvisioningTemplate -Path C:\template8.xml
        write-host "Template applied $($SharePointUrl) Successfully!" -foregroundcolor Green
          #connect to tenant site again and then add each site to HUB
         Connect-pnpOnline -url $TenantUrl -Credentials -useweblogin
        Add-PnPHubSiteAssociation -Site $SharePointUrl -HubSite $myHubsite
        write-host "Hubsite association of $($SharePointUrl) completed Successfully!" -foregroundcolor Green
        Start-Sleep -s 1
        #Connect to Tenant site again and then connect to each site and enable external sharing
        Connect-PnPOnline -url $TenantUrl -Credentials -useweblogin
        Connect-PnPOnline -Url $SharePointUrl -Credentials -useweblogin
        Set-PnPSite -Identity $SharePointUrl -Sharing ExternalUserSharingOnly -Classification "Private External"
        write-host "External Sharing Enablement for $($SharePointUrl) completed Successfully!" -foregroundcolor Green




4 Replies

@gadagottiraj, I'm sure the OP has already figured this out but for anyone googling this; to my knowledge associating a hub whilst using -useweblogin is not supported. 

@_BlackfojDoes this mean that it is not possible with MFA enabled?  or is there an alternative to connect with MFA without -useweblogin?

@Scott Smith I believe you just drop the -UseWebLogin if MFA is present and authenticate using Credential Manager or just typing into the PowerShell authentication prompt. Hope that helps!

@_Blackfoj - thanks!  I'll try credential manager.  It will be nice if the template will apply the associated hub but not a critical issue otherwise since it's just a couple easy clicks.