Skip to content

Instantly share code, notes, and snippets.

@exactmike
Last active November 28, 2018 20:58
Show Gist options
  • Select an option

  • Save exactmike/880fd0ad7b4422fc109e1c918f81e380 to your computer and use it in GitHub Desktop.

Select an option

Save exactmike/880fd0ad7b4422fc109e1c918f81e380 to your computer and use it in GitHub Desktop.
Gets OneDrive User Site Information from an Microsoft 365 Tenant for a specified list of users.
#Import the required module
$Module = Get-Module -ListAvailable -Name 'Microsoft.Online.SharePoint.PowerShell' -ErrorAction Stop
if ($Null -eq $Module) {throw ("PowerShell Module Microsoft.Online.SharePoint.PowerShell was not found")}
else {
Import-Module -Name 'Microsoft.Online.SharePoint.PowerShell' -Force -Global -ErrorAction Stop -DisableNameChecking
}
#Get the administratiave credential
#$AdminCredential = Get-Credential -Message "Please provide the SharePoint Online Admin Credential" -ErrorAction Stop
#if ($null -eq $AdminCredential -or $AdminCredential.GetType().name -ne 'PSCredential') {throw ("SharePoint Online Admin Credential Required")}
#Get the Office 365 Tenant Subdomain
$TenantSubdomainFragment = Read-Host -Prompt "Please provide the Office 365 Tenant Subdomain identity. For example, if your tenant is 'contoso.onmicrosoft.com' enter 'contoso'."
if ($TenantSubdomainFragment -like '*.onmicrosoft.com') {$TenantSubdomainFragment = $TenantSubdomainFragment.Split('.')[0]}
#Get the Users to Report On Data File
function Read-OpenFileDialog
{
[cmdletbinding()]
param(
[string]$WindowTitle
,
[string]$InitialDirectory
,
[string]$Filter = 'All files (*.*)|*.*'
,
[switch]$AllowMultiSelect
)
Add-Type -AssemblyName System.Windows.Forms
$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog.Title = $WindowTitle
if ($PSBoundParameters.ContainsKey('InitialDirectory')) { $openFileDialog.InitialDirectory = $InitialDirectory }
$openFileDialog.Filter = $Filter
if ($AllowMultiSelect) { $openFileDialog.MultiSelect = $true }
$openFileDialog.ShowHelp = $true
# Without this line the ShowDialog() function may hang depending on system configuration and running from console vs. ISE.
$result = $openFileDialog.ShowDialog()
switch ($Result)
{
'OK'
{
if ($AllowMultiSelect)
{
$openFileDialog.Filenames
}
else
{
$openFileDialog.Filename
}
}
'Cancel'
{
}
}
$openFileDialog.Dispose()
Remove-Variable -Name openFileDialog
}#Read-OpenFileDialog
$File = Read-OpenFileDialog -WindowTitle 'Select CSV File to Process. Must Contain UserPrincipalName Column.'
$IdentitiesToProcess = @(
Import-CSV -Path $File -ErrorAction Stop
)
#Configure the Output File Name and Path
$TimeStamp = Get-Date -Format yyyyMMdd-HHmmss
$OutputFilePath = Join-Path -Path $(Split-Path -path $File -Parent) -ChildPath "$($timeStamp)OneDriveUsersReport.csv"
Write-Verbose -Message "This script will output the OneDrive Users Report File to $OutputFilePath" -Verbose
#setup the OneDrive User site information retrieval function
function Get-OneDriveUserSite
{
[cmdletbinding()]
param(
[parameter()]
[pscredential]$Credential
,
[parameter(Mandatory)]
[string]$TenantName #subdomain of tenant, if tenant is contoso.onmicrosoft.com, use value 'contoso'
,
[parameter(Mandatory, ValueFromPipeline)]
[string[]]$UserPrincipalName
)
Begin
{
$TenantSharepointAdminURL = "https://$TenantName-admin.sharepoint.com"
Connect-SPOService -Url $TenantSharepointAdminURL -ErrorAction Stop #-Credential $Credential
$GetSiteErrorsCount = 0
$UPNCounter = 0
}
Process
{
foreach ($o in $UserPrincipalName)
{
switch ($PSCmdlet.MyInvocation.ExpectingInput)
{
$false
{
$UPNCounter++
Write-Progress -Activity 'Getting OneDrive User Site' -CurrentOperation $o -PercentComplete $($UPNCounter/$UserPrincipalName.Count*100) -Status "$UPNCounter of $($UserPrincipalName.Count)"
}
}
$UserURLFragment = ($o -replace '@', '_') -replace '\.', '_'
$UserOneDriveURL = "https://$tenantName-my.sharepoint.com/personal/$UserURLFragment"
try
{
$UserSite = Get-SPOSite -Identity $UserOneDriveURL -Detailed -ErrorAction Stop
$UserSite | Select-Object Owner,Status,LastContentModifiedDate,StorageUsageCurrent,URL,Title
}
catch
{
[PSCustomObject]@{
Owner = $o
Status = 'GetSiteError'
LastContentModifiedDate = $Null
StorageUsageCurrent = $Null
URL = $UserOneDriveURL
Title = $null
}
$GetSiteErrorsCount++
}
}
}
End
{
if ($GetSiteErrorsCount -ge 1)
{
Write-Warning -Message "Encountered $getSiteErrorsCount Errors retrieving User One Drive Site(s). Status of output for these users is 'GetSiteError'."
}
}
}
#Get the OneDrive User Site Information
$UserOneDriveSites = Get-OneDriveUserSite -Credential $AdminCredential -TenantName $TenantSubdomainFragment -UserPrincipalName $($IdentitiesToProcess.UserPrincipalName)
#export the OneDrive User Site Information
$UserOneDriveSites | Export-Csv -Path $OutputFilePath -NoTypeInformation -ErrorAction Stop -Verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment