-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path365SKUTranslator.ps1
65 lines (56 loc) · 2.75 KB
/
365SKUTranslator.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function Get-SubscribedSkusWithMapping {
param (
[string]$ProductsFilePath,
[string]$OutputFilePath
)
Write-Output "Fetching subscribed SKUs from tenant and mapping product names..."
$products = Import-Csv -Path $ProductsFilePath
$results = Get-MgSubscribedSku | Select-Object SkuPartNumber, CapabilityStatus, AppliesTo, ConsumedUnits, ServicePlans
$mappedResults = foreach ($sku in $results) {
$match = $products | Where-Object { $_.String_Id -eq $sku.SkuPartNumber }
$expandedServicePlans = if ($sku.ServicePlans) {
($sku.ServicePlans | ForEach-Object {
$_.ServicePlanName -as [string]
} | Where-Object { $_ } | Sort-Object -Unique) -join "; "
} else {
"None"
}
[PSCustomObject]@{
ProductDisplayName = if ($match) {
($match.Product_Display_Name | Sort-Object -Unique) -join "; "
} else {
"No match found"
}
SkuPartNumber = $sku.SkuPartNumber
CapabilityStatus = $sku.CapabilityStatus
AppliesTo = $sku.AppliesTo
ConsumedUnits = $sku.ConsumedUnits
ServicePlans = $expandedServicePlans
}
}
$mappedResults | Export-Csv -Path $OutputFilePath -NoTypeInformation -Encoding UTF8
Write-Output "Results exported to $OutputFilePath."
return $mappedResults
}
if (Get-Module -ListAvailable -Name "Microsoft.Graph") {
Write-Output "Microsoft.Graph module is installed."
} else {
Write-Output "Microsoft.Graph module is not installed. Installing..."
Install-Module Microsoft.Graph
}
Write-Output "Connecting to Graph with Directory.Read.All and Organization.Read.All permissions..."
try {
Connect-MgGraph -Scopes 'Directory.Read.All','Organization.Read.All' -NoWelcome
} catch {
Write-Output "Failed to connect to Microsoft Graph. Please check your permissions and network connectivity."
Write-Output "Error details: $($_.Exception.Message)"
Write-Output "Script cannot continue without a successful connection to Microsoft Graph. Exiting..."
Exit
}
$defaultDomain = Get-MgDomain | Where-Object { $_.IsDefault -eq $true }
$domainId = $defaultDomain.Id
$domainName = $defaultDomain.Id -split '\.' | Select-Object -First 1
$outputFilePath = ".\$domainName-ProductLicenses.csv"
$productsFilePath = ".\365Products.csv"
$finalResults = Get-SubscribedSkusWithMapping -ProductsFilePath $productsFilePath -OutputFilePath $outputFilePath
$finalResults | Sort-Object -Property ConsumedUnits -Descending | Format-Table -AutoSize