-
Notifications
You must be signed in to change notification settings - Fork 0
/
my-azure.unsigned.psm1
567 lines (469 loc) · 20.9 KB
/
my-azure.unsigned.psm1
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<#
.Synopsis
Login to an Azure Context
.Description
Login to an Azure Context. This function performs the azure login
and then checks if multiple subscriptions exist. If multiple
subscriptions exist the user is prompted to select the desired
subscription.
.Example
Connect-MyAzure
#>
function Connect-MyAzure {
param([string] $Environment = "")
if([string]::IsNullOrEmpty($Environment)) {
Connect-AzAccount
}
else {
Connect-AzAccount -Environment $Environment
}
$subs = Get-AzSubscription
if($subs.count -gt 1) {
$subs | format-table -Property @{name="Index";expression={$subs.IndexOf($_)}},Name,SubscriptionId
$x = Read-Host -Prompt "Please Input the Index of the Subscription you wish to use: "
Set-AzContext -SubscriptionId $subs[$x]
}
}
<#
.Synopsis
Login to Azure for Government (GCC High)
.Description
Login to Azure for Government (GCC High)
.Example
Connect-MyAzureGov
#>
function Connect-MyAzureGov {
Connect-MyAzure -Environment AzureUSGovernment
}
<#
.Synopsis
Display all the available VM SKUs in a particular region
.Description
Queries the VM Image Publishers, their associated offers and then lists the available SKUs for that location
.Example
# Show all the Linux SKUs for the default location (eastus)
Find-MyAzureVMImages -PublisherFilter "*linux*"
#>
function Find-MyAzureVMImages {
param(
[string] $Location="eastus",
[Parameter(Mandatory=$true)][string] $PublisherFilter
)
Get-AzVMImagePublisher -Location $Location | Where-Object { $_.PublisherName -like $PublisherFilter } | ForEach-Object {
$currentPub = $_
Get-AzVMImageOffer -Location $Location -PublisherName $currentPub.PublisherName | ForEach-Object {
Get-AzVMImageSku -Location $Location -PublisherName $currentPub.PublisherName -Offer $_.Offer
}
}
}
<#
.Synopsis
Get the name and version number for the Azure Module that is loaded on the local machine.
.Description
This command queries the list of available modules which include Azure in the name and then prints
a table with their name and version numbers.
.Example
Get-MyAzureVersion
#>
function Get-MyAzureVersion {
Get-Module -ListAvailable | Where-Object { $_.Name -eq 'Azure' -or $_.Name -like 'Az.*' } | Format-Table -Property Name,Version
}
<#
.Synopsis
Get the Power State of the VMs in the resource group
.Description
Queries the Azure VMs in the provided resource group and displays their names and current PowerState
.Example
# Show the power status of all the VMs in resource group 'Demo1'
Get-MyAzureVmStatus -ResourceGroupName Demo1
#>
function Get-MyAzureVMStatus {
param(
[Parameter(Mandatory=$true)][string] $ResourceGroupName,
[string] $TagName = "",
[string] $TagValue = ""
)
if([string]::IsNullOrEmpty($TagName) -or [string]::IsNullOrEmpty($TagValue)){
Get-AzVM -ResourceGroupName $ResourceGroupName -Status | Format-Table -Property Name, ResourceGroupName, PowerState
}
else {
Get-AzVM -ResourceGroupName $ResourceGroupName -Status | Where-Object { $_.Tags[$TagName] -eq $TagValue } | Format-Table -Property Name, ResourceGroupName, PowerState
}
}
<#
.Synopsis
Display all the available VM publishers in a particular region
.Description
Lists all the VM Image Publishers, based on the provided filter or no filter at all, for a specific Azure region
.Example
# Show all the Microsoft Publishers for the default location (eastus)
Get-MyAzureVMPublishers -Filter "*Microsoft*"
#>
function Get-MyAzureVMPublishers {
param([string] $Location = "eastus", [string] $Filter = "")
if([string]::IsNullOrEmpty($Filter)) {
Get-AzVMImagePublisher -Location $Location
}
else {
Get-AzVMImagePublisher -Location $Location | Where-Object { $_.PublisherName -like $Filter }
}
}
<#
.Synopsis
Display all the available VM SKUs for a specific Publisher
.Description
Lists the available VM SKUs for the specified publisher and all of their associated Offers in the specified location
.Example
# Show all the Windows Server SKUs for the default location (eastus)
Find-MyAzureVmImageSkus
#>
function Get-MyAzureVMImageSkus {
param([string] $Location ="eastus", [string] $PublisherName = "MicrosoftWindowsServer")
Get-AzVMImageOffer -Location $Location -PublisherName $PublisherName | ForEach-Object {
Get-AzVMImageSku -Location $Location -PublisherName $PublisherName -Offer $_.Offer
}
}
<#
.Synopsis
Gets the versions of windows available from the publishers
.Description
Gets the Azure VM Image SKUs from Windows-Hub, WindowsServer, and Windows offers
.Example
Get-MyAzureWindowsVersions
#>
function Get-MyAzureWindowsVersions {
#Write-Host "Microsoft VM Image Publishers:"
#Get-AzVMImagePublisher -Location eastus | Where-Object { $_.PublisherName -like "*icrosoft*" }
Write-Host "Microsoft Windows-Hub VM Image SKUs"
Get-AzVMImageSku -Location eastus -PublisherName MicrosoftWindowsServer -Offer Windows-Hub
Write-Host "Microsoft Windows Server VM Image SKUs"
get-Azvmimagesku -Location eastus -PublisherName MicrosoftWindowsServer -Offer WindowsServer
Write-Host "Microsoft Windows Client VM Image SKUs"
get-Azvmimagesku -Location eastus -PublisherName MicrosoftVisualStudio -Offer Windows
}
<#
.Synopsis
Simple Resource Group Deployment Script
.Description
Checks if a resource group exists or creates a new resource group and begins a resource group deployment
.Parameter ResourceGroupName
Name of the existing resource group, or name of the resource group to create
.Parameter ResourceGroupLocation
Location of the resource group - only used when creating a new resource group
.Parameter TemplateFile
The Azure Resource Group Deployment JSON file
.Parameter TemplateParametersFile
The Azure Resource Group Deployment JSON file's optional template parameters file
.Example
New-MyAzureDeployment -ResourceGroupName Demo1 -Location eastus -TemplateFile azuredeploy.json -TemplateParametersFile azuredeploy.parameters.json
#>
function New-MyAzureDeployment {
[cmdletbinding(SupportsShouldProcess=$True)]
param(
[Parameter(Mandatory=$true)][string] $ResourceGroupName,
[Parameter(Mandatory=$true)][string] $ResourceGroupLocation,
[Parameter(Mandatory=$true)][string] $TemplateFile,
[string]$TemplateParametersFile = "",
[switch]$NewP2SCert
)
$deploymentName = $ResourceGroupName + "_" + $(get-date -format MMddyyyyHHmmss) + "_deployment"
Write-Verbose "Starting Deployment $deploymentName"
if($PSCmdlet.ShouldProcess($deploymentName,"New Azure Deployment")) {
$resourceGroup = Get-AzResourceGroup -Name $ResourceGroupName -ErrorAction SilentlyContinue
if($null -eq $resourceGroup) {
Write-Verbose "Creating New Azure Resource Group $ResourceGroupName in $ResourceGroupLocation"
$resourceGroup = New-AzResourceGroup -Name $ResourceGroupName -Location $ResourceGroupLocation -ErrorAction Stop
}
if($TemplateParametersFile -ne "") {
if($NewP2SCert) {
$rootCert = $ResourceGroupName + "RootCert"
$childCert = $ResourceGroupName + "ChildCert"
New-MyP2SCertificate -RootCertCN $rootCert -ChildCertCN $childCert -OutputFile $TemplateParametersFile -TemplateParameterFile $TemplateParametersFile
}
Write-Verbose "Starting Resource Group Deployment $deploymentName with Parameter File $TemplateParametersFile"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $ResourceGroupName -TemplateFile $TemplateFile -TemplateParameterFile $TemplateParametersFile
}
else {
Write-Verbose "Starting Resource Group Deployment $deploymentName"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $ResourceGroupName -TemplateFile $TemplateFile
}
}
}
<#
.Synopsis
Simple way to download and create a DSC zip file for use with Azure Resource Group Deployments
.Description
Download DSC Modules to the local folder and generate a zip with those modules and any custom scripts in the current directory
.Parameter DSCModulesPath
The folder location where the DSC Modules should be downloaded to and where the custom files exist - Include trailing '\' in commandline parameter
.Parameter DSCZipFile
Name of the ZIP'ed output file
.Parameter DSCModules
An array of DSC Modules that need to be downloaded to build the DSC Package
.Parameter ForceDSCDownloads
Force the download of the DSC Module even if the module exists locally
.Example
New-MyDSCPackage -DSCModulesPath .\dsc\ -DSCZipFile .\MyTestDSC.zip -DSCModules "xActiveDirectory","xTestingSomething"
#>
function New-MyDSCPackage {
[cmdletbinding(SupportsShouldProcess=$true)]
Param(
[Parameter(Mandatory = $false)][string]$DSCModulesPath = ".\",
[Parameter(Mandatory = $false)][string]$DSCZipFile = ".\MyDSCPackage.zip",
[Parameter(Mandatory = $false)][string[]]$DSCModules = @("xActiveDirectory"),
[Switch]$ForceDSCDownloads
)
foreach ($dscMod in $DSCModules) {
if ($(test-path $($DSCModulesPath + $dscMod)) -eq $false) {
Find-Module -Name $dscMod | Save-Module -Path $DSCModulesPath
}
else {
Write-Verbose "The $dscMod folder already exists"
if ($ForceDSCDownloads) {
Find-Module -Name $dscMod | Save-Module -Path $DSCModulesPath -Force
}
}
}
Compress-Archive -Path $($DSCModulesPath + "*") $DSCZipFile -Force
}
<#
.Synopsis
Creates a local Point 2 Site certificate for use with Azure Gateway deployments
.Description
This creates a local Self-Signed Root and Child certificate for use with Azure Gateway deployments. This also creates
a text file with the exported root certificate so the cert can be used across multiple deployments if desired.
Without any paramters this command will generate a root cert named P2SRootCert, a child cert P2SChildCert, and the exported
file rootcert.txt (in the local directory).
.Example
New-MyP2SCertificate -RootCertCN "p2scert_root" -ChildCertCN "p2scert_child" -OutputFile "folder\p2scert_root.txt"
#>
function New-MyP2SCertificate {
[cmdletbinding(SupportsShouldProcess=$True)]
Param(
[string]$RootCertCN = "P2SRootCert",
[string]$ChildCertCN = "P2SChildCert",
[string]$OutputFile = "azure.parameters.template.json",
[string]$TemplateParameterFile,
[switch]$OutputRawFile
)
if($PSCmdlet.ShouldProcess($RootCertCN,"Creating Root Certificate")) {
$dnsName = $RootCertCN + "@davidmcwee.com"
$cert = Get-ChildItem -Path Cert:\CurrentUser\My -DnsName $dnsName -ErrorAction SilentlyContinue
if($null -eq $cert) {
Write-Debug "Creating New Root Certificate: $RootCertCN ($dnsName)"
$cert = New-SelfSignedCertificate -Type Custom -KeySpec Signature `
-Subject "CN=$RootCertCN" -KeyExportPolicy Exportable `
-HashAlgorithm sha256 -KeyLength 2048 `
-DnsName $dnsName `
-CertStoreLocation "Cert:\CurrentUser\My" -KeyUsageProperty Sign -KeyUsage CertSign
}
}
if($PSCmdlet.ShouldProcess($ChildCertCN,"Creating Child Certificate")) {
$dnsName = $ChildCertCN + "@davidmcwee.com"
$childCert = Get-ChildItem -Path Cert:\CurrentUser\My -DnsName $dnsName -ErrorAction SilentlyContinue
if($null -eq $childCert) {
Write-Debug "Creating New Child Certificate: $ChildCertCN ($dnsName)"
$childCert = New-SelfSignedCertificate -Type Custom -KeySpec Signature `
-Subject "CN=$ChildCertCN" -KeyExportPolicy Exportable `
-HashAlgorithm sha256 -KeyLength 2048 `
-DnsName $dnsName `
-CertStoreLocation "Cert:\CurrentUser\My" -Signer $cert -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.2")
}
}
if($PSCmdlet.ShouldProcess($OutputFile,"Creating Output File")) {
$certString = [convert]::ToBase64String($cert.RawData)
Write-Debug "Root Cert String for Gateway: "
Write-Debug $certString
$outfile = $OutputFile
if($OutputRawFile -and $outfile -notlike "*.txt") {
$outfile = $outfile + ".txt"
}
elseif (!$OutputRawFile -and $outfile -notlike "*.json") {
$outfile = $outfile + ".json"
}
if($OutputRawFile) {
$output = $certString
}
else {
$content = Get-Content $TemplateParameterFile -Raw -ErrorAction SilentlyContinue
if($null -eq $content) {
$template = $PSScriptRoot + "/azure.parameters.template.json"
Write-Debug "Template File $template"
$content = Get-Content $template -Raw
}
$paramobj = $content | ConvertFrom-Json
$paramobj.parameters.gatewayCertName.value = $RootCertCN
$paramobj.parameters.gatewayCertData.value = $certString
$output = $paramobj | ConvertTo-Json -Depth 4
}
$output | Out-File -FilePath $outfile -Force
$outfile
}
}
<#
.Synopsis
Starts all VMs in the provided Resource Group
.Description
Starts all VMs in the provided Resouce Group. This function retrieves the VMs in a resouce
group and then starts those VMs.
.Parameter ResourceGroupName
The name of the Azure Resource Group where the VMs are located.
.Parameter NoWait
Include this parameter in the command line to start VMs as a job rather than waiting for each one to successfully start
.Example
# Start all the VMs in the Resource Group 'Demo1'
Start-MyAzureVMs -ResourceGroupName Demo1
# Start all the VMs in the Resource Group 'Demo1' in parallel
Start-MyAzureVMs -ResourceGroupName Demo1 -NoWait
#>
function Start-MyAzureVMs {
[cmdletbinding(SupportsShouldProcess=$True)]
param(
[Parameter(Mandatory=$true)][string] $ResourceGroupName,
[string]$VMNameFilter = "",
[string]$TagName = "",
[string]$TagValue = "",
[switch] $Wait
)
Write-Verbose "Wait: $($Wait)"
if($PSCmdlet.ShouldProcess($ResourceGroupName, "Start VMs")){
if($Wait -eq $false){
if(![string]::IsNullOrEmpty($VMNameFilter)) {
get-Azvm -ResourceGroupName $ResourceGroupName | Where-Object { $_.Name -like $VMNameFilter } | ForEach-Object { Start-AzVm -Name $_.Name -ResourceGroupName $_.ResourceGroupName -AsJob }
}
elseif (![string]::IsNullOrEmpty($TagName) -and ![string]::IsNullOrEmpty($TagValue)){
Write-Debug "Starting machines with $TagName : $TagValue"
Get-AzVM -ResourceGroupName $ResourceGroupName | Where-Object { $_.Tags[$TagName] -eq $TagValue } | ForEach-Object {
Write-Debug "Starting VM $($_.Name) in ResourceGroup $($_.ResourceGroupName)"
Start-AzVm -Name $_.Name -ResourceGroupName $_.ResourceGroupName -AsJob
}
}
else {
get-Azvm -ResourceGroupName $ResourceGroupName | ForEach-Object { Start-AzVm -Name $_.Name -ResourceGroupName $_.ResourceGroupName -AsJob }
}
}
else {
if(![string]::IsNullOrEmpty($VMNameFilter)){
get-Azvm -ResourceGroupName $ResourceGroupName | Where-Object { $_.Name -like $VMNameFilter } | ForEach-Object { Start-AzVm -Name $_.Name -ResourceGroupName $_.ResourceGroupName }
}
elseif (![string]::IsNullOrEmpty($TagName) -and ![string]::IsNullOrEmpty($TagValue)){
Get-AzVM -ResourceGroupName $ResourceGroupName | Where-Object { $_.Tags[$TagName] -eq $TagValue } | ForEach-Object { Start-AzVm -Name $_.Name -ResourceGroupName $_.ResourceGroupName }
}
else {
get-Azvm -ResourceGroupName $ResourceGroupName | ForEach-Object { Start-AzVm -Name $_.Name -ResourceGroupName $_.ResourceGroupName }
}
}
}
}
<#
.Synopsis
Stops all VMs in all Resource Groups in the current subscription
.Description
Stops all VMs in all Resource Groups in the current subscription.
This function retrieves all the Resource Groups in the current subscription
and iterates through all the VMs in the Resouce Groups and stops them.
.Parameter NoWait
Include this parameter in the command line to start VMs as a job rather than waiting for each one to successfully start
.Example
# Stop all VMs in the Resource Group 'Demo1'
Stop-MyAzureAllVMs
# Stop all VMs in the Resource Group 'Demo1' in parallel
Stop-MyAzureAllVMs -NoWait
#>
function Stop-MyAzureAllVMs {
[cmdletbinding(SupportsShouldProcess=$True)]
param([switch]$Wait)
Write-Verbose "NoWait: $($Wait)"
if($PSCmdlet.ShouldProcess("All Resource Groups", "Don't Wait: $($Wait)"))
{
Get-AzResourceGroup | ForEach-Object {
Write-Verbose "Stopping VMs in Resource Group $($_.ResourceGroupName)"
Get-AzVm -ResourceGroupName $_.ResourceGroupName | ForEach-Object {
Write-Verbose "Stopping VM $($_.Name) in Resource Group $($_.ResourceGroupName)"
if($Wait -eq $false)
{
Stop-AzVM -Name $_.Name -ResourceGroupName $_.ResourceGroupName -Force -AsJob
}
else
{
Stop-AzVM -Name $_.Name -ResourceGroupName $_.ResourceGroupName -Force
}
}
}
}
}
<#
.Synopsis
Stops all VMs in the provided Resource Group
.Description
Stops all VMs in the provided Resouce Group. This function retrieves the VMs in a resouce
group and then stops those VMs.
.Parameter ResourceGroupName
The name of the Azure Resource Group where the VMs are located.
.Parameter NoWait
Include this parameter in the command line to start VMs as a job rather than waiting for each one to successfully start
.Example
# Stop all VMs in the Resource Group 'Demo1'
Stop-MyAzureVMs -ResourceGroupName Demo1
# Stop all VMs in the Resource Group 'Demo1' in parallel
Stop-MyAzureVMs -ResourceGroupName Demo1 -NoWait
#>
function Stop-MyAzureVMs {
[cmdletbinding(SupportsShouldProcess=$True)]
param(
[Parameter(Mandatory=$true)][string] $ResourceGroupName,
[string]$VMNameFilter = "",
[string]$TagName = "",
[string]$TagValue = "",
[switch] $Wait
)
Write-Verbose "NoWait: $($Wait)"
if($PSCmdlet.ShouldProcess($ResourceGroupName, "Stop VMs"))
{
if($Wait -eq $false)
{
if(![string]::IsNullOrEmpty($VMNameFilter)){
get-Azvm -ResourceGroupName $ResourceGroupName | Where-Object { $_.Name -like $VMNameFilter } | ForEach-Object { Stop-AzVm -Name $_.Name -ResourceGroupName $_.ResourceGroupName -force -AsJob }
}
elseif (![string]::IsNullOrEmpty($TagName) -and ![string]::IsNullOrEmpty($TagValue)){
Get-AzVM -ResourceGroupName $ResourceGroupName | Where-Object { $_.Tags[$TagName] -eq $TagValue } | ForEach-Object { Stop-AzVm -Name $_.Name -ResourceGroupName $_.ResourceGroupName -force -AsJob }
}
else {
get-Azvm -ResourceGroupName $ResourceGroupName | ForEach-Object { Stop-AzVm -Name $_.Name -ResourceGroupName $_.ResourceGroupName -force -AsJob }
}
}
else
{
if(![string]::IsNullOrEmpty($VMNameFilter)){
get-Azvm -ResourceGroupName $ResourceGroupName | Where-Object { $_.Name -like $VMNameFilter } | ForEach-Object { Stop-AzVm -Name $_.Name -ResourceGroupName $_.ResourceGroupName -force }
}
elseif (![string]::IsNullOrEmpty($TagName) -and ![string]::IsNullOrEmpty($TagValue)){
Get-AzVM -ResourceGroupName $ResourceGroupName | Where-Object { $_.Tags[$TagName] -eq $TagValue } | ForEach-Object { Stop-AzVm -Name $_.Name -ResourceGroupName $_.ResourceGroupName -force }
}
else {
get-Azvm -ResourceGroupName $ResourceGroupName | ForEach-Object { Stop-AzVm -Name $_.Name -ResourceGroupName $_.ResourceGroupName -force }
}
}
}
}
<#
.Synopsis
Switch current Azure Resource Manager Subscription.
.Description
Switch current Azure Resource Manager Subscription. This function retrieves the available
resource manager subscriptions and prompts the user to select the subscription they wish
to use.
.Example
Switch-MyAzureSubscription -ResourceGroupName Demo1
#>
function Switch-MyAzureSubscription {
$subs = Get-AzSubscription
if($subs.count -gt 1) {
$subs | format-table -Property @{name="Index";expression={$subs.IndexOf($_)}},Name,SubscriptionId
$x = Read-Host -Prompt "Please Input the Index of the Subscription you wish to use: "
Set-AzContext -SubscriptionId $subs[$x]
}
else {
Write-Host "Only one Azure Subscription was found."
}
}