1

Is there a way to find the properties of the site/web using Get-PnPPropertyBag by looping through a Hub site and associated sites? Get-PnPPropertyBag -Key MyKey only for one site properties, but I would like to iterate through sites on SPO and get properties to output into a CSV file..

Connect-PnPOnline -Url "https://mytenat-admin.sharepoint.us/" 

#Getting the hub site id for which we want to generate the report - those are connected to this hub site.
$hubSiteURL="https://mytenatgov.sharepoint.us/sites/TargetSite"
$hubSite = Get-PnPTenantSite $hubSiteURL  
$hubSiteId = $hubSite.HubSiteId
write-host " #####Generating sites connected a single hub site report######: " -BackgroundColor DarkGreen
write-host "Hub Site URL: " $hubSiteURL


$associatedSites = @()


#Get all sites associated to the hub site(in the above hub site)
$sitesTenant = Get-PnPTenantSite -Detailed 
$sitesTenant | select url | % {$oneSite = Get-PnPTenantSite $_.url 

  if($oneSite.hubsiteid -eq $hubSiteId)
  {

    write-host "Associated Site URL: " $oneSite.url

     $assocatedSiteObject = New-Object PSObject     
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "HubSiteURL" -value $hubSiteURL
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "HubSiteID" -value $hubSiteId
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Title" -value $oneSite.Title
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -Name "URL" -value $oneSite.Url
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "AssociatedStatus" -value $oneSite.Status



     #Add the object with property to an Array
     $associatedSites += $assocatedSiteObject

  }
}

#Export the site array collection to a CSV file
1

Using the Get-PnPPropertyBag command we will get the all property bag key and value from the tenant:

enter image description here

In the script we can add the below inside foreach loop

$propretyBagValue=Get-PnPPropertyBag -Key "vti_sitemasterid"     
    write-host "Associated Site URL: " $oneSite.url
    $assocatedSiteObject = New-Object PSObject

     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site URL" -value $oneHubSite.SiteUrl
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site ID" -value $oneHubSite.ID
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -Name "Associated Site URL" -value $oneSite.Url
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Associated Site Status" -value $oneSite.Status
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Property Bag Value" -value $propretyBagValue     

     #Add the object with property to an Array
     $associatedSites += $assocatedSiteObject 

The final script will look like below:

CLS    
$userName = "Global-sharepoint2019@globalsharepoint2019.onmicrosoft.com"
$passWord = "YourPassWord"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord

Connect-PnPOnline -Url "https://globalsharepoint2019-admin.sharepoint.com/" -Credentials $cred

#Get all sites associated to the hub site(in the above hub site)
$sitesTenant = Get-PnPTenantSite -Detailed 
$sitesTenant | select url | % {$oneSite = Get-PnPTenantSite $_.url 

  if($oneSite.hubsiteid -eq $hubSiteId)
  {
       $propretyBagValue=Get-PnPPropertyBag -Key "vti_sitemasterid" 

    write-host "Associated Site URL: " $oneSite.url
    $assocatedSiteObject = New-Object PSObject

     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site URL" -value $oneHubSite.SiteUrl
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Hub Site ID" -value $oneHubSite.ID
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -Name "Associated Site URL" -value $oneSite.Url
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Associated Site Status" -value $oneSite.Status
     $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Property Bag Value" -value $propretyBagValue     

     #Add the object with property to an Array
     $associatedSites += $assocatedSiteObject

  }
}     

}
#Export the site array collection to a CSV file
$associatedSites | Export-CSV "C:\Temp\GetAllSitesAssociatedInHubSites\SitesConnectedToHubSiteReprotForTenant.csv" -NoTypeInformation  
write-host "##### Generating master hub sites along with connected sites report for the tenant ends here ######:" -BackgroundColor DarkYellow

######The below script will list down all hub sites and their associated connected sites in the tenant - ends here##################

The propertybag value csv will look like below:

enter image description here

Notes :

  • This is the default property bag value for the site master in my tenant... so if we have the custom propertybag value in the tenant, the will get the different key value results of Propertybag... in that case accordingly we need to pass the parameter in Get-PnPPropretyBag command.
  • For some reason it returned empty. is it because of my connection. I do have custom property. – Aby Dec 29 '19 at 9:38
  • It could be the connection issue.. try to debug connect-PnPOnline... and the account using which running this code, it should be SharePoint Administrator or global administrator right. – SP Expert Dec 29 '19 at 10:24
  • Yes, I am global admin, it only get the first site and connection stops – Aby Dec 30 '19 at 13:44
0

The following PowerShell Script for your reference.

$creds = Get-Credential
Connect-PnPOnline -Url "https://mytenat-admin.sharepoint.us/" -Credential $creds

#Getting the hub site id for which we want to generate the report - those are connected to this hub site.
$hubSiteURL="https://mytenatgov.sharepoint.us/sites/TargetSite"
$hubSite = Get-PnPTenantSite $hubSiteURL  
$hubSiteId = $hubSite.HubSiteId
write-host " #####Generating sites connected a single hub site report######: " -BackgroundColor DarkGreen
write-host "Hub Site URL: " $hubSiteURL

$associatedSites = @()

#Get all sites associated to the hub site(in the above hub site)
$sitesTenant = Get-PnPTenantSite -Detailed 
$sitesTenant | select url | % {
    $oneSite = Get-PnPTenantSite $_.url
    if($oneSite.hubsiteid -eq $hubSiteId)
    {
        Connect-PnPOnline -Url $oneSite.Url -Credential $creds
        $property=Get-PnPPropertyBag -Key 'MyKey'
        write-host "Associated Site URL: " $oneSite.url
        $assocatedSiteObject = New-Object PSObject     
        $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "HubSiteURL" -value $hubSiteURL
        $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "HubSiteID" -value $hubSiteId
        $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "Title" -value $oneSite.Title
        $assocatedSiteObject | Add-Member -MemberType NoteProperty -Name "URL" -value $oneSite.Url
        $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "AssociatedStatus" -value $oneSite.Status
        $assocatedSiteObject | Add-Member -MemberType NoteProperty -name "MyProperty" -value $property
        #Add the object with property to an Array
        $associatedSites += $assocatedSiteObject
    }
}
#Export the site array collection to a CSV file
$associatedSites | Export-CSV "C:\Temp\HubSiteReprotForTenant.csv" -NoTypeInformation  
  • Thanks guys, I got the first site property, but then the powershell disconnect. I have more than 20+ sites i would like to get their properties. How should I make sure the ps connection keep running? – Aby Dec 30 '19 at 12:56
  • @Aby: Then inside the foreach loop of each sites , try to write the below connect command and see Connect-PnPOnline -Url "globalsharepoint2019-admin.sharepoint.com" -Credentials $cred – SP Expert Dec 30 '19 at 16:10
  • @HabiburRahaman Sitll no luck with admin site url – Aby Dec 30 '19 at 16:23
  • I wonder why this is happening.. my connection is stable... are you in VPN? As an alternate try to connect using appid and client secret – SP Expert Dec 30 '19 at 16:50
  • Like below :Connect-PnPOnline -Url $siteUrl -AppId $appId -AppSecret $appSecret – SP Expert Dec 30 '19 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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