none
SharePoint Online Statistics

    Question

  • Hi Guys

    Is there a way in PowerShell, to bring back sites and various stats? Perhaps the amount of sites within, the amount of users, groups, storage etc?

    Thursday, September 15, 2016 8:34 AM

All replies

  • Hi,

    You could use CSOM with powershell to get the properties.

    For example(get site storage):

    #Load SharePoint CSOM Assemblies
    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"
    
    <#
    .Synopsis
        Facilitates the loading of specific properties of a Microsoft.SharePoint.Client.ClientObject object or Microsoft.SharePoint.Client.ClientObjectCollection object.
    .DESCRIPTION
        Replicates what you would do with a lambda expression in C#. 
        For example, "ctx.Load(list, l => list.Title, l => list.Id)" becomes
        "Load-CSOMProperties -object $list -propertyNames @('Title', 'Id')".
    .EXAMPLE
        Load-CSOMProperties -parentObject $web -collectionObject $web.Fields -propertyNames @("InternalName", "Id") -parentPropertyName "Fields" -executeQuery
        $web.Fields | select InternalName, Id
    .EXAMPLE
       Load-CSOMProperties -object $web -propertyNames @("Title", "Url", "AllProperties") -executeQuery
       $web | select Title, Url, AllProperties
    #>
    function global:Load-CSOMProperties {
        [CmdletBinding(DefaultParameterSetName='ClientObject')]
        param (
            # The Microsoft.SharePoint.Client.ClientObject to populate.
            [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0, ParameterSetName = "ClientObject")]
            [Microsoft.SharePoint.Client.ClientObject]
            $object,
    
            # The Microsoft.SharePoint.Client.ClientObject that contains the collection object.
            [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0, ParameterSetName = "ClientObjectCollection")]
            [Microsoft.SharePoint.Client.ClientObject]
            $parentObject,
    
            # The Microsoft.SharePoint.Client.ClientObjectCollection to populate.
            [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 1, ParameterSetName = "ClientObjectCollection")]
            [Microsoft.SharePoint.Client.ClientObjectCollection]
            $collectionObject,
    
            # The object properties to populate
            [Parameter(Mandatory = $true, Position = 1, ParameterSetName = "ClientObject")]
            [Parameter(Mandatory = $true, Position = 2, ParameterSetName = "ClientObjectCollection")]
            [string[]]
            $propertyNames,
    
            # The parent object's property name corresponding to the collection object to retrieve (this is required to build the correct lamda expression).
            [Parameter(Mandatory = $true, Position = 3, ParameterSetName = "ClientObjectCollection")]
            [string]
            $parentPropertyName,
    
            # If specified, execute the ClientContext.ExecuteQuery() method.
            [Parameter(Mandatory = $false, Position = 4)]
            [switch]
            $executeQuery
        )
    
        begin { }
        process {
            if ($PsCmdlet.ParameterSetName -eq "ClientObject") {
                $type = $object.GetType()
            } else {
                $type = $collectionObject.GetType() 
                if ($collectionObject -is [Microsoft.SharePoint.Client.ClientObjectCollection]) {
                    $type = $collectionObject.GetType().BaseType.GenericTypeArguments[0]
                }
            }
    
            $exprType = [System.Linq.Expressions.Expression]
            $parameterExprType = [System.Linq.Expressions.ParameterExpression].MakeArrayType()
            $lambdaMethod = $exprType.GetMethods() | ? { $_.Name -eq "Lambda" -and $_.IsGenericMethod -and $_.GetParameters().Length -eq 2 -and $_.GetParameters()[1].ParameterType -eq $parameterExprType }
            $lambdaMethodGeneric = Invoke-Expression "`$lambdaMethod.MakeGenericMethod([System.Func``2[$($type.FullName),System.Object]])"
            $expressions = @()
    
            foreach ($propertyName in $propertyNames) {
                $param1 = [System.Linq.Expressions.Expression]::Parameter($type, "p")
                try {
                    $name1 = [System.Linq.Expressions.Expression]::Property($param1, $propertyName)
                } catch {
                    Write-Error "Instance property '$propertyName' is not defined for type $type"
                    return
                }
                $body1 = [System.Linq.Expressions.Expression]::Convert($name1, [System.Object])
                $expression1 = $lambdaMethodGeneric.Invoke($null, [System.Object[]] @($body1, [System.Linq.Expressions.ParameterExpression[]] @($param1)))
     
                if ($collectionObject -ne $null) {
                    $expression1 = [System.Linq.Expressions.Expression]::Quote($expression1)
                }
                $expressions += @($expression1)
            }
    
    
            if ($PsCmdlet.ParameterSetName -eq "ClientObject") {
                $object.Context.Load($object, $expressions)
                if ($executeQuery) { $object.Context.ExecuteQuery() }
            } else {
                $newArrayInitParam1 = Invoke-Expression "[System.Linq.Expressions.Expression``1[System.Func````2[$($type.FullName),System.Object]]]"
                $newArrayInit = [System.Linq.Expressions.Expression]::NewArrayInit($newArrayInitParam1, $expressions)
    
                $collectionParam = [System.Linq.Expressions.Expression]::Parameter($parentObject.GetType(), "cp")
                $collectionProperty = [System.Linq.Expressions.Expression]::Property($collectionParam, $parentPropertyName)
    
                $expressionArray = @($collectionProperty, $newArrayInit)
                $includeMethod = [Microsoft.SharePoint.Client.ClientObjectQueryableExtension].GetMethod("Include")
                $includeMethodGeneric = Invoke-Expression "`$includeMethod.MakeGenericMethod([$($type.FullName)])"
    
                $lambdaMethodGeneric2 = Invoke-Expression "`$lambdaMethod.MakeGenericMethod([System.Func``2[$($parentObject.GetType().FullName),System.Object]])"
                $callMethod = [System.Linq.Expressions.Expression]::Call($null, $includeMethodGeneric, $expressionArray)
                
                $expression2 = $lambdaMethodGeneric2.Invoke($null, @($callMethod, [System.Linq.Expressions.ParameterExpression[]] @($collectionParam)))
    
                $parentObject.Context.Load($parentObject, $expression2)
                if ($executeQuery) { $parentObject.Context.ExecuteQuery() }
            }
        }
        end { }
    }
    
    ##Variables for Processing
    $SiteUrl = "https://ssssss.sharepoint.com/sites/dev"
    $UserName="sssssss.onmicrosoft.com"
    $Password ="xxxxxxxx"
    #Setup Credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Context.Credentials = $credentials
    #Get the Usage
    $Site = $Context.Site
    Load-CSOMProperties -object $Site -propertyNames @('Usage')
    $context.ExecuteQuery()
    write-host $Site.Usage.Storage

    Below are some links for your reference:

    https://www.itunity.com/article/completing-basic-operations-sharepoint-csom-api-powershell-1278

    https://gist.github.com/glapointe/cc75574a1d4a225f401b#file-load-csomproperties-ps1

    Best Regards,

    Lee


    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, September 21, 2016 6:15 AM
  • Hi,

    Is any update for your issue?

    Best Regards,

    Lee


    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, September 23, 2016 4:53 AM