Skip to content
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

Unable to Re-import the module #6

Closed
giertz opened this issue Oct 5, 2018 · 7 comments
Closed

Unable to Re-import the module #6

giertz opened this issue Oct 5, 2018 · 7 comments
Assignees

Comments

@giertz
Copy link

giertz commented Oct 5, 2018

Hey,

If i try to import the module, then remove it from memory, then import it again i get "An item with the same key has already been added"

Removing the module:

PS C:\> Remove-Module Microsoft.Graph.Intune -Force -Verbose
VERBOSE: Performing the operation "Remove-Module" on target "UtilCmdlets (Path:
'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.Graph.Intune\CustomModules\UtilCmdlets.psm1')".
VERBOSE: Performing the operation "Remove-Module" on target "AliasCmdlets (Path:
'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.Graph.Intune\CustomModules\AliasCmdlets.psm1')".
VERBOSE: Performing the operation "Remove-Module" on target "Microsoft.Graph.Intune (Path:
'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.Graph.Intune\Microsoft.Intune.PowerShellGraphSDK.dll')".
VERBOSE: Removing the imported "Get-MSGraphAllPages" function.
VERBOSE: Removing the imported "Get-MSGraphDebugInfo" function.
VERBOSE: Removing the imported "Set-MSGraphAlias" function.

Importing the module again

PS C:\> Import-Module Microsoft.Graph.Intune -Force
Import-Module : An item with the same key has already been added.
At line:1 char:1
+ Import-Module Microsoft.Graph.Intune -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Import-Module], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ImportModuleCommand

Any ideas why and how to solve it?

@giertz giertz changed the title Unable to import the module Unable to Re-import the module Oct 5, 2018
@nsoy nsoy self-assigned this Oct 5, 2018
@nsoy
Copy link
Contributor

nsoy commented Oct 5, 2018

Investigating... thanks for reporting this issue.

@rohitramu
Copy link
Contributor

@giertz Thanks for reporting this - I've found a fix and am in the process of releasing a new version.

Just out of curiosity, why are you trying to unload/reload the module? Is there a specific scenario you need this functionality for?

@giertz
Copy link
Author

giertz commented Oct 10, 2018

@rohitramu Im working on a console tool where you can load various cloud modules and then authenticate with stored credentials. There is a function builtin that allows the user to disconnect a current session, in this case it will just unload the module. So in a scenario where the user either wants to just "disconnect" or authenticate with a new account this function will be executed.

@rohitramu
Copy link
Contributor

Cool, that sounds interesting! Just in case you weren't aware: even when you unload a module, the currently loaded dll's will not be unloaded from memory - the cmdlets just won't be available to use. The easiest way I know of to unload dll's is to use separate PowerShell sessions. This way, you can just exit the sessions and the dll's will be unloaded from memory.

An alternative is to just not unload the module at all. The "connection" is actually a bit of an illusion. Authentication for Microsoft Graph actually works by retrieving an authentication "token" when you run Connect-MSGraph, and then this token is sent along with each request to Microsoft Graph. This means that there actually isn't any persistent connection that you can close.

In terms of logging into a different account, you can do that with the following command, which will force a login window to open:

Connect-MSGraph -ForceInteractive

I have updated the release with the fix. Please download it again from the "Releases" tab in this repository.

@giertz
Copy link
Author

giertz commented Oct 11, 2018

Thanks! You don't happen to know how long the token is valid during inactivity?

@rohitramu
Copy link
Contributor

rohitramu commented Oct 11, 2018

I think it might be an hour. However, you shouldn't need to worry about that because the module will automatically refresh the token (i.e. get a new token) for you if it has expired.

@ghost
Copy link

ghost commented Jan 1, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants