1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| #-------------------------------------------------------------------
#
# Get-SPAppInstances
#
#-------------------------------------------------------------------
Param(
# Ex: https://abc.sharpeoint.com -> abc
[string]$tenantName,
# Ex : user.name@abc.onmicrosoft.com
[string]$username,
# Ex: MyApp
[string]$appName
)
#
# INCLUDES
#
Import-Module "Microsoft.Online.SharePoint.PowerShell"
#
# CONST
#
[bool]$ROOTWEBONLY = $false
[string]$APPTEMPLATENAME = "APP"
#
# FUNCTIONS
#
# Safe execution with incremental backoff
function ExecuteQueryRetry($ctx){
if($ctx -eq $null){
throw "ctx null"
}
$maxRetries = 10
$delay = 500
$currentRetry = 0
while($currentRetry -lt $maxRetries){
try
{
$ctx.ExecuteQuery()
return;
}
catch [Net.WebException]
{
[Net.HttpWebResponse]$ex = $_.Exception -as [Net.HttpWebResponse]
if($ex -and ([int]($ex.StatusCode) -eq 429 -or [int]($ex.StatusCode) -eq 503)){
Write-Host "ExecuteQuery CSOM request frequency exceeded usage limits."
sleep -Milliseconds $delay
$currentRetry += 1
$delay = $delay * 2
}
else{
Write-Host "ExecuteQuery Fatal Error : " $_.Exception.ToString() -ForegroundColor Red
}
}
}
throw "ExecuteQueryRetry FAILED : Too many attempts."
}
function Get-SPOSiteCollections()
{
param(
[string]$tenantAdminUrl= $(throw "Please provide a Tenant admin Url"),
[System.Management.Automation.PSCredential]$adminCredential = $(throw "Please provide a System PSCredential")
)
Write-Host "Retrieving all site collections for tenant: $tenantAdminUrl "
Connect-SPOService -Url $tenantAdminUrl -Credential $adminCredential
$numSites = 0
Get-SPOSite | %{
$numSites += 1
$_.Url
}
Write-Host "Site collections found: $numSites" -ForegroundColor Green
}
function Get-SPOWebs(){
param(
$Url = $(throw "Please provide a Site Collection Url"),
$Credential = $(throw "Please provide a Credentials"),
[Microsoft.SharePoint.Client.SubwebQuery] $query = $null
)
Write-Host "Reading Web: $Url"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = $Credential
[Microsoft.SharePoint.Client.Web]$web = $context.Web
$webCollection = $web.GetSubwebsForCurrentUser($query)
$context.Load($webCollection)
ExecuteQueryRetry($context)
Write-Host "Found webs : " $webCollection.Count
return $webCollection
}
function Get-SPOAppWebs(){
param(
[string]$Url = $(throw "Please provide a Site Collection Url"),
[Microsoft.SharePoint.Client.SharePointOnlineCredentials]$Credential = $(throw "Please provide a Credentials"),
[System.Guid]$productId = $(throw "Please provide an app ProductId")
)
Write-Host "Reading app in Web: $Url"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = $Credential
[Microsoft.SharePoint.Client.Web]$web = $context.Web
$appinstances = $web.GetAppInstancesByProductId($productId)
$context.Load($appinstances)
ExecuteQueryRetry($context)
Write-Host "Found instances : " $appinstances.Count
return $appinstances
}
function Get-SPOAppProductId()
{
param(
[string]$Url = $(throw "Please provide a Tenant admin url"),
[System.Management.Automation.PSCredential]$Credential = $(throw "Please provide a System PSCredential"),
[string]$applicationName = $(throw "Please provide an application name")
)
Connect-SPOService -Url $Url -Credential $Credential
$app = (Get-SPOAppInfo -Name $applicationName | Select -First 1)
if(-not $app){
return $null
}
Write-Host "App found : " ($app | Select *) -ForegroundColor Green
$appProductId = $app.ProductId
return $appProductId
}
#
#
# SCRIPT START
#
#
# The password for the tenant administration
$AdminPassword = Read-Host -Prompt "Enter password" -AsSecureString
# The main tenant admin url
$Url = "https://" + $tenantName + "-admin.sharepoint.com/"
# We need two sets of credential object, but it is the same user/pwd
$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $AdminPassword
$spocredential = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $AdminPassword)
# Product id retrieval. If the appli is not installed in the farm, should be null
$productId = Get-SPOAppProductId -Url $Url -Credential $credential -applicationName $appName
if(-not $productId){
Write-Host "ProductId null, appname invalid or not installed ???" -ForegroundColor Red
return
}
# Set in the CONST section for what type of search you want
if($ROOTWEBONLY){
# Searches only the main site of the site collection.
# This can return a lot more information, but consumes more ExecuteQuery calls
# All site collections
(Get-SPOSiteCollections -tenantAdminUrl $Url -adminCredential $credential) | %{
# Instanciate each Root Web and find in its app sites
$siteUrl = $_
Get-SPOAppWebs -Url $siteUrl -Credential $spocredential -productId $productId | %{
$appWeb = $_
# Returns the site url containing the app instance
$siteUrl
}
}
}
else{
# Searches all the sites of the site collection.
# This scope is broader, but in order not to consume ExecuteQuery calls, only works with the site url
# All site collections
(Get-SPOSiteCollections -tenantAdminUrl $Url -adminCredential $credential) | %{
# Instanciate each Root Web and find in its app sites
$siteUrl = $_
Get-SPOWebs -Url $siteUrl -Credential $spocredential | %{
[Microsoft.SharePoint.Client.Web]$undefinedWeb = $_
# Installed apps sites check
if($undefinedWeb.WebTemplate -eq $APPTEMPLATENAME){
Write-Host $undefinedWeb.Url -ForegroundColor Gray
$appWeb = $undefinedWeb
# Check url ends with appname
if($appWeb.Url -like ("*/" + $appName) ){
$parentWebUrl = ($appWeb.Url -replace "((.+($tenantName))(-.+)(\.sharepoint.com/.+)(/$appName))",'$2$5')
# Returns the site url containing the app instance into the pipeline
$parentWebUrl
}
}
}
}
} |
Partager