msgraph-sdk-powershell icon indicating copy to clipboard operation
msgraph-sdk-powershell copied to clipboard

Get-MgEnvironment: SessionNotInitialized exception being thrown after the Microsoft.Graph.Authentication module is not forcibly removed from a session

Open peterboba opened this issue 2 years ago • 4 comments

Describe the bug Get-MgEnvironment: SessionNotInitialized exception being thrown after the Microsoft.Graph.Authentication module is not forcibly removed from a session

To Reproduce Steps to reproduce the behavior:

PowerShell 7.4.0
PS /> Install-Module -Name Microsoft.Graph.Authentication -RequiredVersion 2.9.1 -Scope AllUsers -AllowClobber -Force
PS /> Install-Module -Name Microsoft.Graph.Applications -RequiredVersion 2.9.1 -Scope AllUsers -AllowClobber -Force
PS /> Import-Module Microsoft.Graph.Applications                                
PS /> Get-Module                                                                

ModuleType Version    PreRelease Name                                ExportedCo
                                                                     mmands
---------- -------    ---------- ----                                ----------
Script     2.9.1                 Microsoft.Graph.Applications        {Add-MgAp…
Script     2.9.1                 Microsoft.Graph.Authentication      {Add-MgEn…
Manifest   7.0.0.0               Microsoft.PowerShell.Management     {Add-Cont…
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Memb…
Script     1.4.8.1               PackageManagement                   {Find-Pac…
Script     2.2.5                 PowerShellGet                       {Find-Com…
Script     2.3.4                 PSReadLine                          {Get-PSRe…

PS /> Remove-Module Microsoft.Graph.Authentication
Remove-Module: Unable to remove the module 'Microsoft.Graph.Authentication' because it is required by 'Microsoft.Graph.Applications'. Add the Force parameter to your command to remove the module.
PS /> Get-Module

ModuleType Version    PreRelease Name                                ExportedCo
                                                                     mmands
---------- -------    ---------- ----                                ----------
Script     2.9.1                 Microsoft.Graph.Applications        {Add-MgAp…
Script     2.9.1                 Microsoft.Graph.Authentication      {Add-MgEn…
Manifest   7.0.0.0               Microsoft.PowerShell.Management     {Add-Cont…
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Memb…
Script     1.4.8.1               PackageManagement                   {Find-Pac…
Script     2.2.5                 PowerShellGet                       {Find-Com…
Script     2.3.4                 PSReadLine                          {Get-PSRe…

PS /> Get-MgEnvironment
Get-MgEnvironment: SessionNotInitialized

Expected behavior

PowerShell 7.4.0
PS /> Install-Module -Name Microsoft.Graph.Authentication -RequiredVersion 2.9.1 -Scope AllUsers -AllowClobber -Force
PS /> Install-Module -Name Microsoft.Graph.Applications -RequiredVersion 2.9.1 -Scope AllUsers -AllowClobber -Force
PS /> Import-Module Microsoft.Graph.Applications
PS /> Get-Module                                

ModuleType Version    PreRelease Name                                ExportedCo
                                                                     mmands
---------- -------    ---------- ----                                ----------
Script     2.9.1                 Microsoft.Graph.Applications        {Add-MgAp…
Script     2.9.1                 Microsoft.Graph.Authentication      {Add-MgEn…
Manifest   7.0.0.0               Microsoft.PowerShell.Management     {Add-Cont…
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Memb…
Script     1.4.8.1               PackageManagement                   {Find-Pac…
Script     2.2.5                 PowerShellGet                       {Find-Com…
Script     2.3.4                 PSReadLine                          {Get-PSRe…

PS /> Remove-Module Microsoft.Graph.Authentication -Force
PS /> Get-Module                                         

ModuleType Version    PreRelease Name                                ExportedCo
                                                                     mmands
---------- -------    ---------- ----                                ----------
Script     2.9.1                 Microsoft.Graph.Applications        {Add-MgAp…
Manifest   7.0.0.0               Microsoft.PowerShell.Management     {Add-Cont…
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Memb…
Script     1.4.8.1               PackageManagement                   {Find-Pac…
Script     2.2.5                 PowerShellGet                       {Find-Com…
Script     2.3.4                 PSReadLine                          {Get-PSRe…

PS /> Get-MgEnvironment

Name     AzureADEndpoint                   GraphEndpoint
----     ---------------                   -------------                       
Germany  https://login.microsoftonline.de  https://graph.microsoft.de          
Global   https://login.microsoftonline.com https://graph.microsoft.com         
China    https://login.chinacloudapi.cn    https://microsoftgraph.chinacloudap…
USGov    https://login.microsoftonline.us  https://graph.microsoft.us          
USGovDoD https://login.microsoftonline.us  https://dod-graph.microsoft.us

Module Version Script 2.9.1 Microsoft.Graph.Applications Script 2.9.1 Microsoft.Graph.Authentication But it seems to affect all the versions

Environment Data

Name Value


PSVersion 7.4.0 PSEdition Core GitCommitId 7.4.0 OS Ubuntu 22.04.3 LTS Platform Unix PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0

Additional context Note that the only difference is -Force when removing the module for the successful case. It seems like the Authentication module deinitializes the session although doesn't get removed. The fix would be not to run the deinit hook if a dependency prevents the module removal.

peterboba avatar Nov 22 '23 12:11 peterboba

@peterboba I have tried reproducing your issue with the latest Microsoft Graph version(2.10.0) but I was unable to, Could you please upgrade to the latest version and try again? image

timayabi2020 avatar Dec 05 '23 13:12 timayabi2020

@peterboba I have tried reproducing your issue with the latest Microsoft Graph version(2.10.0) but I was unable to, Could you please upgrade to the latest version and try again? image

@timayabi2020 you need to follow the repro steps. I can still repro this in 2.10.0. Can we reopen this issue? I was not around last week and this shouldn't have been auto-closed.

Import-Module Microsoft.Graph.Applications
Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     2.10.0     Microsoft.Graph.Applications        {Add-MgApplicationKey, Add-MgApplicationPassword, Add-MgSe...
Script     2.10.0     Microsoft.Graph.Authentication      {Add-MgEnvironment, Connect-MgGraph, Disconnect-MgGraph, G...
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Con...
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script     2.0.0      PSReadline                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...


Remove-Module Microsoft.Graph.Authentication
Remove-Module : Unable to remove the module 'Microsoft.Graph.Authentication' because it is required by
'Microsoft.Graph.Applications'. Add the Force parameter to your command to remove the module.
At line:1 char:1
+ Remove-Module Microsoft.Graph.Authentication
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (Microsoft.Graph.Authentication:PSModuleInfo) [Remove-Module], Invalid
   OperationException
    + FullyQualifiedErrorId : Modules_ModuleIsRequired,Microsoft.PowerShell.Commands.RemoveModuleCommand

Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     2.10.0     Microsoft.Graph.Applications        {Add-MgApplicationKey, Add-MgApplicationPassword, Add-MgSe...
Script     2.10.0     Microsoft.Graph.Authentication      {Add-MgEnvironment, Connect-MgGraph, Disconnect-MgGraph, G...
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Con...
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script     2.0.0      PSReadline                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...


Get-MgEnvironment
Get-MgEnvironment : SessionNotInitialized
At line:1 char:1
+ Get-MgEnvironment
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-MgEnvironment], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.Graph.PowerShell.Authentication.Cmdlets.GetMg
   Environment

peterboba avatar Dec 14 '23 10:12 peterboba

Hi @peterboba ,

I tested using the latest SDK 2.19 now and it works. Could you please upgrade to the latest version and try again?

PS> Get-Module

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     2.19.0                Microsoft.Graph.Applications        {Add-MgApplicationKey, Add-MgApplicationPassword, Add-MgServicePrincipalKey, Add-MgSer…
Script     2.19.0                Microsoft.Graph.Authentication      {Add-MgEnvironment, Connect-MgGraph, Disconnect-MgGraph, Get-MgContext…}
Script     2.19.0                Microsoft.Graph.Identity.Governance {Add-MgIdentityGovernanceAccessReviewDefinitionInstanceDecision, Get-MgAgreement, Get-…
Manifest   7.0.0.0               Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-ItemProperty…}
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object…}
Script     2.3.5                 PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKeyHandler, Set-PSRe…

PS> Remove-Module Microsoft.Graph.Authentication
Remove-Module: Unable to remove the module 'Microsoft.Graph.Authentication' because it is required by 'Microsoft.Graph.Applications'. Add the Force parameter to your command to remove the module.

PS> Get-Module

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     2.19.0                Microsoft.Graph.Applications        {Add-MgApplicationKey, Add-MgApplicationPassword, Add-MgServicePrincipalKey, Add-MgSer…
Script     2.19.0                Microsoft.Graph.Authentication      {Add-MgEnvironment, Connect-MgGraph, Disconnect-MgGraph, Get-MgContext…}
Script     2.19.0                Microsoft.Graph.Identity.Governance {Add-MgIdentityGovernanceAccessReviewDefinitionInstanceDecision, Get-MgAgreement, Get-…
Manifest   7.0.0.0               Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-ItemProperty…}
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object…}
Script     2.3.5                 PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKeyHandler, Set-PSRe…

petrhollayms avatar Jun 06 '24 08:06 petrhollayms

I just found that while the Get-Module works now in 2.19 in this scenario (as mentioned above), there still is something wrong with the session handling, when executing other cmdlets like Get-MgUser instead, getting error like

Get-MgUser_List: One or more errors occurred. (SessionNotInitialized)

PS> Disconnect-MgGraph Disconnect-MgGraph: One or more errors occurred. (SessionNotInitialized)

PS> Connect-MgGraph ... Connect-MgGraph: Object reference not set to an instance of an object.

petrhollayms avatar Jun 06 '24 09:06 petrhollayms