Skip to content

Commit 55b9d06

Browse files
Update AzureData.ps1
Modified script to include vmss and added workaround for stale data
1 parent 0aa3bed commit 55b9d06

File tree

1 file changed

+167
-21
lines changed

1 file changed

+167
-21
lines changed

Azure Data v2/AzureData.ps1

+167-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
22

3-
$appid = "<azure appid here>"
4-
$cert_thumbprint = "<certificate thumbprint here>"
5-
$tenantid = "<azure tenant id here>"
3+
$appid = "<appid_here>"
4+
$cert_thumbprint = "<cert_thumbprint_here>"
5+
$tenantid = "<tenantid_here>"
66

77
function getHeader($identifier, $type){
88
# Generate required headers
@@ -45,7 +45,8 @@ function summarizePerOrg($serverlist, $offlineservers){
4545
[void]$serversummary.Add($obj_summary)
4646
}
4747

48-
# Count organisation tags for deallocated servers.
48+
# Check if there are any Organizations that only have deallocated servers and set their amount to zero,
49+
# this is needed to workaround the fact that DataMiner will hold on to stale data.
4950
foreach ($zeroName in $orgNamesOffline){
5051
foreach ($entry in $serversummary){if($entry.orgName -eq $zeroName){Continue}}
5152
$obj_summary = [PSCustomObject]@{
@@ -58,35 +59,113 @@ function summarizePerOrg($serverlist, $offlineservers){
5859
return $serversummary
5960
}
6061

61-
function summarizePerVMSku($serverlist){
62+
function summarizePerOS($serverlist){
6263
$serversummary = New-Object System.Collections.ArrayList
6364

64-
# Collect data per SKU
65+
# Collect all OS's
66+
$serverlist = $serverlist | Sort-Object OS
67+
$oslist = $($serverlist | Sort-Object OS -Unique).OS
68+
69+
# Grab number of vm's that have a hybrid-license:
70+
$obj_amount = $($serverlist | Where-Object {$_.HybridLicense -eq $True})
71+
if ($obj_amount -is [array]){
72+
$hybrid_amount = $obj_amount.Count
73+
} Else {
74+
$hybrid_amount = 1
75+
}
76+
77+
foreach ($os in $oslist){
78+
# Grab number of vm's running per OS:
79+
$obj_amount = $($serverlist | Where-Object {$_.OS -eq $os})
80+
if ($obj_amount -is [array]){
81+
$amount = $obj_amount.Count
82+
} Else {
83+
$amount = 1
84+
}
85+
86+
if ($os -like "Windows Server*"){
87+
$obj_summary = [PSCustomObject]@{
88+
OS = $os
89+
Amount = $amount
90+
HybridLicensed = $hybrid_amount
91+
}
92+
} Else{
93+
$obj_summary = [PSCustomObject]@{
94+
OS = $os
95+
Amount = $amount
96+
HybridLicensed = 0
97+
}
98+
}
99+
[void]$serversummary.Add($obj_summary)
100+
}
101+
return $serversummary
102+
}
103+
104+
function summarizePerVMSku($serverlist, $reservations, $offlineservers){
105+
$serversummary = New-Object System.Collections.ArrayList
106+
107+
# Collect all sku's
65108
$serverlist = $serverlist | Sort-Object vmsku
66109
$vmskus = $($serverlist | Sort-Object vmsku -Unique).vmsku
67110

68-
# Count skus
111+
# Grab all skus that have deallocated servers
112+
$skusoffline = $($offlineservers | Sort-Object vmsku -Unique).vmsku
113+
69114
foreach ($vmsku in $vmskus){
70-
$numberofreservations = $($reservations | Where-Object {$_.SkuName -eq $vmsku}).Quantity
115+
# Grab reservations cumulated per sku
116+
$numberofreservations = 0
117+
$reservationsforsku = $($reservations | Where-Object {$_.SkuName -eq $vmsku})
118+
if ($reservationsforsku -is [array]){
119+
foreach ($reservationforsku in $reservationsforsku){
120+
$numberofreservations += $reservationforsku.Quantity
121+
}
122+
} Else {
123+
$numberofreservations = $reservationsforsku.Quantity
124+
}
71125
if (-not ($numberofreservations)){$numberofreservations = 0}
126+
127+
# Grab number of vm's per sku
72128
$obj_amount = $($serverlist | Where-Object {$_.vmsku -eq $vmsku})
73129
if ($obj_amount -is [array]){
74130
$amount = $obj_amount.Count
75131
} Else {
76132
$amount = 1
77133
}
78-
79134
$obj_summary = [PSCustomObject]@{
80135
vmsku = $vmsku
81136
amount = $amount
82137
reservations = $numberofreservations
83138
}
84139
[void]$serversummary.Add($obj_summary)
85140
}
141+
142+
# Check if there are any skus that only have deallocated servers and set their amount to zero,
143+
# this is needed to workaround the fact that DataMiner will hold on to stale data.
144+
foreach ($offlinesku in $skusoffline){
145+
if (-not $vmskus.Contains($offlinesku)){
146+
$reservationsforsku = $($reservations | Where-Object {$_.SkuName -eq $offlinesku})
147+
if ($reservationsforsku -is [array]){
148+
foreach ($reservationforsku in $reservationsforsku){
149+
$numberofreservations += $reservationforsku.Quantity
150+
}
151+
} Else {
152+
$numberofreservations = $reservationsforsku.Quantity
153+
}
154+
if (-not ($numberofreservations)){$numberofreservations = 0}
155+
156+
$obj_summary = [PSCustomObject]@{
157+
vmsku = $vmsku
158+
amount = 0
159+
reservations = $numberofreservations
160+
}
161+
[void]$serversummary.Add($obj_summary)
162+
}
163+
}
86164
return $serversummary
87165
}
88166

89-
$server2022VMsList = New-Object System.Collections.ArrayList
167+
$WindowsVMs = New-Object System.Collections.ArrayList
168+
$AllVMs = New-Object System.Collections.ArrayList
90169
$OfflineVMsList = New-Object System.Collections.ArrayList
91170

92171
# Sign in to your Azure account using the certificate
@@ -103,19 +182,70 @@ foreach ($subscription in $subscriptions) {
103182
Set-AzContext -Subscription $subscription -Tenant $tenantid | Out-Null
104183
$subscriptionName = $subscription.Name
105184

185+
Write-Host "Checking VMs in $subscriptionName"
186+
187+
# Get all scale-sets and extract vms from it.
188+
$allVMSS = Get-AzVmss
189+
foreach ($vmss in $allVMSS){
190+
$numberofinstances = $vmss.Sku.Capacity
191+
if ($vmss.VirtualMachineProfile.StorageProfile.ImageReference.id -like "*Ubuntu*"){
192+
$os = "Ubuntu"
193+
}
194+
for ($i=0; $i -lt $numberofinstances; $i++){
195+
$vmData = [PSCustomObject]@{
196+
Subscription = $subscriptionName
197+
ResourceGroupName = $vmss.ResourceGroupName
198+
Name = "$($vmss.Name)_$i"
199+
Location = $vmss.Location
200+
OS = $os
201+
orgName = ""
202+
vmsku = $vmss.Sku.Name
203+
HybridLicense = $False
204+
}
205+
[void]$AllVMs.Add($vmData)
206+
}
207+
}
208+
106209
# Get a list of all virtual machines in the subscription and also explicitly grab the status
107210
$virtualMachines = Get-AzVM -Status
108211

212+
# Grab all VM's
213+
$AllServers = $virtualMachines | Where-Object { ($_.PowerState -eq "VM Running") } #-and ($_.LicenseType -eq "Windows_Server")
214+
109215
# Grab running Windows VM's
110-
$server2022VMs = $virtualMachines | Where-Object { ($_.StorageProfile.OSDisk.OSType -eq "Windows") -and ($_.PowerState -eq "VM Running") -and ($_.OsName -Like "Windows Server*") } #-and ($_.LicenseType -eq "Windows_Server")
111-
216+
$WindowsServers = $virtualMachines | Where-Object { ($_.StorageProfile.OSDisk.OSType -eq "Windows") -and ($_.PowerState -eq "VM Running") -and ($_.OsName -Like "Windows Server*") } #-and ($_.LicenseType -eq "Windows_Server")
217+
112218
# Grab deallocated Windows VM's
113219
$OfflineVMs = $virtualMachines | Where-Object { ($_.StorageProfile.OSDisk.OSType -eq "Windows") -and ($_.PowerState -eq "VM deallocated") }
114220

115-
Write-Host "Found $($server2022VMs.Count) running and $($OfflineVMs.Count) deallocated Windows VM's in $subscriptionName"
221+
Write-Host "Found $($AllServers.Count) VMs running and $($OfflineVMs.Count) VMs deallocated in $subscriptionName"
222+
223+
# Arraylist with all Running VMs
224+
$AllServers | ForEach-Object {
225+
$vmDetails = Get-AzVM -ResourceGroupName $_.ResourceGroupName -Name $_.Name
226+
if ($_.LicenseType -eq "Windows_Server"){
227+
$hybrid = $True
228+
} Else {
229+
$hybrid = $False
230+
}
231+
$vmData = [PSCustomObject]@{
232+
Subscription = $subscriptionName
233+
ResourceGroupName = $_.ResourceGroupName
234+
Name = $_.Name
235+
Location = $_.Location
236+
OS = $_.OsName
237+
orgName = $($vmDetails.Tags.orgName)
238+
vmsku = $_.HardwareProfile.VmSize
239+
HybridLicense = $hybrid
240+
}
241+
if (-not ($_.OsName)){
242+
Write-Host "OSName parameter empty, this is an Azure-bug" -ForegroundColor Red
243+
}
244+
[void]$AllVMs.Add($vmData)
245+
}
116246

117-
# Create arraylist with details for running vms
118-
$server2022VMs | ForEach-Object {
247+
# Arraylist with Running Windows VMs
248+
$WindowsServers | ForEach-Object {
119249
$vmDetails = Get-AzVM -ResourceGroupName $_.ResourceGroupName -Name $_.Name
120250
if ($_.LicenseType -eq "Windows_Server"){
121251
$hybrid = $True
@@ -132,10 +262,10 @@ foreach ($subscription in $subscriptions) {
132262
vmsku = $_.HardwareProfile.VmSize
133263
HybridLicense = $hybrid
134264
}
135-
[void]$server2022VMsList.Add($vmData)
265+
[void]$WindowsVMs.Add($vmData)
136266
}
137267

138-
# Create arraylist with details for deallocated vms
268+
# Arraylist with Deallocated VMs
139269
$OfflineVMs | ForEach-Object {
140270
$vmDetails = Get-AzVM -ResourceGroupName $_.ResourceGroupName -Name $_.Name
141271
if ($_.LicenseType -eq "Windows_Server"){
@@ -162,8 +292,8 @@ foreach ($subscription in $subscriptions) {
162292
# Sign out from your Azure account
163293
Disconnect-AzAccount
164294

165-
# Display summary info
166-
$summaryPerOrg = summarizePerOrg -serverlist $server2022VMsList -offlineservers $OfflineVMsList
295+
# Summarize per org-tag, here we only need the Windows VM's and Deallocated VM's
296+
$summaryPerOrg = summarizePerOrg -serverlist $WindowsVMs -offlineservers $OfflineVMsList
167297
Write-Host "Summary per Organization:"
168298
Write-Host "----------------------------"
169299
foreach ($entry in $summaryPerOrg){
@@ -175,18 +305,27 @@ foreach ($entry in $summaryPerOrg){
175305
}
176306
Write-Host ""
177307

178-
$summaryPerVMSku = summarizePerVMSku -serverlist $server2022VMsList
308+
# Summarize per SKU, here we'll need all VM's, regardless of OS or vmss-instance and offline VMs to workaround the stale-data issue
309+
$summaryPerVMSku = summarizePerVMSku -serverlist $AllVMs -reservations $reservations -offlineservers $OfflineVMsList
179310
Write-Host "Summary per VM Sku:"
180311
Write-Host "----------------------------"
181312
foreach ($entry in $summaryPerVMSku){
182313
Write-Host "$($entry.vmsku): $($entry.amount)"
183314
}
315+
Write-Host ""
184316

317+
# Summarize per SKU, here we'll need all VM's, regardless of OS or vmss-instance
318+
$summaryPerOS = summarizePerOS -serverlist $AllVMs
319+
Write-Host "Summary per VM OS:"
320+
Write-Host "----------------------------"
321+
foreach ($entry in $summaryPerOS){
322+
Write-Host "$($entry.OS): $($entry.amount) (Hybrid Active: $($entry.HybridLicensed))"
323+
}
185324

186325
# Wrap the data, so DataMiner can read it
187326
$header = getHeader -identifier "Azure Data v2" -type "Azure Data v2"
188327
$wrappedData = @{
189-
"All VMs" = $server2022VMsList | ForEach-Object {
328+
"All VMs" = $AllVMs | ForEach-Object {
190329
@{
191330
Subscription = $_.Subscription
192331
ResourceGroupName = $_.ResourceGroupName
@@ -211,6 +350,13 @@ $wrappedData = @{
211350
Reservation = $_.reservations
212351
}
213352
}
353+
"VMs per OS" = $summaryPerOS | ForEach-Object {
354+
@{
355+
ID = $_.OS
356+
Amount = $_.amount
357+
HybridLicensed = $_.HybridLicensed
358+
}
359+
}
214360
}
215361

216362
$body = $wrappedData | ConvertTo-Json

0 commit comments

Comments
 (0)