Quantcast
Channel: Exchange Server Development forum
Viewing all articles
Browse latest Browse all 7132

Exchange Command execution failing with type initializer exception while executing from RunSpace

$
0
0

I wanted to execute Exchange powershell commands using System.Management.Automation.Runspaces;

Add-PSSnapIn of Exchange 2013 was successful, but unable to execute any command. e.g: Get-Group, Get-MailBox, Enable-MailBox

Exchange 2013 SnapIn used here is "Microsoft.Exchange.Management.PowerShell.SnapIn". 

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(AssemblyResolver); 
            rc = RunspaceConfiguration.Create();
            PSSnapInInfo info = rc.AddPSSnapIn(Exchange2013SnapIn, out warning);
            if (warning != null)
            {
                System.Console.Write(warning.Message);
                return;
            }
            r = RunspaceFactory.CreateRunspace(rc);
            r.Open();

            ri = new RunspaceInvoke(r);
            try
            {
                System.Console.WriteLine("Executing command : " + sampleArg);
                resultSet = ri.Invoke("Get-Group", input, out errors);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
Same code was working for Exchange 2007/2010 using respective SnapIn.

Is anyone facing/faced similar issue ??



Viewing all articles
Browse latest Browse all 7132

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>