-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSnippets.psm1
More file actions
1358 lines (1141 loc) · 44.3 KB
/
Copy pathSnippets.psm1
File metadata and controls
1358 lines (1141 loc) · 44.3 KB
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Requires -Version 7.0
<#
.SYNOPSIS
Reusable automation helpers distilled from the personal snippet collection.
.DESCRIPTION
Converts the ad-hoc scripts under PowerShell/Snippets into a single module of
advanced functions covering Azure (Az PowerShell, Azure CLI), Azure DevOps
(REST + CLI), GitHub (gh CLI) and local git/file housekeeping. Every function
carries comment-based help, validated parameters in place of the original
hardcoded organisation/subscription/token/prefix literals, and ShouldProcess
support on the state-changing ones.
Prerequisites depend on the function used: the Az PowerShell modules and an
authenticated context (Connect-AzAccount) for the Az* helpers, the Azure CLI
with the azure-devops extension for the DevOps helpers, and the GitHub CLI
(gh) authenticated for the GitHub helpers.
Import with:
Import-Module "$PSScriptRoot/Snippets.psm1" -Force
.NOTES
Author: Sebastian Gräf
Repo: https://github.com/segraef/Scripts
Version history is tracked in git, not in this header.
#>
#region Initialisation
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
Import-Module "$PSScriptRoot/../Write-Log.psm1" -Force
#endregion
#region Functions
function Test-AvmModule {
<#
.SYNOPSIS
Generate the README and run local Pester/validation/deployment tests for AVM modules.
.DESCRIPTION
Dot-sources the Set-AVMModule and Test-ModuleLocally tooling from a local
clone of Azure/bicep-registry-modules, regenerates each module README, then
executes the requested end-to-end test cases against the given subscription.
Ensures an Azure context exists and sets the subscription before testing.
.PARAMETER RepositoryPath
Path to a local clone of Azure/bicep-registry-modules.
.PARAMETER Module
One or more module paths relative to avm/res (for example 'web/site').
.PARAMETER SubscriptionId
Azure subscription ID used as the deployment/validation target context.
.PARAMETER TenantId
Azure AD tenant ID injected as the TenantId additional token.
.PARAMETER NamePrefix
Naming prefix injected as the namePrefix additional token.
.PARAMETER TestCase
Test case folder names under tests/e2e to run. Pass 'all' to discover and
run every test case folder. Defaults to 'all'.
.PARAMETER Location
Azure region used for validation/deployment. Defaults to australiaeast.
.EXAMPLE
Test-AvmModule -RepositoryPath ~/git/bicep-registry-modules -Module 'web/site' -SubscriptionId $subId -TenantId $tenantId -NamePrefix 'asf3re'
Regenerates the README for avm/res/web/site and runs all its e2e test cases.
.OUTPUTS
None. Emits test progress and results to the log/host.
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$RepositoryPath,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string[]]$Module,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$SubscriptionId,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$TenantId,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$NamePrefix,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string[]]$TestCase = @('all'),
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Location = 'australiaeast'
)
try {
if (-not (Get-AzContext)) {
Write-Log 'No Azure context found. Authenticating.'
Connect-AzAccount
}
Set-AzContext -SubscriptionId $SubscriptionId | Out-Null
}
catch {
Write-Log -Message 'Failed to establish Azure context.' -ErrorRecord $_
throw
}
try {
. "$RepositoryPath/utilities/tools/Set-AVMModule.ps1"
. "$RepositoryPath/utilities/tools/Test-ModuleLocally.ps1"
}
catch {
Write-Log -Message "Failed to load AVM tooling from '$RepositoryPath'." -ErrorRecord $_
throw
}
foreach ($currentModule in $Module) {
Write-Log "Generating README for module '$currentModule'."
try {
Set-AVMModule -ModuleFolderPath "$RepositoryPath/avm/res/$currentModule" -Recurse
}
catch {
Write-Log -Message "Failed to generate README for '$currentModule'." -ErrorRecord $_
throw
}
$testModuleLocallyInput = @{
TemplateFilePath = "$RepositoryPath/avm/res/$currentModule/main.bicep"
PesterTest = $true
ValidationTest = $true
DeploymentTest = $true
ValidateOrDeployParameters = @{
Location = $Location
SubscriptionId = $SubscriptionId
RemoveDeployment = $true
}
AdditionalTokens = @{
namePrefix = $NamePrefix
TenantId = $TenantId
}
}
$resolvedCases = $TestCase
if ($TestCase -contains 'all') {
$resolvedCases = Get-ChildItem -Path "$RepositoryPath/avm/res/$currentModule/tests/e2e" -Directory |
ForEach-Object { $_.Name }
}
foreach ($case in $resolvedCases) {
Write-Log "Running test case '$case' on module '$currentModule'."
$testModuleLocallyInput.ModuleTestFilePath = "$RepositoryPath/avm/res/$currentModule/tests/e2e/$case/main.test.bicep"
try {
Test-ModuleLocally @testModuleLocallyInput
}
catch {
Write-Log -Message "Test case '$case' on module '$currentModule' failed." -ErrorRecord $_
}
}
}
}
function Add-GitHubIssueToProject {
<#
.SYNOPSIS
Add open issues from matching GitHub repositories to a GitHub project.
.DESCRIPTION
Lists repositories in the given owner whose name matches a pattern, collects
their open issues via the GitHub CLI, and adds each issue to the named
project. Repositories matching the exclude pattern are skipped.
.PARAMETER Owner
GitHub owner/organisation to list repositories from.
.PARAMETER RepositoryFilter
Substring used to select repositories by name (for example 'terraform-azurerm-avm').
.PARAMETER Project
Title of the GitHub project to add the issues to.
.PARAMETER ExcludeRepository
Repository name to skip (for example a template repo). Optional.
.PARAMETER RepositoryLimit
Maximum number of repositories to fetch from the owner. Defaults to 5000.
.EXAMPLE
Add-GitHubIssueToProject -Owner Azure -RepositoryFilter 'terraform-azurerm-avm' -Project 'AVM - Module Issues' -ExcludeRepository 'terraform-azurerm-avm-template'
Adds every open issue from matching repos to the project, skipping the template repo.
.OUTPUTS
None. Adds issues to the project as a side effect.
#>
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Owner,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$RepositoryFilter,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Project,
[Parameter()]
[string]$ExcludeRepository,
[Parameter()]
[ValidateRange(1, 5000)]
[int]$RepositoryLimit = 5000
)
try {
$repos = gh repo list $Owner -L $RepositoryLimit --json name --jq '.[].name' |
Select-String -Pattern $RepositoryFilter |
ForEach-Object { $_.Line }
}
catch {
Write-Log -Message "Failed to list repositories for owner '$Owner'." -ErrorRecord $_
throw
}
Write-Log "Fetching issues for $($repos.Count) repositories."
foreach ($repo in $repos) {
if ($ExcludeRepository -and $repo -eq $ExcludeRepository) {
Write-Log "Skipping excluded repository '$repo'."
continue
}
Write-Log "Fetching issues for '$repo'."
try {
$issues = gh issue list -R "$Owner/$repo" --json 'number,title,url' | ConvertFrom-Json
}
catch {
Write-Log -Message "Failed to list issues for '$repo'." -ErrorRecord $_
continue
}
foreach ($issue in $issues) {
if ($PSCmdlet.ShouldProcess($issue.url, "Add to project '$Project'")) {
Write-Log "Adding issue '$($issue.title)' from '$repo' to project '$Project'."
try {
gh issue edit $issue.url --add-project $Project
}
catch {
Write-Log -Message "Failed to add issue '$($issue.url)' to project '$Project'." -ErrorRecord $_
}
}
}
}
}
function Add-AzureDevOpsPullRequest {
<#
.SYNOPSIS
Open a pull request from a source branch across every repository in an Azure DevOps project.
.DESCRIPTION
Signs in to Azure DevOps with the supplied PAT, configures the default
organisation and project, lists all repositories, and creates a pull request
from the given source branch in each one. Requires the azure-devops Azure CLI
extension (added if missing).
.PARAMETER Organization
Azure DevOps organisation name (the segment in https://dev.azure.com/<org>).
.PARAMETER Project
Azure DevOps project name.
.PARAMETER Token
Personal access token used to authenticate the Azure CLI to Azure DevOps.
.PARAMETER SourceBranch
Source branch to open each pull request from (for example users/segraef/provider-upgrade).
.EXAMPLE
Add-AzureDevOpsPullRequest -Organization contoso -Project Modules -Token $pat -SourceBranch 'users/segraef/provider-upgrade'
Opens a PR from the branch in every repository of the Modules project.
.OUTPUTS
None. Creates pull requests as a side effect.
#>
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Organization,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Project,
[Parameter(Mandatory)]
[securestring]$Token,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$SourceBranch
)
$orgUrl = "https://dev.azure.com/$Organization"
try {
az extension add --name azure-devops
$plainToken = [System.Net.NetworkCredential]::new('', $Token).Password
$plainToken | az devops login --organization $orgUrl
az devops configure --defaults organization=$orgUrl project=$Project
$repos = az repos list | ConvertFrom-Json
}
catch {
Write-Log -Message "Failed to query Azure DevOps repositories for '$Project'." -ErrorRecord $_
throw
}
foreach ($repo in $repos) {
if ($PSCmdlet.ShouldProcess($repo.name, "Create pull request from '$SourceBranch'")) {
Write-Log "Creating pull request in '$($repo.name)'."
try {
az repos pr create --repository $repo.name --source-branch $SourceBranch --open --output table
}
catch {
Write-Log -Message "Failed to create pull request in '$($repo.name)'." -ErrorRecord $_
}
}
}
}
function Remove-GoneGitBranch {
<#
.SYNOPSIS
Prune local git branches whose upstream is gone across nested repositories.
.DESCRIPTION
Walks each git repository under the given root, fetches and prunes remote
tracking references, then deletes local branches whose upstream has been
removed (marked ': gone]' by git branch -vv). Returns to the starting
directory when finished.
.PARAMETER Path
Root directory whose subdirectories are treated as repositories. Defaults to
the current location.
.EXAMPLE
Remove-GoneGitBranch -Path ~/git/work
Prunes stale local branches in every repository found under the work folder.
.OUTPUTS
None. Deletes local branches as a side effect.
#>
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Path = (Get-Location).Path
)
$origin = (Get-Location).Path
try {
$repos = Get-ChildItem -Path $Path -Directory -Recurse
foreach ($repo in $repos) {
Write-Log "Processing repository '$($repo.FullName)'."
Set-Location $repo.FullName
try {
git checkout main
git pull
git remote update origin --prune
$goneBranches = git branch -vv |
Select-String -Pattern ': gone]' |
ForEach-Object { $_.ToString().Trim().Split(' ')[0] }
foreach ($branch in $goneBranches) {
if ($PSCmdlet.ShouldProcess("$($repo.FullName):$branch", 'Delete local branch')) {
git branch -D $branch
}
}
}
catch {
Write-Log -Message "Failed to prune branches in '$($repo.FullName)'." -ErrorRecord $_
}
}
}
finally {
Set-Location $origin
}
}
function New-GitFeatureBranch {
<#
.SYNOPSIS
Create, push and commit a feature branch across nested repositories.
.DESCRIPTION
For each repository directory under the given root, creates the named branch,
pushes it with upstream tracking, stages all changes, commits them with the
supplied message and pushes the commit. Returns to the starting directory
when finished.
.PARAMETER Path
Root directory whose subdirectories are treated as repositories. Defaults to
the current directory.
.PARAMETER BranchName
Name of the branch to create (for example users/segraef/provider-upgrade).
.PARAMETER CommitMessage
Commit message used for the staged changes. Defaults to 'provider upgrade'.
.EXAMPLE
New-GitFeatureBranch -Path . -BranchName 'users/segraef/provider-upgrade' -CommitMessage 'provider upgrade'
Creates and pushes the branch in every repository under the current folder.
.OUTPUTS
None. Creates branches and commits as a side effect.
#>
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Path = '.',
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$BranchName,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$CommitMessage = 'provider upgrade'
)
$origin = (Get-Location).Path
try {
$repos = Get-ChildItem -Path $Path -Directory
foreach ($repo in $repos) {
if ($PSCmdlet.ShouldProcess($repo.FullName, "Create and push branch '$BranchName'")) {
Write-Log "Creating branch '$BranchName' in '$($repo.FullName)'."
Set-Location $repo.FullName
try {
git checkout -b $BranchName
git push --set-upstream origin $BranchName
git add .
git commit -m $CommitMessage
git push
}
catch {
Write-Log -Message "Failed to create/push branch in '$($repo.FullName)'." -ErrorRecord $_
}
}
}
}
finally {
Set-Location $origin
}
}
function New-BuildValidationPolicy {
<#
.SYNOPSIS
Create or update build validation branch policies for Azure DevOps repositories.
.DESCRIPTION
Enumerates repositories, pipelines and existing policy configurations in each
Azure DevOps project (resolved via REST using the supplied PAT), then creates
or updates an optional, manually triggered build validation policy on the
target branch for every repository whose name matches a pipeline. If no
projects are supplied, all projects in the organisation are processed.
.PARAMETER Organization
Azure DevOps organisation name.
.PARAMETER Token
Personal access token used for REST and Azure CLI calls.
.PARAMETER Project
One or more project names to process. When omitted, all projects are discovered.
.PARAMETER Branch
Target branch the policy applies to. Defaults to main.
.PARAMETER ApiVersion
Azure DevOps REST API version. Defaults to 6.0.
.EXAMPLE
New-BuildValidationPolicy -Organization contoso -Token $pat -Project 'Modules'
Creates or updates build validation policies for the Modules project.
.OUTPUTS
None. Creates or updates branch policies as a side effect.
#>
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Organization,
[Parameter(Mandatory)]
[securestring]$Token,
[Parameter()]
[string[]]$Project,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Branch = 'main',
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$ApiVersion = '6.0'
)
$plainToken = [System.Net.NetworkCredential]::new('', $Token).Password
$env:AZURE_DEVOPS_EXT_PAT = $plainToken
$authHeader = @{
Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$plainToken"))
}
function Invoke-AdoCollection {
param
(
[Parameter(Mandatory)]
[string]$Uri
)
$encoded = $Uri -replace ' ', '%20'
(Invoke-RestMethod -Uri $encoded -Headers $authHeader).value
}
try {
if (-not $Project) {
Write-Log 'No project supplied; discovering all projects.'
$projectNames = (Invoke-AdoCollection -Uri "https://dev.azure.com/$Organization/_apis/projects?api-version=$ApiVersion").name
}
else {
$projectNames = $Project
}
}
catch {
Write-Log -Message 'Failed to resolve Azure DevOps projects.' -ErrorRecord $_
throw
}
Write-Log "Processing $($projectNames.Count) project(s)."
foreach ($projectName in $projectNames) {
try {
$repos = Invoke-AdoCollection -Uri "https://dev.azure.com/$Organization/$projectName/_apis/git/repositories?api-version=$ApiVersion"
$pipelines = Invoke-AdoCollection -Uri "https://dev.azure.com/$Organization/$projectName/_apis/pipelines?api-version=$ApiVersion"
$buildPolicies = Invoke-AdoCollection -Uri "https://dev.azure.com/$Organization/$projectName/_apis/policy/configurations?api-version=$ApiVersion"
}
catch {
Write-Log -Message "Failed to read configuration for project '$projectName'." -ErrorRecord $_
continue
}
Write-Log "Project '$projectName': $($repos.Count) repos, $($pipelines.Count) pipelines, $($buildPolicies.Count) policies."
foreach ($repo in $repos) {
$buildDefinition = $pipelines | Where-Object { $_.name -eq $repo.name }
if (-not $buildDefinition) {
Write-Log "No matching pipeline for repository '$($repo.name)'; skipping." -Level Verbose
continue
}
$matchingPolicy = $buildPolicies | Where-Object { $_.settings.buildDefinitionId -eq $buildDefinition.id }
if ($matchingPolicy) {
if ($PSCmdlet.ShouldProcess($repo.name, 'Update build validation policy')) {
Write-Log "Updating build validation policy for '$($repo.name)' using pipeline '$($buildDefinition.name)'."
try {
az repos policy build update `
--id $matchingPolicy.id `
--blocking $false `
--branch $Branch `
--build-definition-id $buildDefinition.id `
--display-name $repo.name `
--enabled $true `
--manual-queue-only $false `
--queue-on-source-update-only $false `
--repository-id $repo.id `
--valid-duration 0 `
--project $projectName
}
catch {
Write-Log -Message "Failed to update policy for '$($repo.name)'." -ErrorRecord $_
}
}
}
else {
if ($PSCmdlet.ShouldProcess($repo.name, 'Create build validation policy')) {
Write-Log "Creating build validation policy for '$($repo.name)' using pipeline '$($buildDefinition.name)'."
try {
az repos policy build create `
--blocking $false `
--branch $Branch `
--build-definition-id $buildDefinition.id `
--display-name $repo.name `
--enabled $true `
--manual-queue-only $false `
--queue-on-source-update-only $false `
--repository-id $repo.id `
--valid-duration 0 `
--project $projectName
}
catch {
Write-Log -Message "Failed to create policy for '$($repo.name)'." -ErrorRecord $_
}
}
}
}
}
}
function New-AzServicePrincipalAssignment {
<#
.SYNOPSIS
Create an Azure AD service principal and assign it a role at a scope.
.DESCRIPTION
Creates a service principal with a generated display name and grants it the
requested role definition at the supplied scope using the Az PowerShell
modules. Requires an authenticated Azure context.
.PARAMETER SubscriptionId
Subscription ID used to build the default scope when Scope is not supplied.
.PARAMETER Role
Role definition name to assign. Defaults to Contributor.
.PARAMETER Scope
Assignment scope. Defaults to the supplied subscription.
.PARAMETER DisplayName
Service principal display name. Defaults to a generated '<random>-sp' value.
.EXAMPLE
New-AzServicePrincipalAssignment -SubscriptionId $subId -Role Reader
Creates a service principal and grants it Reader on the subscription.
.OUTPUTS
The created role assignment object.
#>
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$SubscriptionId,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Role = 'Contributor',
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Scope = "/subscriptions/$SubscriptionId",
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$DisplayName = "$(Get-Random -Minimum 100000 -Maximum 999999)-sp"
)
if ($PSCmdlet.ShouldProcess($Scope, "Create service principal '$DisplayName' with role '$Role'")) {
try {
$sp = New-AzADServicePrincipal -DisplayName $DisplayName
New-AzRoleAssignment -RoleDefinitionName $Role -ServicePrincipalName $sp.AppId -Scope $Scope
}
catch {
Write-Log -Message "Failed to create service principal or role assignment at '$Scope'." -ErrorRecord $_
throw
}
}
}
function Remove-ItemByPrefix {
<#
.SYNOPSIS
Recursively remove files or folders matching a name prefix.
.DESCRIPTION
Finds items (including hidden ones) whose name matches the given prefix
pattern under the current location and removes them recursively. Honours
-WhatIf/-Confirm via ShouldProcess.
.PARAMETER Prefix
Name pattern to match (for example '.pre*' or '.git').
.PARAMETER ExcludeHidden
Exclude hidden items from the search. By default hidden items are included
(matching the original snippet behaviour).
.EXAMPLE
Remove-ItemByPrefix -Prefix '.pre*'
Removes every item whose name starts with '.pre' beneath the current folder.
.OUTPUTS
None. Deletes items as a side effect.
#>
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Prefix,
[Parameter()]
[switch]$ExcludeHidden
)
try {
$items = Get-ChildItem -Path $Prefix -Recurse -Hidden:(-not $ExcludeHidden)
}
catch {
Write-Log -Message "Failed to enumerate items matching '$Prefix'." -ErrorRecord $_
throw
}
foreach ($item in $items) {
if ($PSCmdlet.ShouldProcess($item.FullName, 'Remove item')) {
try {
Remove-Item -Path $item.FullName -Recurse -Force
}
catch {
Write-Log -Message "Failed to remove '$($item.FullName)'." -ErrorRecord $_
}
}
}
}
function ConvertTo-TerraformModuleName {
<#
.SYNOPSIS
Convert child item names to dash-separated terraform-azurerm module names.
.DESCRIPTION
Reads the immediate child items of the given path, lower-cases each base name
and inserts a dash before each upper-case letter, then emits a quoted
'terraform-azurerm<name>' string per item. Useful for generating module name
lists.
.PARAMETER Path
Directory whose child items are converted. Defaults to the current directory.
.EXAMPLE
ConvertTo-TerraformModuleName -Path .
Emits a quoted terraform-azurerm module name for each child item.
.OUTPUTS
System.String. One quoted module name per child item.
#>
[CmdletBinding()]
param
(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Path = '.'
)
try {
$names = Get-ChildItem -Path $Path
}
catch {
Write-Log -Message "Failed to enumerate items under '$Path'." -ErrorRecord $_
throw
}
foreach ($name in $names) {
$converted = ($name.BaseName -replace '(?-i)[A-Z]', '-$&').ToLower()
Write-Output """terraform-azurerm$converted"","
}
}
function Export-AzProviderReadAction {
<#
.SYNOPSIS
Export all Azure provider read operations to a CSV file.
.DESCRIPTION
Queries the full set of Azure provider operations, filters to those whose
operation name contains 'read', and exports the operation names to a CSV
file. Requires an authenticated Azure context.
.PARAMETER Path
Output CSV path. Defaults to Get-AllActions.csv in the current directory.
.EXAMPLE
Export-AzProviderReadAction -Path ./read-actions.csv
Writes every read operation to read-actions.csv.
.OUTPUTS
None. Writes a CSV file as a side effect.
#>
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Path = 'Get-AllActions.csv'
)
if ($PSCmdlet.ShouldProcess($Path, 'Export provider read actions')) {
try {
Get-AzProviderOperation -OperationSearchString '*' |
Where-Object { $_.Operation -like '*read*' } |
Select-Object Operation |
Export-Csv -Path $Path -NoTypeInformation -Force
}
catch {
Write-Log -Message "Failed to export provider read actions to '$Path'." -ErrorRecord $_
throw
}
}
}
function Get-RBACDetails {
<#
.SYNOPSIS
Collect RBAC role assignments for a management group hierarchy or a single scope.
.DESCRIPTION
With -ManagementGroup, recursively walks the management group, its
subscriptions and their resource groups, attaching role assignments to each
node, and optionally exports the result to CSV. With -Scope, returns a flat
list of role assignments for that single scope. Requires an authenticated
Azure context.
.PARAMETER ManagementGroup
Management group name to walk recursively (hierarchy mode).
.PARAMETER Scope
A single resource scope to query (flat mode).
.PARAMETER CsvPath
Optional CSV output path used in hierarchy mode.
.EXAMPLE
Get-RBACDetails -ManagementGroup root -CsvPath ./rbac.csv
Walks the root management group hierarchy and writes the result to rbac.csv.
.EXAMPLE
Get-RBACDetails -Scope '/subscriptions/<id>'
Returns the role assignments at the given subscription scope.
.OUTPUTS
System.Object[]. The collected RBAC detail objects.
#>
[CmdletBinding(DefaultParameterSetName = 'ManagementGroup', SupportsShouldProcess)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseSingularNouns', '',
Justification = 'RBACDetails is the established name of this collection helper.')]
param
(
[Parameter(Mandatory, ParameterSetName = 'ManagementGroup')]
[ValidateNotNullOrEmpty()]
[string]$ManagementGroup,
[Parameter(Mandatory, ParameterSetName = 'Scope')]
[ValidateNotNullOrEmpty()]
[string]$Scope,
[Parameter(ParameterSetName = 'ManagementGroup')]
[ValidateNotNullOrEmpty()]
[string]$CsvPath
)
try {
if ($PSCmdlet.ParameterSetName -eq 'Scope') {
$roleAssignments = Get-AzRoleAssignment -Scope $Scope
return $roleAssignments | ForEach-Object {
[PSCustomObject]@{
Scope = $_.Scope
RoleDefinitionName = $_.RoleDefinitionName
PrincipalType = $_.PrincipalType
PrincipalId = $_.PrincipalId
ObjectId = $_.ObjectId
ObjectType = $_.ObjectType
CanDelegate = $_.CanDelegate
}
}
}
$rbacDetails = @()
$mgInfo = Get-AzManagementGroup -GroupName $ManagementGroup
$mgRbac = Get-AzManagementGroupRoleAssignment -GroupId $ManagementGroup
$mgInfo | Add-Member -MemberType NoteProperty -Name 'Type' -Value 'Management Group' -Force
$mgInfo | Add-Member -MemberType NoteProperty -Name 'RoleAssignment' -Value $mgRbac -Force
$rbacDetails += $mgInfo
$subscriptions = Get-AzManagementGroupSubscriptions -GroupId $ManagementGroup
foreach ($subscription in $subscriptions) {
$subRbac = Get-AzRoleAssignment -Scope $subscription.Id
$subscription | Add-Member -MemberType NoteProperty -Name 'Type' -Value 'Subscription' -Force
$subscription | Add-Member -MemberType NoteProperty -Name 'RoleAssignment' -Value $subRbac -Force
$rbacDetails += $subscription
$resourceGroups = Get-AzResourceGroup -SubscriptionId $subscription.Id
foreach ($resourceGroup in $resourceGroups) {
$rgInfo = Get-AzResourceGroup -Name $resourceGroup.ResourceGroupName
$rgRbac = Get-AzRoleAssignment -Scope $resourceGroup.ResourceId
$rgInfo | Add-Member -MemberType NoteProperty -Name 'Type' -Value 'Resource Group' -Force
$rgInfo | Add-Member -MemberType NoteProperty -Name 'RoleAssignment' -Value $rgRbac -Force
$rbacDetails += $rgInfo
}
}
}
catch {
Write-Log -Message 'Failed to collect RBAC details.' -ErrorRecord $_
throw
}
if ($CsvPath) {
if ($PSCmdlet.ShouldProcess($CsvPath, 'Export RBAC details to CSV')) {
try {
$rbacDetails | Export-Csv -Path $CsvPath -NoTypeInformation
}
catch {
Write-Log -Message "Failed to export RBAC details to '$CsvPath'." -ErrorRecord $_
throw
}
}
}
Write-Output $rbacDetails
}
function Get-RBACHierarchy {
<#
.SYNOPSIS
Print the RBAC hierarchy from a management group down to its resource groups.
.DESCRIPTION
Walks a management group, its subscriptions and their resource groups,
logging each node and its role assignments (role name plus assignee). A
human-readable companion to Get-RBACDetails. Requires an authenticated Azure
context.
.PARAMETER ManagementGroupId
Management group ID to start from (for example 'root').
.EXAMPLE
Get-RBACHierarchy -ManagementGroupId root
Logs the role assignments across the root management group hierarchy.
.OUTPUTS
None. Writes the hierarchy to the log/host.
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ManagementGroupId
)
try {
$mgInfo = Get-AzManagementGroup -GroupId $ManagementGroupId
$mgRbac = Get-AzRoleAssignment -Scope "/providers/Microsoft.Management/managementGroups/$ManagementGroupId"
Write-Log "Management Group: $($mgInfo.DisplayName)"
foreach ($role in $mgRbac) {
Write-Log " Role: $($role.RoleDefinitionName) - Assigned to: $($role.SignInName)"
}
$subscriptions = Get-AzSubscription -ManagementGroup $ManagementGroupId
foreach ($subscription in $subscriptions) {
Write-Log " Subscription: $($subscription.Name)"
$subRbac = Get-AzRoleAssignment -Scope $subscription.Id
foreach ($role in $subRbac) {
Write-Log " Role: $($role.RoleDefinitionName) - Assigned to: $($role.SignInName)"
}
$resourceGroups = Get-AzResourceGroup -SubscriptionId $subscription.Id
foreach ($rg in $resourceGroups) {
Write-Log " Resource Group: $($rg.ResourceGroupName)"
$rgRbac = Get-AzRoleAssignment -ResourceGroupName $rg.ResourceGroupName
foreach ($role in $rgRbac) {
Write-Log " Role: $($role.RoleDefinitionName) - Assigned to: $($role.SignInName)"
}
}
}
}
catch {
Write-Log -Message "Failed to walk RBAC hierarchy for '$ManagementGroupId'." -ErrorRecord $_
throw