-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.ps1
208 lines (167 loc) · 8.64 KB
/
report.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Requires Lynx All Access and Caretaker permissions to run properly
Param (
[Parameter(Mandatory=$true)][string]$tpidInput,
[Parameter(Mandatory=$true)][string]$bearer_token
)
# Setting $tagetMode = $true will only return orgs that own at least one E3 or E5 license
$targetMode = $flase
$ids = Get-Content $tpidInput
$currentUTCtime = (Get-Date).ToUniversalTime()
$startDate = $currentUTCtime.AddDays(-30)
$outfileC = "C:\temp\Consolidated-LynxReport-{0}.csv" -f $currentUTCtime.tostring("dd-MM-yyyy-hh-mm-ss")
$newcsv = {} | Select-Object "OrgName","TPID","Customer","Tenant","ID","IsGov", "E3", "E5", "E5 Sec","MDCA","MDI","AADP2","CA-U","MDO-U","MDE-U","MDCA-U","MDI-U","AADP2-U","MDO-P","MDE-P","MDI-P","MDCA-P","AADP2-P"| Export-Csv $outfileC
$csvfileC = Import-Csv $outfileC
function get-licenses($tenantID, $bearerToken){
$method = "GET"
$headers = @{Authorization = "Bearer $bearer_token"}
$url = "https://lynx.office.net/api/LynxStorage/TenantSubscriptions?statusFilters%5B%5D%3DActive&omsTenantId={0}&includeInformationWorkerSubscriptions=false" -f $tenantID
# Get Licenses
$resLic = Invoke-RestMethod -Uri $url -Method $method -Headers $headers
return $resLic
}
function get-tenants($tpid, $bearerToken){
$method = "POST"
$headers = @{Authorization = "Bearer $bearer_token"}
$tpidarray = @($tpid)
$payload = @{
"TopParentOrgIds" = $tpidarray
"ShowDeletedTenants" = $false
}
$payload = $payload | ConvertTo-Json
$urlt = "https://lynx.office.net/api/Search/Tenants?SearchTerm=*"
$resTenants = Invoke-RestMethod -uri $urlt -Method $method -Headers $headers -Body $payload -ContentType "application/json"
return $resTenants
}
function get-usagestats ($tenantID, $bearerToken, $workload, $et, $startDate, $enddate){
$method = "GET"
$headers = @{Authorization = "Bearer $bearerToken"}
$url = "https://lynx.office.net/api/ApplicationUsage/AllUpHistory?omsTenantId={0}&workloads%5B0%5D={1}&startDate={2}&endDate={3}&usageType=RL28&entityType={4}" -f $tenantID,$workload,$startDate,$enddate,$et
try{
$results = Invoke-RestMethod -Uri $url -Method $method -Headers $headers -ContentType "application/json" -UseBasicParsing
return $results
} catch {
"Error pulling {0} Data, could be no vaild results: {1}" -f $workload,$resLic.statuscode | Write-Host
return $null
}
}
function get-featurestats ($tenantID, $bearerToken, $workload, $et, $startDate, $enddate){
$method = "GET"
$headers = @{Authorization = "Bearer $bearerToken"}
$url = "https://lynx.office.net/api/ApplicationUsage/History?omsTenantId={0}&workloads%5B0%5D={1}&startDate={2}&endDate={3}&usageType=RL28&entityType={4}" -f $tenantID,$workload,$startDate,$enddate,$et
try{
$results = Invoke-RestMethod -Uri $url -Method $method -Headers $headers -ContentType "application/json" -UseBasicParsing
return $results
} catch {
"Error pulling {0} Data, could be no vaild results: {1}" -f $workload,$resLic.statuscode | Write-Host
return $null
}
}
function get-usagePercent($licNum,$usageNum){
$percent = 0
if($licNum -eq 0){
return 0
}
try {
$percent = $usageNum/$licNum
$a = [math]::Round($percent,2)
return $a
} catch {
return 0
}
}
foreach($id in $ids){
Write-Host "TPID: $id------------------------------------------------------------"
$et = $workload = ""
$E3 = $E5 = $E5Sec = $AADP2 = $MDCA = $MDI = $MDATPAverage = $OATPAverage = $AADPAverage = 0
#
# Customer Name and Tenants
$tenants = get-tenants -tpid $id -bearerToken $bearer_token
foreach ($cxtenant in $tenants.Results.Document){
$csvfileC.TPID = $id
$csvfileC.Customer = $cxtenant.MSSalesTopParentOrgName
$csvfileC.Tenant = $cxtenant.DefaultDomain
$csvfileC.OrgName = $cxtenant.Name
$csvfileC.Id = $cxtenant.OmsTenantId
$csvfileC.IsGov = $cxtenant.IsGov
Write-Host $cxtenant.Name
$tenLics = get-licenses -tenantID $cxtenant.OmsTenantId -bearerToken $bearer_token
# Extract Licensing Data
foreach($lic in $tenLics){
if($lic.StateName = "Active" -and $lic.SubscriptionEndDate -gt $currentUTCtime){
switch -wildcard ( $lic.OfferProductName )
{
'* 365 *3*' { $E3 += $lic.IncludedQuantity }
'* 365 *5*' { $E5 += $lic.IncludedQuantity }
'ENTERPRISE MOBILITY + SECURITY*'{ $E5Sec += $lic.IncludedQuantity }
'MICROSOFT DEFENDER FOR CLOUD APPS*' { $MDCA += $lic.IncludedQuantity }
'MICROSOFT DEFENDER FOR IDENTITY*' { $MDI += $lic.IncludedQuantity }
'AZURE ACTIVE DIRECTORY PREMIUM P2*'{ $AADP2 += $lic.IncludedQuantity }
}
}
}
$csvfileC.E3 = $E3
$csvfileC.E5 = $E5
$csvfileC.'E5 Sec' = $E5Sec
$csvfileC.MDCA = $MDCA
$csvfileC.MDI = $MDI
$csvfileC.AADP2 = $AADP2
if ($E5Sec -gt $E5) {
$secLic = $E5Sec
} else {
$secLic =$E5
}
if ($MDI -gt $secLic) {
$mdiLic = $MDI
} else {
$mdiLic = $secLic
}
$1 = get-usagestats -tenantID $cxtenant.OmsTenantId -bearerToken $bearer_token -et "Device" -startDate $startDate -enddate $currentUTCtime -workload "MDATP"
$MDATPAverage = $1.Usage.MDATP | ForEach-Object {$_.Usage} | Measure-Object -Average
$MDEAvg = [math]::Round($MDATPAverage.Average)
$csvfileC.'MDE-U' = $MDEAvg
$percentUsage = get-usagePercent -licNum $secLic -usageNum $MDEAvg
$csvfileC.'MDE-P' = $percentUsage
$2 = get-usagestats -tenantID $cxtenant.OmsTenantId -bearerToken $bearer_token -et "User" -startDate $startDate -enddate $currentUTCtime -workload "OATP"
$OATPAverage = $2.Usage.OATP | ForEach-Object {$_.Usage} | Measure-Object -Average
$MDOAvg = [math]::Round($OATPAverage.Average)
$csvfileC.'MDO-U' = $MDOAvg
$percentUsage = get-usagePercent -licNum $secLic -usageNum $MDOAvg
$csvfileC.'MDO-P' = $percentUsage
$3 = get-usagestats -tenantID $cxtenant.OmsTenantId -bearerToken $bearer_token -et "User" -startDate $startDate -enddate $currentUTCtime -workload "MCAS"
$MCASAverage = $3.Usage.MCAS | ForEach-Object {$_.Usage} | Measure-Object -Average
$MCASAvg = [math]::Round($MCASAverage.Average)
$csvfileC.'MDCA-U' = $MCASAvg
$percentUsage = get-usagePercent -licNum $secLic -usageNum $MCASAvg
$csvfileC.'MDCA-P' = $percentUsage
$4 = get-usagestats -tenantID $cxtenant.OmsTenantId -bearerToken $bearer_token -et "User" -startDate $startDate -enddate $currentUTCtime -workload "AATP"
$AATPAverage = $4.Usage.AATP | ForEach-Object {$_.Usage} | Measure-Object -Average
$MDIAvg = [math]::Round($AATPAverage.Average)
$csvfileC.'MDI-U' = $MDIAvg
$percentUsage = get-usagePercent -licNum $mdiLic -usageNum $MDIAvg
$csvfileC.'MDI-P' = $percentUsage
$5 = get-usagestats -tenantID $cxtenant.OmsTenantId -bearerToken $bearer_token -et "User" -startDate $startDate -enddate $currentUTCtime -workload "AADP"
$AADPAverage = $5.Usage.AADP | ForEach-Object {$_.Usage} | Measure-Object -Average
$AADPAvg = [math]::Round($AADPAverage.Average)
$csvfileC.'AADP2-U' = $AADPAvg
$percentUsage = get-usagePercent -licNum $secLic -usageNum $AADPAvg
$csvfileC.'AADP2-P' = $percentUsage
$6 = get-featurestats -tenantID $cxtenant.OmsTenantId -bearerToken $bearer_token -et "User" -startDate $startDate -enddate $currentUTCtime -workload "AADP"
$3PCA_Average = $6.Usage.AADP.Data.Feature | ForEach-Object {$_.ThirdParty} | Measure-Object -Average
$3PCA_AVg = [math]::Round($3PCA_Average.Average)
$csvfileC.'CA-U' = $3PCA_AVg
if($targetMode){
if($E3 -eq 0 -and $E5 -eq 0){
$E3 = $E5 = $E5Sec = $secLic = $AADP2 = $MDCA = $MDI = $MDATPAverage = $OATPAverage = $AADPAverage = 0
} else {
$csvfileC | Export-Csv $outfileC -Append
}
} else {
$csvfileC | Export-Csv $outfileC -Append
}
# Reset the variables
$E3 = $E5 = $E5Sec = $secLic = $AADP2 = $MDCA = $MDI = $MDATPAverage = $OATPAverage = $AADPAverage = 0
}
# Remove any blank lines from the CSV file
$cleanFileC = Import-Csv $outfileC | Where-Object 'Customer' -ne ''
$cleanFileC | Export-Csv $outfileC
}