Execute multiple exchange online cmdlets using C# , ASP.NET with only 1 runspace -
i working on asp.net webapplication can connect exchange online , run commandlets.
the following working codebehind single button, gets list of mailboxes in asp.net
protected void finalconnection_click(object sender, eventargs e) { string username = ""; string password = ""; securestring securestring = new securestring(); foreach (char pass in password) { securestring.appendchar(pass); } pscredential pscred = new pscredential(username,securestring); wsmanconnectioninfo connectioninfo = new wsmanconnectioninfo(new uri("https://ps.outlook.com/powershell"), "http://schemas.microsoft.com/powershell/microsoft.exchange", pscred); connectioninfo.authenticationmechanism = authenticationmechanism.basic; connectioninfo.maximumconnectionredirectioncount = 2; using (runspace runspace = runspacefactory.createrunspace(connectioninfo)) { runspace.open(); using (powershell powershell = powershell.create()) { powershell.runspace = runspace; //create command , add parameter powershell.addcommand("get-mailbox"); //var script = "get-mailbox"; //powershell.addscript(script); //powershell.addparameter("recipienttypedetails", "usermailbox"); //invoke command , store results in psobject collection var results = powershell.invoke(); //iterate through results , write displayname , primarysmtp //address each mailbox foreach (psobject result in results) { //console.writeline(string.format("name: {0}, primarysmtpaddress: {1}", // result.properties["displayname"].value.tostring(), // result.properties["primarysmtpaddress"].value.tostring() // )); newpowershell.innerhtml += result.properties["name"].value; } } runspace.close(); } }
i want create different buttons "get-users" "get-msolusers", want keep 1 runspace because of throttling restrictions, max runspace = 3;
should open , close runspace every button ? not correct idea believe
can please suggest alternatives on this?
you can keep runspace open , create new pipeline (runspace.createpipeline
) each time need run script. i'm not sure if it's threadsafe though. may need limit runspace 1 open pipeline @ time.
Comments
Post a Comment