unable to apply PNP template due to MFA

%3CLINGO-SUB%20id%3D%22lingo-sub-1187568%22%20slang%3D%22en-US%22%3Eunable%20to%20apply%20PNP%20template%20due%20to%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1187568%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Everyone%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20MFA%20enabled%20due%20to%20which%20whenver%20I%20run%20PNP%20powershell%20I%20have%20to%20user%20-useweblogin.%20But%20this%20is%20also%20not%20working%20when%20I%20loop%20through%20several%20sites.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Erequirement%3A%201.%26nbsp%3B%20%26nbsp%3BUsing%20PNP%20I%20have%20to%20create%2050%20Sitecolelction%20%3ASharePoint%20online%3C%2FP%3E%3CP%3E2.%20Apply%20PNPProvisioning%20Template%3CBR%20%2F%3E3.%20Associate%20each%20site%20to%20HUB%20site.%26nbsp%3B%3C%2FP%3E%3CP%3E4.%20Enable%20external%20Sharing%20for%20each.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CEM%3EWhen%20I%20ran%20below%20command%20it%20will%20fail%20after%20creating%20first%20site.%26nbsp%3B%20So%20I%20have%20to%20remove%20%22start-sleep%20-s%201-%22%20from%20the%20script.%20If%20I%20do%20that%2C%20it%20will%20create%20all%20the%20sites%20but%20%22Provisioning%20template%22%20is%20applying%20early%20without%20waiting%20for%20site%20to%20be%20created%20properly.%20Due%20to%20which%20it%20will%20fail%20applying%20template%20after%202%20sites.%26nbsp%3B%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENot%20sure%20how%20to%20fix%20this.%20trying%20since%205days.%20no%20luck.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%24TenantUrl%20%3D%20%22https%3A%2F%2Fm365x539190-admin.sharepoint.com%22%0A%24myHubsite%20%3D%20%22https%3A%2F%2Fm365x539190.sharepoint.com%2Fsites%2Ftesting%22%0A%20%20%20%20%20%20%20%20Connect-pnpOnline%20-url%20%24TenantUrl%20-Credentials%20-useweblogin%0A%20%20%20%20%20%20%20%20%20%24siteCollectionList%20%3D%20Import-Csv%20-Path%20%22C%3A%5Csites.csv%22%0A%20%0A%20%0A%20%20%20%20%23Loop%20through%20csv%20and%20provision%20site%20collection%20from%20each%20csv%20entry%0A%20%20%20%20get-date%0A%20%20%20%20foreach%20(%24siteCollection%20in%20%24siteCollectionList)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%24SharePointUrl%20%3D%20%24siteCollection.Url%0A%20%20%20%20%20%20%20%20%24SiteOwner%20%3D%20%24siteCollection.Owner%0A%20%20%20%20%20%20%20%20%24Title%20%3D%20%24siteCollection.Title%0A%20%20%20%20%20%20%20%20%24Template%20%3D%20%24siteCollection.SiteTemplate%0A%20%20%20%20%20%20%20%20%24TimeZone%20%3D%20%24siteCollection.TimeZone%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23Create%20site%20collection%20based%20on%20values%20above%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20New-PnPTenantSite%20-Owner%20%24SiteOwner%20-Title%20%24Title%20-Url%20%24SharePointUrl%20-Template%20%24Template%20-TimeZone%20%24TimeZone%20%20-Wait%0A%20%20%20%20%20%20%20%20write-host%20%22Site%20Collection%20%24(%24SharePointUrl)%20Created%20Successfully!%22%20-foregroundcolor%20Green%0A%20%20%20%20%20%20%20%20get-date%0A%20%20%20%20%20%20%20%20Start-Sleep%20-s%2010%0A%20%0A%0A%20%20%20%20%20%20%20%20%23connect%20to%20each%20site%20that%20was%20created%20earlier%20and%20then%20apply%20PNP%20template%0A%20%20%20%20%20%20%20%20Connect-PnPOnline%20-Url%20%24SharePointUrl%20-Credentials%20-useweblogin%0A%20%20%20%20%20%20%20%20Apply-PnPProvisioningTemplate%20-Path%20C%3A%5Ctemplate8.xml%0A%20%20%20%20%20%20%20%20write-host%20%22Template%20applied%20%24(%24SharePointUrl)%20Successfully!%22%20-foregroundcolor%20Green%0A%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%23connect%20to%20tenant%20site%20again%20and%20then%20add%20each%20site%20to%20HUB%0A%20%20%20%20%20%20%20%20%20Connect-pnpOnline%20-url%20%24TenantUrl%20-Credentials%20-useweblogin%0A%20%20%20%20%20%20%20%20Add-PnPHubSiteAssociation%20-Site%20%24SharePointUrl%20-HubSite%20%24myHubsite%0A%20%20%20%20%20%20%20%20write-host%20%22Hubsite%20association%20of%20%24(%24SharePointUrl)%20completed%20Successfully!%22%20-foregroundcolor%20Green%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20Start-Sleep%20-s%201%0A%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23Connect%20to%20Tenant%20site%20again%20and%20then%20connect%20to%20each%20site%20and%20enable%20external%20sharing%0A%20%20%20%20%20%20%20%20Connect-PnPOnline%20-url%20%24TenantUrl%20-Credentials%20-useweblogin%0A%20%20%20%20%20%20%20%20Connect-PnPOnline%20-Url%20%24SharePointUrl%20-Credentials%20-useweblogin%0A%20%20%20%20%20%20%20%20Set-PnPSite%20-Identity%20%24SharePointUrl%20-Sharing%20ExternalUserSharingOnly%20-Classification%20%22Private%20External%22%0A%20%20%20%20%20%20%20%20write-host%20%22External%20Sharing%20Enablement%20for%20%24(%24SharePointUrl)%20completed%20Successfully!%22%20-foregroundcolor%20Green%0A%20%20%20%20%20%20%20%20get-date%0A%20%0A%20%20%20%20%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1187568%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAdmin%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESites%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1352300%22%20slang%3D%22en-US%22%3ERe%3A%20unable%20to%20apply%20PNP%20template%20due%20to%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1352300%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F288162%22%20target%3D%22_blank%22%3E%40gadagottiraj%3C%2FA%3E%2C%20I'm%20sure%20the%20OP%20has%20already%20figured%20this%20out%20but%20for%20anyone%20googling%20this%3B%20to%20my%20knowledge%20associating%20a%20hub%20whilst%20using%20-useweblogin%20is%20not%20supported.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1496217%22%20slang%3D%22en-US%22%3ERe%3A%20unable%20to%20apply%20PNP%20template%20due%20to%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1496217%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F644084%22%20target%3D%22_blank%22%3E%40_Blackfoj%3C%2FA%3EDoes%20this%20mean%20that%20it%20is%20not%20possible%20with%20MFA%20enabled%3F%26nbsp%3B%20or%20is%20there%20an%20alternative%20to%20connect%20with%20MFA%20without%20-useweblogin%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1498067%22%20slang%3D%22en-US%22%3ERe%3A%20unable%20to%20apply%20PNP%20template%20due%20to%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1498067%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F43188%22%20target%3D%22_blank%22%3E%40Scott%20Smith%3C%2FA%3E%26nbsp%3BI%20believe%20you%20just%20drop%20the%20-UseWebLogin%20if%20MFA%20is%20present%20and%20authenticate%20using%20Credential%20Manager%20or%20just%20typing%20into%20the%20PowerShell%20authentication%20prompt.%20Hope%20that%20helps!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1498136%22%20slang%3D%22en-US%22%3ERe%3A%20unable%20to%20apply%20PNP%20template%20due%20to%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1498136%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F644084%22%20target%3D%22_blank%22%3E%40_Blackfoj%3C%2FA%3E%26nbsp%3B-%20thanks!%26nbsp%3B%20I'll%20try%20credential%20manager.%26nbsp%3B%20It%20will%20be%20nice%20if%20the%20template%20will%20apply%20the%20associated%20hub%20but%20not%20a%20critical%20issue%20otherwise%20since%20it's%20just%20a%20couple%20easy%20clicks.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
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
    get-date
    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
        get-date
        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
        get-date
 
        }

 

 

 

4 Replies
Highlighted

@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. 

Highlighted

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

Highlighted

@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!

Highlighted

@_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.