0

enter image description here

I've been trying to run different PowerShell scripts to get all or subsites that are into a site collection, however when I run this ask for the Windows PowerShell credential that haven't able to figure out to get the correct one or kind of username there

This one is an example of any of scripts I've been trying to run to figure how to fetch these subsites:

#Provide your SharePoint Online Admin center URL
$AdminSiteURL = "**"
 
#Get SharePoint Admin User Credentials  
$Cred = Get-Credential
 
#Connect to SharePoint Admin Site
Connect-PnPOnline -Url $AdminSiteURL -Credentials $Cred 
 
#Get all site collections
$Sites = Get-Collaboration-SiteTesting
#The below command gets only modern Team & Communication sites
#$Sites = Get-Collaboration-SiteTesting | Where -Property Template -In ("", "SITEPAGEPUBLISHING#0")
   
$AllSites = @()
 
$i = 0;
$TotoalSites = $Sites.Count

#Enumerate site collections and get sub sites recursively
ForEach($Site in $Sites)
{
    $i++;
    Write-Progress -activity "Processing $($Site.Url)" -status "$i out of $TotalSites completed"
 
    $SubWebs=$null;
    Try
    {
    
        #Connect to site collection
        $SiteConnection = Connect-PnPOnline -Url $Site.Url -Credentials $Cred
   
        #Get the sub sites of the site collection
        $SubWebs = Get-PnPSubWeb -Recurse -Connection $SiteConnection
  
        Disconnect-PnPOnline -Connection $SiteConnection
    }
    catch{
        Write-Host "Error occured $($Site.Url) : $_.Exception.Message"   -Foreground Red;
    }
 
    #Add site collection in AllSites list 
    $AllSites += New-Object PSObject -property $([ordered]@{ 
        SiteName  = $Site.Title            
        SiteURL = $Site.Url
        IsSubSite = $false
        HasSubSites = if ($SubWebs -and $SubWebs.Count -gt 0) { $true } Else {$false}
        SiteCollectionName = $Site.Title
        SiteCollectionURL = $Site.Url
    })
 
    if ($SubWebs -and $SubWebs.Count -gt 0) {
    
        #Enumerate sub sites and add in AllSites list 
        ForEach($SubSite in $SubWebs)
        {
            $AllSites += New-Object PSObject -property $([ordered]@{ 
            SiteName  = $SubSite.Title            
            SiteURL = $SubSite.Url
            IsSubSite = $true
            HasSubSites = $false
            SiteCollectionName = $Site.Title
            SiteCollectionURL = $Site.Url
            })
        }
    }
}
1
  • You have to use your SharePoint (office 365) username and password here. Try it and let me know if it works for you. Commented Jan 5, 2023 at 2:54

2 Answers 2

0

You have to use your SharePoint (office 365) username and password in "Windows PowerShell credential request" popup.

If multi-factor authentication (MFA) is enabled for your user account, you can use -Interactive login or Office 365 app password instead of normal user account password.

Check more details at:

  1. Use App Password to Authenticate with PowerShell PnP
  2. Connect-SPOService : The sign-in name or password does not match one in the Microsoft account system
0

When you run different PowerShell scripts to get all or subsites that are into a site collection, you need to sign in an account which has the propriate access to those sites. The reason is the same as we need to key in username and password when we access a site collection from browser.

Your Answer

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

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