-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew-AzInfo.ps1
183 lines (153 loc) · 6.35 KB
/
New-AzInfo.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
<#
.SYNOPSIS
Script starts AzInfo which gathers cross subscription configuration details by resource group
.NOTES
AzInfo typically writes temp data to a folder of your choice i.e. C:\temp. It also zips up the final results.
#>
param (
$ConfigLabel = "AllSubsAndRGs"
)
If ((Get-Command Get-AutomationConnection -ErrorAction SilentlyContinue)) {
$AzureAutomation = $true
try {
Write-Output "Found Azure Automation commands, checking for Azure RunAs Connection..."
# Attempts to use the Azure Run As Connection for automation
$svcPrncpl = Get-AutomationConnection -Name "AzureRunAsConnection"
$tenantId = $svcPrncpl.tenantId
$appId = $svcPrncpl.ApplicationId
$crtThmprnt = $svcPrncpl.CertificateThumbprint
Add-AzAccount -ServicePrincipal -TenantId $tenantId -ApplicationId $appId -CertificateThumbprint $crtThmprnt -EnvironmentName AzureUsGovernment # | Out-Null
}
catch {Write-Error -Exception "Azure RunAs Connection Failure" -Message "Unable to use Azure RunAs Connection" -Category "OperationStopped" -ErrorAction Stop}
}
Else {Write-Output ("Azure Automation commands missing, skipping Azure RunAs Connection...")}
Write-Output "$(Get-Date -Format yyyy-MM-ddTHH.mm.ss) Starting..."
$VerbosePreference = "Continue"
Import-Module .\Modules\AzInfo -Force
$VerbosePreference = "SilentlyContinue"
Import-Module Az
$VerbosePreference = "Continue"
# Find TempPath for local files
$TempPath = If ($AzureAutomation) {$env:Temp}
Else {"C:\Temp"}
Write-Output "$(Get-Date -Format yyyy-MM-ddTHH.mm.ss) TempPath: $($TempPath )"
Write-Output "$(Get-Date -Format yyyy-MM-ddTHH.mm.ss) ConfigLabel: $($ConfigLabel)"
Switch ($ConfigLabel) {
AllSubsAndRGs {
Write-Output "$(Get-Date -Format yyyy-MM-ddTHH.mm.ss) Gathering Sub and RG Info..."
$SubsAll = Get-AzSubscription
$RGsAll = @()
foreach ( $Sub in $SubsAll ) {
Set-AzContext -SubscriptionId $Sub.SubscriptionId | Out-Null
$SubRGs = Get-AzResourceGroup
$RGsAll += $SubRGs
}
$Subs = $SubsAll
$RGs = $RGsAll
Write-Output "$(Get-Date -Format yyyy-MM-ddTHH.mm.ss) Gathering Sub and RG Info - Done! Total Subs:$($Subs.Count) RGs:$($RGs.Count)"
$ScriptControl = @{
GetAzInfo = @{
Execute = $true
Params = @{
Subscription = $Subs
ResourceGroup = $RGs
ConfigLabel = $ConfigLabel
}
}
ExportAzInfo = @{
Execute = $true
Params = @{
LocalPath = $TempPath
}
}
ExportAzInfoToBlobStorage = @{
Execute = $false
}
CreateBuildSheet = @{
Execute = $false
}
} # End ScriptControl
} # Env ConfigLabel
MAG-Prod{
$Subs = Get-AzSubscription -SubscriptionName "Azure Government Internal"
Set-AzContext -SubscriptionId $Subs.SubscriptionId | Out-Null
$RGs = Get-AzResourceGroup -ResourceGroupName "prod-rg"
$ScriptControl = @{
GetAzInfo = @{
Execute = $true
Params = @{
Subscription = $Subs
ResourceGroup = $RGs
ConfigLabel = $ConfigLabel
}
}
ExportAzInfo = @{
Execute = $true
Params = @{
LocalPath = $TempPath
}
}
ExportAzInfoToBlobStorage = @{
Execute = $true
Params = @{
LocalPath = $TempPath
StorageAccountSubID = $Subs.SubscriptionId
StorageAccountRG = "prod-rg"
StorageAccountName = "prodrgdiag"
StorageAccountContainer = "azinfo"
}
}
} # End ScriptControl
} # End Env ConfigLabel
AzCloud-Prod-RG{
$Subs = Get-AzSubscription -SubscriptionID "3ba3ebad-7974-4e80-a019-3a61e0b7fa91"
Set-AzContext -SubscriptionId $Subs.SubscriptionId | Out-Null
$RGs = Get-AzResourceGroup -ResourceGroupName "prod-rg"
$ScriptControl = @{
GetAzInfo = @{
Execute = $true
Params = @{
Subscription = $Subs
ResourceGroup = $RGs
ConfigLabel = $ConfigLabel
}
}
ExportAzInfo = @{
Execute = $true
Params = @{
LocalPath = $TempPath
}
}
ExportAzInfoToBlobStorage = @{
Execute = $false
Params = @{
LocalPath = $TempPath
StorageAccountSubID = "3ba3ebad-7974-4e80-a019-3a61e0b7fa91"
StorageAccountRG = "prod-rg"
StorageAccountName = "prodrgdiag"
StorageAccountContainer = "azinfo"
}
}
} # End ScriptControl
} # End Env ConfigLabel
} # End Switch ConfigLabel
If ($ScriptControl.GetAzInfo.Execute) {
Write-Output "$(Get-Date -Format yyyy-MM-ddTHH.mm.ss) Running Get-AzInfo..."
$Params = $ScriptControl.GetAzInfo.Params
$AzInfoResults = Get-AzInfo @Params -Verbose
} Else {Write-Output "Skipping GetAzInfo..."}
If ($ScriptControl.ExportAzInfo.Execute) {
Write-Output "$(Get-Date -Format yyyy-MM-ddTHH.mm.ss) Running Export-AzInfo..."
$Params = $ScriptControl.ExportAzInfo.Params
$Params.AzInfoResults = $AzInfoResults
Export-AzInfo @Params
} Else {Write-Output "Skipping ExportAzInfo..."}
If ($ScriptControl.ExportAzInfoToBlobStorage.Execute) {
Write-Output "$(Get-Date -Format yyyy-MM-ddTHH.mm.ss) Running Export-AzInfoToBlobStorage..."
$Params = $ScriptControl.ExportAzInfoToBlobStorage.Params
$Params.AzInfoResults = $AzInfoResults
Export-AzInfoToBlobStorage @Params
} Else {Write-Output "Skipping ExportAzInfoToBlobStorage..."}
# Post Processing...
# if any...
Write-Output "$(Get-Date -Format yyyy-MM-ddTHH.mm.ss) Done!"