Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$AdminUrl = "https://tenant-admin.sharepoint.com/"
$UserName = "username@tenant.onmicrosoft.com"
$Password = "password"
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $SecurePassword
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
function Get-SPOWebs(){
param(
$Url = $(throw "Please provide a Site Collection Url"),
$Credential = $(throw "Please provide a Credentials")
)
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = $Credential
$web = $context.Web
$context.Load($web)
$context.Load($web.Webs)
$context.ExecuteQuery()
foreach($web in $web.Webs)
{
Get-SPOWebs -Url $web.Url -Credential $Credential
$web
}
}
#Retrieve all site collection infos
Connect-SPOService -Url $AdminUrl -Credential $Credentials
$sites = Get-SPOSite
#Retrieve and print all sites
foreach ($site in $sites)
{
Write-Host 'Site collection:' $site.Url
$AllWebs = Get-SPOWebs -Url $site.Url -Credential $SPOCredentials
$AllWebs | %{ Write-Host $_.Title }
Write-Host '-----------------------------'
}
$AllWebs = Get-SPOWebs -Url 'https://tenant.sharepoint.com' -Credential $SPOCredentials
$AllWebs | %{ Write-Host $_.Title }
@kendomen

This comment has been minimized.

Copy link

kendomen commented Nov 11, 2015

Great stuff!

@EAMOOLMAN

This comment has been minimized.

Copy link

EAMOOLMAN commented Feb 10, 2016

Thanks :)

@EAMOOLMAN

This comment has been minimized.

Copy link

EAMOOLMAN commented Feb 10, 2016

Do you perhaps know how to include $web.WorkflowAssociations ?

I can only assume I'm going to spend some time to load the context of the workflow?

An error occurred while enumerating through a collection: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested..
At line:1 char:1

  • $web.WorkflowAssociations
  • - CategoryInfo          : InvalidOperation: (Microsoft.Share...lowAssociation]:<GetEnumerator>d__0) [], RuntimeException
    - FullyQualifiedErrorId : BadEnumeration
    
@umananan

This comment has been minimized.

Copy link

umananan commented Dec 24, 2019

Getting this error , any Idea what needs to be changed (line 33 is - Connect-SPOService -Url $AdminUrl -Credential $Credentials)
Connect-SPOService : Could not connect to SharePoint Online.
At C:\Users\user1\Desktop\SPO scripts\Print-SPOSites.ps1:33 char:1

  • Connect-SPOService -Url $AdminUrl -Credential $Credentials
  •   + CategoryInfo          : NotSpecified: (:) [Connect-SPOService], InvalidOperationException
      + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.Online.SharePoint.PowerShell.ConnectSPOService
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.