Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .Description
- Script to gather information of a clients M365 setup, potentially for use with migration of tenants.
- .Author
- Kurt Mitchell
- .Version
- 1.0.0
- .Explanation
- Running the script as is, without indicating the two parameters will ask for those parameters to be filled in.
- 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.
- 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.
- .Example
- .\Get-M365Info.ps1 -Clientname 'Contoso' -TenantName 'contoso'
- #>
- param(
- [Parameter(Mandatory) ]
- [ValidateNotNullOrEmpty ()]
- [String]$Clientname,
- [Parameter(Mandatory) ]
- [ValidateNotNullOrEmpty ()]
- [String]$TenantName
- )
- #Install the required modules if neccessary.
- if (Get-Module ExchangeOnlineManagement -ListAvailable) {
- Write-Host "ExchangeOnlineManagement module is already installed."
- } else {
- # Install ExchangeOnlineManagement module
- try {
- Install-Module ExchangeOnlineManagement -Force -Scope CurrentUser
- Write-Host "ExchangeOnlineManagement module installed successfully."
- } catch {
- Write-Host "Error installing ExchangeOnlineManagement module. Please check your internet connection and try again."
- }
- }
- if (Get-Module ImportExcel -ListAvailable) {
- Write-Host "ImportExcel module is already installed."
- } else {
- # Install ImportExcel module
- try {
- Install-Module ImportExcel -Force -Scope CurrentUser
- Write-Host "ImportExcel module installed successfully."
- } catch {
- Write-Host "Error installing ImportExcel module. Please check your internet connection and try again."
- }
- }
- if (Get-Module Microsoft.Online.SharePoint.PowerShell -ListAvailable) {
- Write-Host "Microsoft.Online.SharePoint.PowerShell module is already installed."
- } else {
- # Install Microsoft.Online.SharePoint.PowerShell module
- try {
- Install-Module Microsoft.Online.SharePoint.PowerShell -Force -Scope CurrentUser
- Write-Host "Microsoft.Online.SharePoint.PowerShell module installed successfully."
- } catch {
- Write-Host "Error installing Microsoft.Online.SharePoint.PowerShell module. Please check your internet connection and try again."
- }
- }
- if (Get-Module Microsoft.Graph -ListAvailable) {
- Write-Host "Microsoft.Graph module is already installed."
- } else {
- # Install Microsoft.Graph module
- try {
- Install-Module Microsoft.Graph -Force -Scope CurrentUser
- Write-Host "Microsoft.Graph module installed successfully."
- } catch {
- Write-Host "Error installing Microsoft.Graph module. Please check your internet connection and try again."
- }
- }
- # Import required modules
- Import-Module ExchangeOnlineManagement
- Import-Module ImportExcel
- Import-Module Microsoft.Online.SharePoint.PowerShell
- Import-Module Microsoft.Graph.Users
- Import-Module Microsoft.Graph.Applications
- # Connect to Exchange Online
- Connect-ExchangeOnline
- # Connect to MS Online
- Connect-SPOService -Url https://$TenantName-admin.sharepoint.com
- # Connect to Microsoft Graph
- Connect-MgGraph -NoWelcome -Scopes Application.Read.All, User.Read.All
- # Specify the output Excel file path
- $ExcelFilePath = "$env:USERPROFILE\downloads\$clientname-M365Info.xlsx"
- # Get mailboxes
- $Mailboxes = Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName, Alias, UserPrincipalName, PrimarySmtpAddress
- $Mailboxes | Export-Excel -path $ExcelFilePath -AutoSize -WorksheetName "Mailboxes" -Title "Mailboxes" -TitleBold -TableName "Mailboxes" -TableStyle Medium9
- # Get mail transport rules
- $MailTransportRules = Get-TransportRule | Select-Object Name, Priority, Enabled, Comments
- $MailTransportRules | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "Mail Transport Rules" -Title "Transport Rules" -TitleBold -TableName "TransportRules" -TableStyle Medium9
- # Get shared mailboxes
- $SharedMailboxes = Get-Mailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited | Select-Object DisplayName, Alias, UserPrincipalName, PrimarySmtpAddress
- $SharedMailboxes | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "Shared Mailboxes" -Title "Shared Mailboxes" -TitleBold -TableName "SharedMailboxes" -TableStyle Medium9
- # Get distribution lists
- $DistributionLists = Get-DistributionGroup -ResultSize Unlimited | Select-Object DisplayName, Alias, PrimarySmtpAddress
- $DistributionLists | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "Distribution Lists" -Title "Distribution Lists" -TitleBold -TableName "DistributionLists" -TableStyle Medium9
- # Get Microsoft 365 groups
- $M365Groups = Get-UnifiedGroup -ResultSize Unlimited | Select-Object DisplayName, Alias, PrimarySmtpAddress
- $M365Groups | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "M365 Groups" -Title "M365 Groups" -TitleBold -TableName "M365Groups" -TableStyle Medium9
- # Get OneDrive sizes
- $OneDrives = Get-SPOSite -IncludePersonalSite $True -Limit All -Filter "Url -like '-my.sharepoint.com/personal/'"
- $ODResult = @()
- ForEach ($OneDrive in $OneDrives) {
- $OneDrive = [PSCustomObject]@{
- Email = $OneDrive.Owner
- URL = $OneDrive.URL
- QuotaGB = [Math]::Round($OneDrive.StorageQuota / 1024, 3)
- UsedGB = [Math]::Round($OneDrive.StorageUsageCurrent / 1024, 3)
- PercentUsed = [Math]::Round(($OneDrive.StorageUsageCurrent / $OneDrive.StorageQuota * 100), 3)
- }
- $ODResult += $OneDrive
- }
- $ODResult | Format-Table Email,URL,UsedGB,QuotaGB,PercentUsed -AutoSize | Out-Null
- $ODResult | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "OneDrive Sizes" -Title "OneDrive Sizes" -TitleBold -TableName "OneDriveSizes" -TableStyle Medium9
- # Get SharePoint size
- $SharePointSites = Get-SPOSite | Select-Object Title, Url, StorageUsageCurrent, Owner
- $SharePointSites | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "SharePoint Site Sizes" -Title "SharePoint Site Sizes" -TitleBold -TableName "SharePointSiteSizes" -TableStyle Medium9
- # Get Assigned Licenses
- # Get all users in your tenant
- $allUsers = Get-MgUser -All
- # Initialize an empty array to store license details
- $licenseDetails = @()
- # Loop through each user
- foreach ($user in $allUsers) {
- $userPrincipalName = $user.UserPrincipalName
- # Get license details for the user
- $userLicenses = Get-MgUserLicenseDetail -UserId $userPrincipalName
- # Add user license details to the array
- foreach ($license in $userLicenses) {
- $licenseDetails += [PSCustomObject]@{
- UPN = $userPrincipalName
- License = $license.SkuPartNumber
- }
- }
- }
- # Display the results
- $licenseDetails | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "Assigned Licenses" -Title "Assigned Licenses" -TitleBold -TableName "AsssignedLicenses" -TableStyle Medium9
- # Get a list of Registered Enterprise Applications using Microsoft Graph.
- $Apps = Get-MgApplication | Select-Object DisplayName, Id, AppId, CreatedDateTime
- $Apps | Export-Excel -Path $ExcelFilePath -AutoSize -Append -WorksheetName "Reg. Enterprise Applications" -Title "Registered Enterprise Applications" -TitleBold -TableName "RegisteredEnterpriseApplications" -TableStyle Medium9
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement