Advertisement
PtiTom

List all O365 storage usage

Jun 8th, 2016
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Please find and replace XXX
  2.  
  3. Import-Module "XXX\Microsoft.SharePoint.Client.dll"
  4. Import-Module "XXX\Microsoft.SharePoint.Client.Runtime.dll"
  5.  
  6. Function Invoke-LoadMethod() {
  7. param(
  8.    [Microsoft.SharePoint.Client.ClientObject]$Object = $(throw "Please provide a Client Object"),
  9.    [string]$PropertyName
  10. )
  11.    $ctx = $Object.Context
  12.    $load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load")
  13.    $type = $Object.GetType()
  14.    $clientLoad = $load.MakeGenericMethod($type)
  15.  
  16.  
  17.    $Parameter = [System.Linq.Expressions.Expression]::Parameter(($type), $type.Name)
  18.    $Expression = [System.Linq.Expressions.Expression]::Lambda(
  19.             [System.Linq.Expressions.Expression]::Convert(
  20.                 [System.Linq.Expressions.Expression]::PropertyOrField($Parameter,$PropertyName),
  21.                 [System.Object]
  22.             ),
  23.             $($Parameter)
  24.    )
  25.    $ExpressionArray = [System.Array]::CreateInstance($Expression.GetType(), 1)
  26.    $ExpressionArray.SetValue($Expression, 0)
  27.    $clientLoad.Invoke($ctx,@($Object,$ExpressionArray))
  28. }
  29.  
  30. # We need some credentials of course
  31. $UserCredential = Get-Credential
  32. Connect-SPOService https://XXX-admin.sharepoint.com -Credential $UserCredential
  33. $myCred = New-Object "Microsoft.SharePoint.Client.SharePointOnlineCredentials" -ArgumentList @($UserCredential.UserName, $UserCredential.Password)
  34.  
  35. # Create the session
  36. $Session = New-PSSession -ConfigurationName Microsoft.Exchange `
  37.     -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
  38.     -Credential $UserCredential `
  39.     -Authentication Basic -AllowRedirection
  40.  
  41. # Import the session
  42. Import-PSSession $Session
  43.  
  44. # Do Stuff...
  45. cls
  46.  
  47. "##### GROUPES O365 #####"
  48. Get-UnifiedGroup | Foreach { $_.DisplayName + " : " + (((Get-SPOSite (Get-UnifiedGroup $_.Name).SharePointSiteUrl).StorageUsageCurrent.ToString()) + " Mo")}
  49.  
  50. "##### Collections de site #####"
  51. $sites = Get-SPOSite
  52. foreach ($s in $sites)
  53. {
  54.     $ctx = New-Object "Microsoft.SharePoint.Client.ClientContext" -ArgumentList @($s.Url)
  55.     $ctx.Credentials = $myCred;
  56.     $si = $ctx.Site
  57.     Invoke-LoadMethod -Object $si -PropertyName "Usage"
  58.     try
  59.     {
  60.         $ctx.ExecuteQuery();
  61.         ($s.Url) + " : " + (($si.Usage.Storage / 1024 / 1024).ToString()) + " Mo"
  62.     }
  63.     catch
  64.     {
  65.         ($s.Url) + " : Exception"
  66.     }
  67.  
  68.     $ctx.Dispose();
  69. }
  70.  
  71.  
  72. # Kill the session
  73. Remove-PSSession $Session
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement