Skip to content

Commit

Permalink
Update ErrorAction
Browse files Browse the repository at this point in the history
  • Loading branch information
Jefajers authored Feb 5, 2024
1 parent 6462caa commit d93e9c1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/functions/Initialize-AzOpsEnvironment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@
}

#region Validate root '/' permissions - different methods of getting current context depending on principalType
$currentPrincipal = Get-AzOpsCurrentPrincipal -AzContext $currentAzContext
$rootPermissions = Get-AzRoleAssignment -ObjectId $currentPrincipal.id -Scope "/" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
$currentPrincipal = Get-AzOpsCurrentPrincipal -AzContext $currentAzContext -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if ($currentPrincipal.id) {
$rootPermissions = Get-AzRoleAssignment -ObjectId $currentPrincipal.id -Scope "/" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
}

if (-not $rootPermissions) {
Write-AzOpsMessage -LogLevel Important -LogString 'Initialize-AzOpsEnvironment.ManagementGroup.NoRootPermissions' -LogStringValues $currentAzContext.Account.Id
Expand Down
13 changes: 9 additions & 4 deletions src/internal/functions/Get-AzOpsCurrentPrincipal.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@

switch ($AzContext.Account.Type) {
'User' {
$principalObject = (Invoke-AzRestMethod -Uri https://graph.microsoft.com/v1.0/me).Content | ConvertFrom-Json
$restMethodResult = Invoke-AzRestMethod -Uri https://graph.microsoft.com/v1.0/me -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if ($restMethodResult) {
$principalObject = $restMethodResult.Content | ConvertFrom-Json
}
}
'ManagedService' {
# Get managed identity application id via IMDS (https://learn.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/how-to-use-vm-token)
$applicationId = (Invoke-RestMethod -Uri "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2021-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F" -Headers @{ Metadata = $true }).client_id
$principalObject = Get-AzADServicePrincipal -ApplicationId $applicationId
$restMethodResult = Invoke-RestMethod -Uri "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2021-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F" -Headers @{ Metadata = $true } -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if ($restMethodResult) {
$principalObject = Get-AzADServicePrincipal -ApplicationId $restMethodResult.client_id -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
}
}
default {
$principalObject = Get-AzADServicePrincipal -ApplicationId $AzContext.Account.Id
$principalObject = Get-AzADServicePrincipal -ApplicationId $AzContext.Account.Id -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
}
}
Write-AzOpsMessage -LogLevel InternalComment -LogString 'Get-AzOpsCurrentPrincipal.PrincipalId' -LogStringValues $principalObject.Id
Expand Down

0 comments on commit d93e9c1

Please sign in to comment.