.net - Connect to Microsoft Exchange PowerShell within C# -


i'm trying connect remote powershell c# .net winforms app. goal create own version of microsoft powershell ise. need way execute powershell scripts app on remote machines. i've created couple of methods , tested on local machine app. if don't use wsmanconnectioninfo , use using (runspace remoterunspace = runspacefactory.createrunspace()) can execute scripts locally if true powershell (little scripts, usage of variables, output data using ft, fl, lot of other things powershell. problem starts when add wsmanconnectioninfo , point exchange server instead of using local connection. seems it's able execute basic stuff "get-mailbox" try pipe things, use scripting capabilities $variables breaks saying it's unsupported.

similarly have disable powershell.addcommand("out-string"); when not using locally.

an unhandled exception of type 'system.management.automation.remoteexception' occurred in system.management.automation.dll.

additional information: term 'out-string' not recognized name of cmdlet, function, script file, or operable program. check spelling of name, or if path included, verify path correct , try again.

the same error doesn't appear if don't force remote connection locally. seems schemauri making strict execute basic commands. saw other examples people using direct information such us:

powershell.addcommand("get-users"); powershell.addparameter("resultsize", count); 

but approach have define lot of possible options , don't want go thru defining parameters , other stuff. load "script" , execute in powershell window.. here's example of use now.

    public static wsmanconnectioninfo powershellconnectioninformation(string serverurl, pscredential pscredentials)     {         var connectioninfo = new wsmanconnectioninfo(new uri(serverurl), "http://schemas.microsoft.com/powershell/microsoft.exchange", pscredentials);         //var connectioninfo = new wsmanconnectioninfo(new uri(serverurl), "http://schemas.microsoft.com/powershell", pscredentials);         connectioninfo.authenticationmechanism = authenticationmechanism.basic;         connectioninfo.skipcacheck = true;         connectioninfo.skipcncheck = true;         connectioninfo.skiprevocationcheck = true;         connectioninfo.maximumconnectionredirectioncount = 5;         connectioninfo.operationtimeout = 150000;         return connectioninfo;     }     public static pscredential securepassword(string login, string password)     {         securestring ssloginpassword = new securestring();         foreach (char x in password) { ssloginpassword.appendchar(x); }         return new pscredential(login, ssloginpassword);     }     public static string runscriptps(wsmanconnectioninfo connectioninfo, string scripttext)     {         stringbuilder stringbuilder = new stringbuilder();         // create remote runspace using connection information.         //using (runspace remoterunspace = runspacefactory.createrunspace())         using (runspace remoterunspace = runspacefactory.createrunspace(connectioninfo))         {             // establish connection calling open() method open runspace.              // opentimeout value set applied while establishing              // connection. establishing remote connection involves sending ,              // receiving data, operationtimeout play role in process.             remoterunspace.open();             // create powershell object run commands in remote runspace.             using (powershell powershell = powershell.create())             {                 powershell.runspace = remoterunspace;                 powershell.addscript(scripttext);                 //powershell.addcommand("out-string");                 powershell.commands.commands[0].mergemyresults(pipelineresulttypes.error, pipelineresulttypes.output);                 collection<psobject> results = powershell.invoke();                  foreach (psobject result in results) {                         stringbuilder.appendline(result.tostring());                 }              }             // close connection. call close() method close remote              // runspace. dispose() method (called using primitive) call              // close() method if not called.             remoterunspace.close();         }          // convert script result single string         return stringbuilder.tostring();     } 

any advice on why happening , workaround how behave same way? i've seen lot of blogs this defining every simple command doesn't make sense me. saw option create local connection , execute remote connection within that that's gotta last resort since relies on multiple other factors.

check https://blogs.msdn.microsoft.com/akashb/2010/03/25/how-to-migrating-exchange-2007-powershell-managed-code-to-work-with-exchange-2010/:

the management experience given exchange 2010 through powershell has been moved way local remote. [...] exchange cmdlets work in remoting scenario, you not able run of powershell cmdlets. [...] yes, mean you not able run cmdlets where-object , .ps1 scripts in remote runspace.

is limitation? don’t think so. can get around create new session , importing it.


so you'll need something this:

pscredential creds = new pscredential(username, securepassword); system.uri uri = new uri("http://exchange-server/powershell?serializationlevel=full");  runspace runspace = runspacefactory.createrunspace();  powershell powershell = powershell.create(); pscommand command = new pscommand(); command.addcommand("new-pssession"); command.addparameter("configurationname", "microsoft.exchange"); command.addparameter("connectionuri", uri); command.addparameter("credential", creds); command.addparameter("authentication", "default"); powershell.commands = command; runspace.open(); powershell.runspace = runspace; collection<pssession> result = powershell.invoke<pssession>();  powershell = powershell.create(); command = new pscommand(); command.addcommand("set-variable"); command.addparameter("name", "ra"); command.addparameter("value", result[0]); powershell.commands = command; powershell.runspace = runspace; powershell.invoke();  powershell = powershell.create(); command = new pscommand(); command.addscript("import-pssession -session $ra"); powershell.commands = command; powershell.runspace = runspace; powershell.invoke();  # can use remote ps it's local 1 

Comments