-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInvoke-AzRestVmReport.ps1
213 lines (201 loc) · 9.62 KB
/
Invoke-AzRestVmReport.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
204
205
206
207
208
209
210
211
212
213
function Invoke-AzRestVmReport {
[CmdletBinding()]
param(
[PSObject]$token
,
[string[]]$SubscriptionId
,
[switch]$metrics
,
[ValidateRange(1,30)]
[int]$days = 1
)
$splatToken = @{}
If ($token){
$splatToken.token = $token
}Else{
$splatToken.token = Get-AzRestToken
}
$AzSubscription = Get-AzRestSubscription -token $token
If ($null -eq $SubscriptionId){
$SubscriptionId = $AzSubscription.SubscriptionId
}
foreach ($sid in $SubscriptionId){
$splatSubscriptionId = @{'SubscriptionId' = $sid}
$AzVm = Get-AzRestVm @splatToken @splatSubscriptionId -Status
$SqlVirtualMachines = Get-AzRestSqlVirtualMachines @splatToken @splatSubscriptionId
$AzDisks = Get-AzRestDisk @splatToken @splatSubscriptionId
$AzNetworkInterface = Get-AzRestNetworkInterface @splatToken @splatSubscriptionId
$AzPublicIpAddress = Get-AzRestPublicIpAddress @splatToken @splatSubscriptionId
foreach ($vm in $AzVm) {
#network
$nics = $vm.properties.networkprofile.networkinterfaces
$nif = @()
foreach ($n in $nics){
$nopip = $false
$nic = $AzNetworkInterface | Where-Object {$_.Id -eq $n.id}
If (($nic.properties.Ipconfigurations.properties | Get-Member).Name -contains 'publicIPAddress') {
$validPip = $AzPublicIpAddress | Where-Object {($_.properties | Get-Member).Name -contains 'ipConfiguration'}
$pip = $validPip | Where-Object {$_.properties.ipConfiguration.id -eq $nic.properties.Ipconfigurations.properties.PublicIPAddress.id}
If ($null -eq $pip){
$nopip = $true
}
}
Else {
$nopip = $true
}
if ($nopip){
$pip = @{
properties = [ordered]@{
IpAddress = $null
PublicIpAllocationMethod = $null
}
}
}
Try{ $PrimaryInterace = $nic.properties.Primary }Catch{ $PrimaryInterace = $false }
$nif += [PsCustomObject][ordered]@{
PrimaryInterface = $PrimaryInterace
PrivateIpAddress = $nic.properties.IpConfigurations.properties.PrivateIpAddress
AcceleratedNetworking = $nic.properties.EnableAcceleratedNetworking
PublicIpAddress = $pip.properties.IpAddress
PublicIpAllocationMethod = $pip.properties.PublicIpAllocationMethod
}
}
Write-Verbose "$($vm.Name): Found $($nics.count) networkinterfaces, has public ip: $(-not $nopip)"
#disks
$disks = @()
#werkt alleen als het managed disks zijn!
If (($vm.properties.StorageProfile.OsDisk | Get-Member).Name -contains 'managedDisk') {
$od = $AzDisks | Where-Object {$_.Id -eq $vm.properties.StorageProfile.OsDisk.managedDisk.id}
$disks += [PsCustomObject][ordered]@{
Profile = 'OsDisk'
Type = 'managed'
Name = $od.Name
Lun = $null
DiskSizeGB =$od.properties.DiskSizeGB
DiskIOPSReadWrite = $od.properties.DiskIOPSReadWrite
DiskMBpsReadWrite = $od.properties.DiskMBpsReadWrite
Caching = $vm.properties.storageprofile.osdisk.Caching
}
}Else{
$disks += [PsCustomObject][ordered]@{
Profile = 'OsDisk'
Type = 'vhd'
Name = $vm.properties.StorageProfile.OsDisk.Name
Lun = $null
DiskSizeGB = 0
DiskIOPSReadWrite = 0
DiskMBpsReadWrite = 0
Caching = $vm.properties.storageprofile.osdisk.Caching
}
}
$datadisks = $vm.properties.StorageProfile.datadisks
foreach ($d in $datadisks){
If (($d | Get-Member).Name -contains 'managedDisk') {
$dd = $AzDisks | Where-Object {$_.Id -eq $d.managedDisk.id}
$disks += [PsCustomObject][ordered]@{
Profile = 'DataDisk'
Type = 'managed'
Name = $d.Name
Lun = $d.Lun
DiskSizeGB = $dd.properties.DiskSizeGB
DiskIOPSReadWrite = $dd.properties.DiskIOPSReadWrite
DiskMBpsReadWrite = $dd.properties.DiskMBpsReadWrite
Caching = $d.Caching
}
}Else{
$disks += [PsCustomObject][ordered]@{
Profile = 'DataDisk'
Type = 'vhd'
Name = $d.Name
Lun = $null
DiskSizeGB = 0
DiskIOPSReadWrite = 0
DiskMBpsReadWrite = 0
Caching = $d.Caching
}
}
}
Write-Verbose "$($vm.Name): Found $($datadisks.count) data disks"
#sql
$sqlinfo = [ordered]@{
sqlImageOffer = $null
sqlServerLicenseType = $null
sqlImageSku = $null
}
$sql = $SqlVirtualMachines | Where-Object {$_.properties.virtualMachineResourceId -eq $vm.Id}
if ($sql){
Try {$sqlinfo.sqlImageOffer = $sql.properties.sqlImageOffer}Catch{$sqlinfo.sqlImageOffer = $null}
Try {$sqlinfo.sqlServerLicenseType= $sql.properties.sqlServerLicenseType}Catch{$sqlinfo.sqlServerLicenseType = $null}
Try {$sqlinfo.sqlImageSku = $sql.properties.sqlImageSku}Catch{$sqlinfo.sqlImageSku= $null}
}
$sqlinfo = [PsCustomObject]$sqlinfo
Write-Verbose "$($vm.Name): Has SQL installed $($sql -and $true)"
#subscription
$SubcrInfo = $AzSubscription | Where-Object {$_.subscriptionId -eq $sid}
$subscriptionInfo = [PsCustomObject]@{
displayName = $SubcrInfo.displayName
subscriptionId = $SubcrInfo.subscriptionId
subscriptionPolicies = $SubcrInfo.subscriptionPolicies
}
#extensions - create some order for viewing
Try{
$IaaSAntimalwareVersion = $vm.instanceview.extensions.Where{$_.Name -eq 'IaaSAntimalware'}.typeHandlerVersion
}
Catch{
$IaaSAntimalwareVersion = $null
}
Try{
$SiteRecoveryVersion = $vm.instanceview.extensions.Where{$_.Name -like 'SiteRecovery-*'}.typeHandlerVersion
}
Catch{
$SiteRecoveryVersion = $null
}
Try{
$SqlIaasExtensionVersion = $vm.instanceview.extensions.Where{$_.Name -eq 'SqlIaasExtension'}.typeHandlerVersion
}
Catch{
$SqlIaasExtensionVersion = $null
}
#remaining extensions
$extensions = $vm.instanceview.extensions.Where{($_.Name -ne 'IaaSAntimalware') -and ($_.Name -ne 'SqlIaasExtension') -and ($_.Name -notlike 'SiteRecovery-*') }
#agregate
$AzVMInfo = [ordered]@{
Subscription = $subscriptionInfo
ResourceGroupName = $vm.id.split('/')[4]
Name = $vm.Name
Status = $vm.instanceview.statuses.where{$_.code -like 'PowerState*'}.displaystatus
osType = $vm.properties.StorageProfile.OsDisk.OSType
osName = $null
osVersion = $null
Location = $vm.Location
#LicenseType = $vm.properties.LicenseType
VmSize = $vm.properties.hardwareprofile.vmsize
nic = $nif
disk = $disks
sql = $sqlinfo
extensionIaaSAntimalwareVersion = $IaaSAntimalwareVersion
SqlIaasExtensionVersion = $SqlIaasExtensionVersion
extensionSiteRecoveryVersion = $SiteRecoveryVersion
otherExtensions = $extensions
}
$PreviousErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'
If ($vm.properties.diagnosticsProfile.bootDiagnostics.storageUri){$AzVMInfo.bootDiagnostics = $vm.properties.diagnosticsProfile.bootDiagnostics.storageUri}Else{$AzVMInfo.bootDiagnostics = $null}
$VmInstanceView = Get-AzRestVmInstanceView -vmAzResourceId $vm.Id
If ($VmInstanceView.HyperVGeneration){$AzVMInfo.HyperVGeneration = $VmInstanceView.HyperVGeneration}
If ($VmInstanceView.osName){$AzVMInfo.osName = $VmInstanceView.osName}
If ($VmInstanceView.osVersion){$AzVMInfo.osVersion = $VmInstanceView.osVersion}
$ErrorActionPreference = $PreviousErrorActionPreference
#metrics
If($metrics){
Write-Verbose "$($vm.Name): Collecting metrics...."
$AzVMInfo.Metrics = Invoke-AzRestVmMetricsReport -vm $vm -days $days
}Else{
$AzVMInfo.Metrics = $null
}
#If ($true -eq $VmInstanceView.osVersion){$AzVMInfo.osVersion = $VmInstanceView.osVersion}else{$AzVMInfo.osVersion=$null}
[PsCustomObject]$AzVMInfo
}
}
}