"import-module msonline" and "connect-MsolService" working in powershell but not in c#


the "import-module msonline"  and "connect-msolservice" working fine in powershell prompt when use c# using runspace , pipeline showing errors.

here code

                                                    

  initialsessionstate initialsession = initialsessionstate.createdefault();
            initialsession.importpsmodule(new[] { "msonline" });


            runspaceconfiguration runspaceconfiguration = runspaceconfiguration.create();

            runspace runspace = runspacefactory.createrunspace(runspaceconfiguration);
            runspace.open();

            runspaceinvoke scriptinvoker = new runspaceinvoke(runspace);

            pipeline pipeline = runspace.createpipeline();
            securestring adminpass = securepass;
            pscredential credential = new pscredential(adminusername, adminpass);
            // create command connect office 365. 
            command connectcommand = new command("connect-msolservice");
            connectcommand.parameters.add((new commandparameter("credential", credential)));
            // create command office 365 users. 


            using (runspace psrunspace = runspacefactory.createrunspace(initialsession))
            {
                // open runspace. 
                psrunspace.open();

                //iterate through each command , executes it. 
                foreach (var com in new command[] { connectcommand })
                {
                    var pipe = psrunspace.createpipeline();
                    pipe.commands.add(com);
                    // execute command , generate results , errors (if any). 
                    collection<psobject> results = pipe.invoke();
                    var error = pipe.error.readtoend();

                }
                // close runspace. 
                psrunspace.close();
            }
            return true;


        }
    }

the error is 

the term 'connect-msolservice' not recognized name of cmdlet, func
tion, script file, or operable program. check spelling of name, or if a
path included, verify path correct , try again.

pl give me assistance 

thanks in advance

hi,

for how run powershell in c#, please refer below article:

http://www.codeproject.com/articles/18229/how-to-run-powershell-scripts-from-c

hope helps.

regards,

yan li


cataleya li
technet community support



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Motherboard replacement

Cannot create Full Text Search catalog after upgrading to V12 - Database is not fully started up or it is not in an ONLINE state

Remote Desktop App - Error 0x207 or 0x607