Reputation: 1
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?
Upvotes: 0
Views: 778