简体   繁体   中英

C# .Net How Can I Use Powershell+Exchange Snapin and Make It Stay Open

My new code is below, but it says get-mailbox is not recognized like snapin never added.

    Runspace runSpace;
    public void button1_Click(object sender, EventArgs e)
    {
        Pipeline pipeline = runSpace.CreatePipeline();
        pipeline.Commands.Add("get-mailbox cbilgic");
            pipeline.Invoke();         
    }

    private void button2_Click(object sender, EventArgs e)
    {
        RunspaceConfiguration runspaceConfig = RunspaceConfiguration.Create();
        PSSnapInException snapEx = null;
            PSSnapInInfo psinfo = runspaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapEx);
        runSpace = RunspaceFactory.CreateRunspace(runspaceConfig);
        runSpace.Open();
    }

Firstly, use Runspaces , it will make things much easier.Load your PSSnapin with RunspaceConfiguration and get output of your commands with pipeline.invoke() . Load the snapin and instantiate the runspace when the form loads, then invoke your commands when the user clicks the button.

Reading this article helped me alot when I was trying similar tasks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM