Skip to content

Commit ce0d04c

Browse files
committed
Removed raising exceptions
1 parent c35cfbe commit ce0d04c

11 files changed

Lines changed: 81 additions & 44 deletions

src/Export-Entra.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@
259259
}
260260

261261
default {
262-
throw "Unknown command '$command'"
262+
Write-Warning "Unknown command '$command'"
263263
}
264264
}
265265

@@ -401,7 +401,7 @@
401401
}
402402

403403
default {
404-
throw "Unknown command '$command'"
404+
Write-Warning "Unknown command '$command'"
405405
}
406406
}
407407

@@ -512,7 +512,7 @@
512512

513513
if (!$item.RequestId) {
514514
$item
515-
throw "Item without RequestId. Shouldn't happen!"
515+
Write-Warning "Item without RequestId. Shouldn't happen!"
516516
}
517517

518518
$outputFileName = $item.RequestId -replace "/", "\"
@@ -524,10 +524,11 @@
524524
}
525525

526526
if ($outputFileName.Length -gt 255 -and (Get-ItemPropertyValue HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem -Name LongPathsEnabled -ErrorAction SilentlyContinue) -ne 1) {
527-
throw "Output file path '$outputFileName' is longer than 255 characters. Enable long path support to continue!"
527+
Write-Warning "Output file path '$outputFileName' is longer than 255 characters. Enable long path support to continue!"
528+
return
528529
}
529530

530-
$item | select * -ExcludeProperty RequestId | ConvertTo-Json -depth 100 | Out-File (New-Item -Path $outputFileName -Force)
531+
$item | Select-Object * -ExcludeProperty RequestId | ConvertTo-Json -depth 100 | Out-File (New-Item -Path $outputFileName -Force)
531532
}
532533
#endregion output results
533534
}

src/command/Get-AzurePIMDirectoryRoles.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@
8383
$roleID = $response.Id
8484
Write-Verbose "roleID = $roleID"
8585
if (!$roleID) {
86-
throw "Role $roleName not found. Search is CASE SENSITIVE!"
86+
Write-Warning "Role $roleName not found. Search is CASE SENSITIVE!"
87+
return
8788
}
8889
}
8990

@@ -229,7 +230,8 @@
229230
$outputFileName = Join-Path -Path $rootFolder -ChildPath "$itemId.json"
230231

231232
if ($outputFileName.Length -gt 255 -and (Get-ItemPropertyValue HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem -Name LongPathsEnabled -ErrorAction SilentlyContinue) -ne 1) {
232-
throw "Output file path '$outputFileName' is longer than 255 characters. Enable long path support to continue!"
233+
Write-Warning "Output file path '$outputFileName' is longer than 255 characters. Enable long path support to continue!"
234+
return
233235
}
234236

235237
$item | ConvertTo-Json -depth 100 | Out-File (New-Item -Path $outputFileName -Force)

src/command/Get-AzurePIMGroups.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@
130130
$outputFileName = Join-Path -Path $rootFolder -ChildPath "$itemId.json"
131131

132132
if ($outputFileName.Length -gt 255 -and (Get-ItemPropertyValue HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem -Name LongPathsEnabled -ErrorAction SilentlyContinue) -ne 1) {
133-
throw "Output file path '$outputFileName' is longer than 255 characters. Enable long path support to continue!"
133+
Write-Warning "Output file path '$outputFileName' is longer than 255 characters. Enable long path support to continue!"
134+
return
134135
}
135136

136137
# Hide warning for depth when converting to JSON

src/command/Get-AzurePIMResources.ps1

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
)
77

88
if (!(Get-Command 'Get-AzAccessToken' -ErrorAction silentlycontinue) -or !($azAccessToken = Get-AzAccessToken -WarningAction SilentlyContinue -ErrorAction SilentlyContinue) -or $azAccessToken.ExpiresOn -lt [datetime]::now) {
9-
throw "$($MyInvocation.MyCommand): Authentication needed. Please call Connect-AzAccount."
9+
Write-Warning "Skipping Get-AzurePIMResources. Not connected to Azure."
10+
return
1011
}
1112

1213
#region functions
@@ -76,7 +77,7 @@
7677
}
7778
}
7879
} else {
79-
throw "Undefined property type '$propertyType'"
80+
Write-Warning "Undefined property type '$propertyType'"
8081
}
8182

8283
$object | Select-Object -Property * -ExcludeProperty $propertyName
@@ -127,7 +128,8 @@
127128
$scope = $scope.TrimStart('/')
128129

129130
if (!(Get-Command 'Get-AzAccessToken' -ErrorAction silentlycontinue) -or !($azAccessToken = Get-AzAccessToken -WarningAction SilentlyContinue -ErrorAction SilentlyContinue) -or $azAccessToken.ExpiresOn -lt [datetime]::now) {
130-
throw "$($MyInvocation.MyCommand): Authentication needed. Please call Connect-AzAccount."
131+
Write-Warning "Skipping Get-AzurePIMResources. Not connected to Azure."
132+
return
131133
}
132134

133135
$base = "https://management.azure.com"
@@ -183,7 +185,8 @@
183185
)
184186

185187
if (!(Get-Command 'Get-AzAccessToken' -ErrorAction silentlycontinue) -or !($azAccessToken = Get-AzAccessToken -WarningAction SilentlyContinue -ErrorAction SilentlyContinue) -or $azAccessToken.ExpiresOn -lt [datetime]::now) {
186-
throw "$($MyInvocation.MyCommand): Authentication needed. Please call Connect-AzAccount."
188+
Write-Warning "Skipping Get-AzurePIMResources. Not connected to Azure."
189+
return
187190
}
188191

189192
if ($name) {
@@ -238,7 +241,8 @@
238241
)
239242

240243
if (!(Get-Command 'Get-AzAccessToken' -ErrorAction silentlycontinue) -or !($azAccessToken = Get-AzAccessToken -WarningAction SilentlyContinue -ErrorAction SilentlyContinue) -or $azAccessToken.ExpiresOn -lt [datetime]::now) {
241-
throw "$($MyInvocation.MyCommand): Authentication needed. Please call Connect-AzAccount."
244+
Write-Warning "Skipping Get-AzurePIMResources. Not connected to Azure."
245+
return
242246
}
243247

244248
if ($id) {
@@ -275,7 +279,8 @@
275279
$outputFileName = Join-Path -Path (Join-Path -Path $rootFolder -ChildPath "ManagementGroups") -ChildPath "$itemId.json"
276280

277281
if ($outputFileName.Length -gt 255 -and (Get-ItemPropertyValue HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem -Name LongPathsEnabled -ErrorAction SilentlyContinue) -ne 1) {
278-
throw "Output file path '$outputFileName' is longer than 255 characters. Enable long path support to continue!"
282+
Write-Warning "Output file path '$outputFileName' is longer than 255 characters. Enable long path support to continue!"
283+
return
279284
}
280285

281286
$item | ConvertTo-Json -depth 100 | Out-File (New-Item -Path $outputFileName -Force)
@@ -289,7 +294,8 @@
289294
$outputFileName = Join-Path -Path (Join-Path -Path $rootFolder -ChildPath "Subscriptions") -ChildPath "$itemId.json"
290295

291296
if ($outputFileName.Length -gt 255 -and (Get-ItemPropertyValue HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem -Name LongPathsEnabled -ErrorAction SilentlyContinue) -ne 1) {
292-
throw "Output file path '$outputFileName' is longer than 255 characters. Enable long path support to continue!"
297+
Write-Warning "Output file path '$outputFileName' is longer than 255 characters. Enable long path support to continue!"
298+
return
293299
}
294300

295301
$item | ConvertTo-Json -depth 100 | Out-File (New-Item -Path $outputFileName -Force)

src/command/Get-AzureResourceAccessPolicies.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
)
77

88
if (!(Get-Command 'Get-AzAccessToken' -ErrorAction silentlycontinue) -or !($azAccessToken = Get-AzAccessToken -WarningAction SilentlyContinue -ErrorAction SilentlyContinue) -or $azAccessToken.ExpiresOn -lt [datetime]::now) {
9-
throw "$($MyInvocation.MyCommand): Authentication needed. Please call Connect-AzAccount."
9+
Write-Warning "Skipping Get-AzureResourceAccessPolicies. Not connected to Azure."
10+
return
1011
}
1112

1213
function Get-AzureResourceAccessPolicy {

src/command/Get-AzureResourceIAMData.ps1

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
)
77

88
if (!(Get-Command 'Get-AzAccessToken' -ErrorAction silentlycontinue) -or !($azAccessToken = Get-AzAccessToken -WarningAction SilentlyContinue -ErrorAction SilentlyContinue) -or $azAccessToken.ExpiresOn -lt [datetime]::now) {
9-
throw "$($MyInvocation.MyCommand): Authentication needed. Please call Connect-AzAccount."
9+
Write-Warning "Skipping Get-AzureResourceIAMData. Not connected to Azure."
10+
return
1011
}
1112

1213
$assignmentsFolder = Join-Path -Path $rootFolder -ChildPath "RoleAssignments"
@@ -28,7 +29,7 @@
2829
} elseif ($scope -match "^/providers/Microsoft.Management/managementGroups/.+") {
2930
return 'managementGroup'
3031
} else {
31-
throw 'undefined type'
32+
Write-Warning 'undefined type'
3233
}
3334
}
3435
#endregion helper functions
@@ -104,7 +105,8 @@ authorizationresources
104105
$outputPath = Join-Path -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path $assignmentsFolder -ChildPath "Subscriptions") -ChildPath $item.SubscriptionId) -ChildPath $item.ResourceGroup) -ChildPath $folder
105106
}
106107
default {
107-
throw "Undefined scope type $($item.scopeType)"
108+
Write-Warning "Undefined scope type $($item.scopeType)"
109+
return
108110
}
109111
}
110112

@@ -113,7 +115,8 @@ authorizationresources
113115
$outputFileName = Join-Path -Path $outputPath -ChildPath "$itemId.json"
114116

115117
if ($outputFileName.Length -gt 255 -and (Get-ItemPropertyValue HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem -Name LongPathsEnabled -ErrorAction SilentlyContinue) -ne 1) {
116-
throw "Output file path '$outputFileName' is longer than 255 characters. Enable long path support to continue!"
118+
Write-Warning "Output file path '$outputFileName' is longer than 255 characters. Enable long path support to continue!"
119+
return
117120
}
118121

119122
if (Test-Path $outputFileName -ErrorAction SilentlyContinue) {
@@ -157,12 +160,13 @@ ResourceContainers
157160
} elseif ($result.RequestName -like "/subscriptions/*") {
158161
$outputPath = Join-Path -Path (Join-Path -Path $definitionsFolder -ChildPath "CustomRole\Subscriptions") -ChildPath $scopeId
159162
} else {
160-
throw "Undefined scope type in $($result.RequestName)"
163+
Write-Warning "Undefined scope type in $($result.RequestName)"
164+
return
161165
}
162166

163167
$outputFileName = Join-Path -Path $outputPath -ChildPath "$roleId.json"
164168

165-
$result | select * -ExcludeProperty RequestName | ConvertTo-Json -depth 100 | Out-File (New-Item -Path $outputFileName -Force)
169+
$result | Select-Object * -ExcludeProperty RequestName | ConvertTo-Json -depth 100 | Out-File (New-Item -Path $outputFileName -Force)
166170
}
167171
#endregion export custom RBAC (IAM) roles
168172
#endregion IAM Role definitions export

src/internal/Get-MgGraphAllPages.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@
8282
try {
8383
$page = Invoke-MgGraphRequest -Uri $currentNextLink -Method GET
8484
} catch {
85-
throw $_
85+
Write-Warning "Failed to get next page from $currentNextLink. $_"
86+
return
8687
}
8788

8889
# Extract the NextLink

src/internal/Invoke-AzureBatchRequest.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@
115115

116116
$duplicityId = $requestChunk | Select-Object -ExpandProperty Name | Group-Object | ? { $_.Count -gt 1 }
117117
if ($duplicityId) {
118-
throw "Batch requests must have unique names. Name $(($duplicityId | select -Unique) -join ', ') is there more than once"
118+
Write-Warning "Batch requests must have unique names. Name $(($duplicityId | select -Unique) -join ', ') is there more than once"
119+
return
119120
}
120121

121122
Write-Debug ($requestChunk | ConvertTo-Json -Depth 10)
@@ -307,11 +308,13 @@
307308
# check url validity
308309
$batchRequest.URL | % {
309310
if ($_ -notlike "https://management.azure.com/*" -and $_ -notlike "/*") {
310-
throw "url '$_' has to be relative (without the whole 'https://management.azure.com' part) or absolute!"
311+
Write-Warning "url '$_' has to be relative (without the whole 'https://management.azure.com' part) or absolute!"
312+
return
311313
}
312314

313315
if ($_ -notmatch "/subscriptions/|\?" -and $_ -notmatch "/providers/|\?" -and $_ -notmatch "/resources/|\?" -and $_ -notmatch "/locations/|\?" -and $_ -notmatch "/tenants/|\?" -and $_ -notmatch "/bulkdelete/|\?") {
314-
throw "url '$_' is not valid. Is should starts with:`n/subscriptions, /providers, /resources, /locations, /tenants or /bulkdelete!"
316+
Write-Warning "url '$_' is not valid. Is should starts with:`n/subscriptions, /providers, /resources, /locations, /tenants or /bulkdelete!"
317+
return
315318
}
316319
}
317320

src/internal/Invoke-GraphBatchRequest.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@
226226

227227
$duplicityId = $requestChunk.id | Group-Object | ? { $_.Count -gt 1 }
228228
if ($duplicityId) {
229-
throw "Batch requests must have unique ids. Id(s): '$(($duplicityId.Name | select -Unique) -join ', ')' is there more than once"
229+
Write-Warning "Batch requests must have unique ids. Id(s): '$(($duplicityId.Name | select -Unique) -join ', ')' is there more than once"
230+
return
230231
}
231232

232233
Write-Debug ($requestChunk | ConvertTo-Json -Depth 10)
@@ -450,7 +451,8 @@
450451
# check url validity
451452
$batchRequest.URL | % {
452453
if ($_ -like "http*" -or $_ -like "*/beta/*" -or $_ -like "*/v1.0/*" -or $_ -like "*/graph.microsoft.com/*") {
453-
throw "url '$_' has to be relative (without the whole 'https://graph.microsoft.com/<apiversion>' part)!"
454+
Write-Warning "url '$_' has to be relative (without the whole 'https://graph.microsoft.com/<apiversion>' part)!"
455+
return
454456
}
455457
}
456458

src/internal/New-AzureBatchRequest.ps1

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,29 +149,35 @@
149149

150150
#region validity checks
151151
if ($name -and @($url).count -gt 1) {
152-
throw "'name' parameter cannot be used with multiple urls"
152+
Write-Warning "'name' parameter cannot be used with multiple urls"
153+
return
153154
}
154155

155156
if ($placeholder -and $url -notlike "*<placeholder>*") {
156-
throw "You have specified 'placeholder' parameter, but 'url' parameter doesn't contain string '<placeholder>' for replace."
157+
Write-Warning "You have specified 'placeholder' parameter, but 'url' parameter doesn't contain string '<placeholder>' for replace."
158+
return
157159
}
158160

159161
if (!$placeholder -and $url -like "*<placeholder>*") {
160-
throw "You have specified 'url' with '<placeholder>' in it, but not the 'placeholder' parameter itself."
162+
Write-Warning "You have specified 'url' with '<placeholder>' in it, but not the 'placeholder' parameter itself."
163+
return
161164
}
162165

163166
if ($placeholderAsId -and !$placeholder) {
164-
throw "'placeholderAsId' parameter cannot be used without specifying 'placeholder' parameter"
167+
Write-Warning "'placeholderAsId' parameter cannot be used without specifying 'placeholder' parameter"
168+
return
165169
}
166170

167171
if ($placeholderAsId -and $placeholder -and @($url).count -gt 1) {
168-
throw "'placeholderAsId' parameter cannot be used with multiple urls"
172+
Write-Warning "'placeholderAsId' parameter cannot be used with multiple urls"
173+
return
169174
}
170175

171176
# api version check
172177
$url | % {
173178
if ($_ -notlike "*api-version=*") {
174-
throw "URL '$_' is missing what api to use (api-version=2025-01-01 or similar). For example: 'https://management.azure.com/subscriptions/.../roleEligibilitySchedules?api-version=2020-10-01'. If you are unsure what api you can use, use the one from the example above and in case the request fails with 400 error, check the error message for the correct api version. Or use official Az cmdlet with -debug parameter and check the 'Absolute uri' output."
179+
Write-Warning "URL '$_' is missing what api to use (api-version=2025-01-01 or similar). For example: 'https://management.azure.com/subscriptions/.../roleEligibilitySchedules?api-version=2020-10-01'. If you are unsure what api you can use, use the one from the example above and in case the request fails with 400 error, check the error message for the correct api version. Or use official Az cmdlet with -debug parameter and check the 'Absolute uri' output."
180+
return
175181
}
176182
}
177183
#endregion validity checks
@@ -194,11 +200,13 @@
194200

195201
#region url validity checks
196202
if ($_ -notlike "https://management.azure.com/*" -and $_ -notlike "/*") {
197-
throw "url '$_' has to be in the relative (without the 'https://management.azure.com' prefix and starting with the '/') or absolute form!"
203+
Write-Warning "url '$_' has to be in the relative (without the 'https://management.azure.com' prefix and starting with the '/') or absolute form!"
204+
return
198205
}
199206

200207
if ($_ -notmatch "/subscriptions/|\?" -and $_ -notmatch "/providers/|\?" -and $_ -notmatch "/resources/|\?" -and $_ -notmatch "/locations/|\?" -and $_ -notmatch "/tenants/|\?" -and $_ -notmatch "/bulkdelete/|\?") {
201-
throw "url '$_' is not valid. Is should starts with:`n/subscriptions, /providers, /resources, /locations, /tenants or /bulkdelete!"
208+
Write-Warning "url '$_' is not valid. Is should starts with:`n/subscriptions, /providers, /resources, /locations, /tenants or /bulkdelete!"
209+
return
202210
}
203211
#endregion url validity checks
204212

0 commit comments

Comments
 (0)