visual studio - Running PowerShell commands from C# -
i have powershell script connects exchange online , performs tasks creating shared mailboxes, calendars, adding licenses etc.
the thing when run commands c# class error.
this connection:
string schemauri = "http://schemas.microsoft.com/powershell/microsoft.exchange"; uri connectto = new uri("https://outlook.office365.com/powershell-liveid/"); var securepassword = new securestring(); foreach (char c in password) { securepassword.appendchar(c); } this.credential = new pscredential(login, securepassword); wsmanconnectioninfo connectioninfo = new wsmanconnectioninfo(connectto, schemauri, credential); connectioninfo.maximumconnectionredirectioncount = 5; connectioninfo.authenticationmechanism = authenticationmechanism.basic; this.remoterunspace = runspacefactory.createrunspace(connectioninfo); this.remoterunspace.open();
this powershell working fine:
powershell powershell = powershell.create(); powershell.runspace = this.remoterunspace; pscommand command = new pscommand(); command.addcommand("new-mailbox"); command.addparameter("name", mailboxname); command.addparameter("shared"); command.addparameter("primarysmtpaddress", formatedmailboxname); powershell.commands = command; powershell.invoke();
and code not working fine:
powershell powershell = powershell.create(); powershell.runspace = this.remoterunspace; pscommand command = new pscommand(); command.addcommand("connect-msolservice"); command.addcommand("import-module msonline"); command.addparameter("credential", this.credential); command.addcommand("set-msoluser"); command.addparameter("userprincipalname", userlogin); command.addparameter("usagelocation", "se");
the error got following:
additional information: term 'import-module msonline' not recognized name of cmdlet, function, script file, or operable program. check spelling of name, or if path included, verify path correct , try again.
i tried various things copy-paste dll's , changing active solution platform cpu 64 bit , none of helped me.
might stupid have tried following?
command.addcommand("import-module 'msonline'");
i have had similar issue skypeforbusiness module. worked when added ''.
can execute import-module msonline command in powershell ise?
Comments
Post a Comment