Advertisement
Rakkii

Get-M365Info.ps1

Mar 26th, 2024 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.     .Description
  3.         Script to gather information of a clients M365 setup, potentially for use with migration of tenants.
  4.        
  5.     .Author
  6.         Kurt Mitchell
  7.  
  8.     .Version
  9.         1.0.0
  10.  
  11.     .Explanation
  12.         Running the script as is, without indicating the two parameters will ask for those parameters to be filled in.
  13.         Clientname is used to name the Excel file that is generated by this script. IE If Contoso is used, the name of the Excel file will include Contoso in it.
  14.         TenantName is the name of the SharePoint Online tenant. It is usually the same as the domain name, so with Contoso, it would be contoso.
  15.  
  16.     .Example
  17.         .\Get-M365Info.ps1 -Clientname 'Contoso' -TenantName 'contoso'
  18. #>
  19.  
  20. param(
  21.     [Parameter(Mandatory) ]
  22.     [ValidateNotNullOrEmpty ()]
  23.     [String]$Clientname,
  24.  
  25.     [Parameter(Mandatory) ]
  26.     [ValidateNotNullOrEmpty ()]
  27.     [String]$TenantName
  28. )
  29.  
  30. #Install the required modules if neccessary.
  31. if (Get-Module ExchangeOnlineManagement -ListAvailable) {
  32.     Write-Host "ExchangeOnlineManagement module is already installed."
  33. } else {
  34.     # Install ExchangeOnlineManagement module
  35.     try {
  36.         Install-Module ExchangeOnlineManagement -Force -Scope CurrentUser
  37.         Write-Host "ExchangeOnlineManagement module installed successfully."
  38.     } catch {
  39.         Write-Host "Error installing ExchangeOnlineManagement module. Please check your internet connection and try again."
  40.     }
  41. }
  42.  
  43. if (Get-Module ImportExcel -ListAvailable) {
  44.     Write-Host "ImportExcel module is already installed."
  45. } else {
  46.     # Install ImportExcel module
  47.     try {
  48.         Install-Module ImportExcel -Force -Scope CurrentUser
  49.         Write-Host "ImportExcel module installed successfully."
  50.     } catch {
  51.         Write-Host "Error installing ImportExcel module. Please check your internet connection and try again."
  52.     }
  53. }
  54.  
  55. if (Get-Module Microsoft.Online.SharePoint.PowerShell -ListAvailable) {
  56.     Write-Host "Microsoft.Online.SharePoint.PowerShell module is already installed."
  57. } else {
  58.     # Install Microsoft.Online.SharePoint.PowerShell module
  59.     try {
  60.         Install-Module Microsoft.Online.SharePoint.PowerShell -Force -Scope CurrentUser
  61.         Write-Host "Microsoft.Online.SharePoint.PowerShell module installed successfully."
  62.     } catch {
  63.         Write-Host "Error installing Microsoft.Online.SharePoint.PowerShell module. Please check your internet connection and try again."
  64.     }
  65. }
  66.  
  67. if (Get-Module Microsoft.Graph -ListAvailable) {
  68.     Write-Host "Microsoft.Graph module is already installed."
  69. } else {
  70.     # Install Microsoft.Graph module
  71.     try {
  72.         Install-Module Microsoft.Graph -Force -Scope CurrentUser
  73.         Write-Host "Microsoft.Graph module installed successfully."
  74.     } catch {
  75.         Write-Host "Error installing Microsoft.Graph module. Please check your internet connection and try again."
  76.     }
  77. }
  78.  
  79. # Import required modules
  80. Import-Module ExchangeOnlineManagement
  81. Import-Module ImportExcel
  82. Import-Module Microsoft.Online.SharePoint.PowerShell
  83. Import-Module Microsoft.Graph.Users
  84. Import-Module Microsoft.Graph.Applications
  85.  
  86. # Connect to Exchange Online
  87. Connect-ExchangeOnline
  88.  
  89. # Connect to MS Online
  90. Connect-SPOService -Url https://$TenantName-admin.sharepoint.com
  91.  
  92. # Connect to Microsoft Graph
  93. Connect-MgGraph -NoWelcome -Scopes Application.Read.All, User.Read.All
  94.  
  95. # Specify the output Excel file path
  96. $ExcelFilePath = "$env:USERPROFILE\downloads\$clientname-M365Info.xlsx"
  97.  
  98. # Get mailboxes
  99. $Mailboxes = Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName, Alias, UserPrincipalName, PrimarySmtpAddress
  100. $Mailboxes | Export-Excel -path $ExcelFilePath -AutoSize -WorksheetName "Mailboxes" -Title "Mailboxes" -TitleBold -TableName "Mailboxes" -TableStyle Medium9
  101.  
  102. # Get mail transport rules
  103. $MailTransportRules = Get-TransportRule | Select-Object Name, Priority, Enabled, Comments
  104. $MailTransportRules | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "Mail Transport Rules" -Title "Transport Rules" -TitleBold -TableName "TransportRules" -TableStyle Medium9
  105.  
  106. # Get shared mailboxes
  107. $SharedMailboxes = Get-Mailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited | Select-Object DisplayName, Alias, UserPrincipalName, PrimarySmtpAddress
  108. $SharedMailboxes | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "Shared Mailboxes" -Title "Shared Mailboxes" -TitleBold -TableName "SharedMailboxes" -TableStyle Medium9
  109.  
  110. # Get distribution lists
  111. $DistributionLists = Get-DistributionGroup -ResultSize Unlimited | Select-Object DisplayName, Alias, PrimarySmtpAddress
  112. $DistributionLists | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "Distribution Lists" -Title "Distribution Lists" -TitleBold -TableName "DistributionLists" -TableStyle Medium9
  113.  
  114. # Get Microsoft 365 groups
  115. $M365Groups = Get-UnifiedGroup -ResultSize Unlimited | Select-Object DisplayName, Alias, PrimarySmtpAddress
  116. $M365Groups | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "M365 Groups" -Title "M365 Groups" -TitleBold -TableName "M365Groups" -TableStyle Medium9
  117.  
  118. # Get OneDrive sizes
  119. $OneDrives = Get-SPOSite -IncludePersonalSite $True -Limit All -Filter "Url -like '-my.sharepoint.com/personal/'"
  120. $ODResult = @()
  121. ForEach ($OneDrive in $OneDrives) {
  122.     $OneDrive = [PSCustomObject]@{
  123.         Email       = $OneDrive.Owner
  124.         URL         = $OneDrive.URL
  125.         QuotaGB     = [Math]::Round($OneDrive.StorageQuota / 1024, 3)
  126.         UsedGB      = [Math]::Round($OneDrive.StorageUsageCurrent / 1024, 3)
  127.         PercentUsed = [Math]::Round(($OneDrive.StorageUsageCurrent / $OneDrive.StorageQuota * 100), 3)
  128.     }
  129.     $ODResult += $OneDrive
  130. }
  131. $ODResult | Format-Table Email,URL,UsedGB,QuotaGB,PercentUsed -AutoSize | Out-Null
  132.  
  133. $ODResult | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "OneDrive Sizes" -Title "OneDrive Sizes" -TitleBold -TableName "OneDriveSizes" -TableStyle Medium9
  134.  
  135. # Get SharePoint size
  136. $SharePointSites = Get-SPOSite | Select-Object Title, Url, StorageUsageCurrent, Owner
  137. $SharePointSites | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "SharePoint Site Sizes" -Title "SharePoint Site Sizes" -TitleBold -TableName "SharePointSiteSizes" -TableStyle Medium9
  138.  
  139. # Get Assigned Licenses
  140. # Get all users in your tenant
  141. $allUsers = Get-MgUser -All
  142.  
  143. # Initialize an empty array to store license details
  144. $licenseDetails = @()
  145.  
  146. # Loop through each user
  147. foreach ($user in $allUsers) {
  148.     $userPrincipalName = $user.UserPrincipalName
  149.  
  150.     # Get license details for the user
  151.     $userLicenses = Get-MgUserLicenseDetail -UserId $userPrincipalName
  152.  
  153.     # Add user license details to the array
  154.     foreach ($license in $userLicenses) {
  155.         $licenseDetails += [PSCustomObject]@{
  156.             UPN = $userPrincipalName
  157.             License = $license.SkuPartNumber
  158.         }
  159.     }
  160. }
  161.  
  162. # Display the results
  163. $licenseDetails | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "Assigned Licenses" -Title "Assigned Licenses" -TitleBold -TableName "AsssignedLicenses" -TableStyle Medium9
  164.  
  165. # Get a list of Registered Enterprise Applications using Microsoft Graph.
  166. $Apps = Get-MgApplication | Select-Object DisplayName, Id, AppId, CreatedDateTime
  167. $Apps | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "Reg. Enterprise Applications" -Title "Registered Enterprise Applications" -TitleBold -TableName "RegisteredEnterpriseApplications" -TableStyle Medium9
Tags: powershell
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement