0

I'm attempting to connect to Exchange Online using Exchange Online PowerShell V2 in C# using the Connect-ExchangeOnline command from the ExchangeOnlineManagement module. My code looks like this:

    public class EMShell : IDisposable
    {
        public PowerShell EXOShell { get; set; }

        public EMShell()
        {
            InitialSessionState iss = InitialSessionState.CreateDefault();

            iss.ImportPSModule(new string[] {"ExchangeOnlineManagement" });

            iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.RemoteSigned;

            iss.ThrowOnRunspaceOpenError = true;

            Runspace runspace = RunspaceFactory.CreateRunspace(iss);

            runspace.Open();

            EXOShell = PowerShell.Create(runspace);

            var credentialsPath = Settings.Default.Credentials;

            EXOShell.Commands.Clear();

            EXOShell.AddCommand("Import-Clixml");

            EXOShell.AddParameter("Path", credentialsPath);

            var cred = EXOShell.Invoke().First();

            var credentials = new PSCredential(cred);

            EXOShell.Commands.Clear();

            EXOShell.AddCommand("Connect-ExchangeOnline");

            EXOShell.AddParameter("Credential", credentials);

            var res = EXOShell.Invoke();
        }

        public void Dispose()
        {
            EXOShell.Commands.Clear();
            EXOShell.AddCommand("Disconnect-ExchangeOnline");
            EXOShell.AddParameter("Confirm", false);
            EXOShell.Runspace.Close();
        }
    }

When I run this code, I get the following error on line var res = EXOShell.Invoke();:

System.Management.Automation.CommandNotFoundException: "The 'Update-ModuleManifest' command was found in the module 'PowerShellGet', but the module could not be loaded. For more information, run 'Import-Module PowerShellGet'."

This is strange because the Update-ModuleManifest command belongs to the PowerShellGet module and I wouldn't expect it to be required for the Connect-ExchangeOnline command.

Interestingly, the Connect-ExchangeOnline command runs fine in the PowerShell console without needing to load the PowerShellGet module.

I have already checked that both the ExchangeOnlineManagement and PowerShellGet modules are properly installed on my system.

Can anyone explain why this error is occurring and how I might fix it? Is there a dependency between the Connect-ExchangeOnline command and the PowerShellGet module that I'm overlooking?

Islam Elbanna
  • 1,438
  • 2
  • 9
  • 15
GreenHat
  • 1
  • 1
  • Did you follow the error instructions and run : 'Import-Module PowerShellGet' – jdweng Jun 13 '23 at 14:29
  • Yes, I tried once, but the error remained the same. So, I decided to reinstall both modules. Now, when I try to import the PowerShellGet module, I encounter a different error when I open the runspace: "One or more errors occurred processing the module 'PowerShellGet' specified in the InitialSessionState object used to create this runspace. See the ErrorRecords property for a complete list of errors. The first error was: The required module 'PackageManagement' is not loaded. Load the module or remove the module from 'RequiredModules' in the file ... I tried to load PM and this is happened: – GreenHat Jun 13 '23 at 14:51
  • System.Management.Automation.Runspaces.RunspaceOpenModuleLoadException: "One or more errors occurred processing the module 'PackageManagement' specified in the InitialSessionState object used to create this runspace. See the ErrorRecords property for a complete list of errors. The first error was: The specified module 'PackageManagement' was not loaded because no valid module file was found in any module directory." – GreenHat Jun 13 '23 at 14:52
  • I believe this is leading to a dead end. I managed to load and use all modules in PowerShell, so I don't understand why PowerShellGet needs to be loaded. – GreenHat Jun 13 '23 at 14:55
  • Do you have latest version of PowerShellGet? See following : https://github.com/PowerShell/PowerShellGetv2/issues/447 – jdweng Jun 13 '23 at 15:28
  • Yes, i have the latest version (2.2.5) installed – GreenHat Jun 14 '23 at 09:04

0 Answers0