Skip to content

Fixes subsequent failures after first successful Invoke-MgGraphRequest run when using national cloud environments #2971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1003,11 +1003,7 @@ private string QualifyFilePath(string path)
/// </summary>
private void ResetGraphSessionEnvironment()
{
var currentEnvironment = GraphSession.Instance.Environment;
if(currentEnvironment != null && !currentEnvironment.Equals(_originalEnvironment))
{
GraphSession.Instance.Environment = _originalEnvironment;
}
_originalEnvironment = GraphSession.Instance.Environment;
}

#region CmdLet LifeCycle
Expand Down Expand Up @@ -1046,8 +1042,6 @@ private async Task ProcessRecordAsync()
if (ShouldCheckHttpStatus && !isSuccess)
{
var httpErrorRecord = await GenerateHttpErrorRecordAsync(httpResponseMessageFormatter, httpRequestMessage);
// A reset of the GraphSession Environment is required to avoid side effects
ResetGraphSessionEnvironment();
ThrowTerminatingError(httpErrorRecord);
}
await ProcessResponseAsync(httpResponseMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ function Permissions_GetPermissionsData([bool] $online) {
# Make a REST request to MS Graph to get the permissions data from the Microsoft Graph service principal
if ( $online -or ! $_permissions.msGraphServicePrincipal -or ! $_permissions.isFromInvokeMgGraphRequest ) {
try {
$restResult = Invoke-MgGraphRequest -method GET -OutputType PSObject $_permissions.msGraphPermissionsRequestUri

# Get-MgContext is used to get the current context for the request to MS Graph
# From the context, we can get the current environment and use it to get the permissions request URI
# If the context is not available, then we will use the default permissions request URI

$context = Get-MgContext
$uri = $_permissions.msGraphPermissionsRequestUri
if($context){
$currentEnv = $context.Environment
$allEnv = Get-MgEnvironment
$env = $allEnv | Where-Object { $_.Name -eq $currentEnv }
$uri = $env.GraphEndpoint + "/v1.0/servicePrincipals?`$filter=appId eq '$Permissions_msGraphApplicationId'"
}
$restResult = Invoke-MgGraphRequest -method GET -OutputType PSObject $uri

if ( $restResult ) {
$_permissions.msGraphServicePrincipal = $restResult | Select-Object -ExpandProperty value
Expand Down