forked from BNWEIN/CIPPAPIModule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-AllTenants-Licenses.ps1
70 lines (56 loc) · 2.43 KB
/
Get-AllTenants-Licenses.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
66
67
68
69
70
<#
.SYNOPSIS
This script retrieves a list of tenants and their licenses using the CIPP API.
.DESCRIPTION
The script prompts the user to enter the CIPP API URL, client ID, client secret, and tenant ID.
It then imports the CIPPAPIModule and sets the CIPP API details using the provided inputs.
The script retrieves a list of tenants using the Get-CIPPTenants function.
For each tenant, it retrieves the licenses using the Get-CIPPLicenses function.
The script creates a custom object for each license and outputs the results.
Finally, it exports the results to a CSV file and opens it using the default application.
.PARAMETER CIPPAPIUrl
The URL of the CIPP API.
.PARAMETER CIPPClientID
The client ID for accessing the CIPP API.
.PARAMETER CIPPClientSecret
The client secret for accessing the CIPP API.
.PARAMETER TenantId
The ID of the tenant.
.EXAMPLE
.\Get-AllTenants-Licenses.ps1
.NOTES
This script requires the CIPPAPIModule to be installed.
#>
$CIPPAPIUrl = Read-Host 'Enter the CIPP API URL'
$CIPPClientID = Read-Host 'Enter the CIPP API Client ID'
$CIPPClientSecret = Read-Host 'Enter the CIPP API Client Secret'
$TenantId = Read-Host 'Enter the Tenant ID'
Import-Module CIPPAPIModule
Set-CIPPAPIDetails -CIPPClientID $CIPPClientID -CIPPClientSecret $CIPPClientSecret -CIPPAPIUrl $CIPPAPIUrl -TenantID $TenantId
Write-Output 'Getting List of tenants'
$tenantsList = Get-CIPPTenants
Write-Output 'Getting all customer licenses - This can take a few minutes...'
$CustomerLicenses = $tenantsList.defaultDomainName | ForEach-Object -Parallel {
Import-Module CIPPAPIModule
Set-CIPPAPIDetails -TenantID $using:TenantId -CIPPClientID $using:CIPPClientID -CIPPClientSecret $using:CIPPClientSecret -CIPPAPIUrl $using:CIPPAPIUrl
$tenant = $_
$Licenses = Get-CIPPLicenses -CustomerTenantID $tenant
foreach ($License in $Licenses) {
[PSCustomObject]@{
Tenant = $tenant
License = $License.License
CountUsed = $License.CountUsed
CountAvailable = $License.CountAvailable
TotalLicenses = $License.TotalLicenses
skuid = $License.skuid
skuPartNumber = $License.skuPartNumber
}
}
} -ThrottleLimit 5
if ($null -eq $CustomerLicenses) {
exit
}
$filename = 'CustomerLicenses' + (Get-Date -Format 'yyyy-MM-dd') + '.csv'
$filepath = "$env:TEMP\$($filename)"
$CustomerLicenses | Export-Csv -Path $filepath
Start-Process -FilePath $filepath