Unable to debug why ImportPSModule fails when calling Powershell cmdlets from C# -
i trying call "connect-msolservice" cmdlet c# getting "... 'connect-msolservice' not recognized name of cmdlet ..." error. pointers on how debug problem appreciated.
i based project on office dev center sample office 365: manage users calling windows powershell cmdlets c# first importpsmodule of "msonline" module.
when open office dev center sample in vs 2015 works expected when create own project not. struggling bottom of why error in in project not in sample. have done far is
- have both projects (sample , mine) in same solution
- reduced both projects down console applications removing unnecessary dependencies , references
- the sample had hard reference system.management.automation changed nugget reference (both worked) when tried each in project still got error
- tried basic powershell commands (e.g. get-process) in project , worked
- tried use importpsmodulesfrompath , full path module in importpsmodule neither worked
- tried check if get-item env:\psmodulepath showed same paths in each project , appears case
- check project properties each project in vs , made sure settings same
i running out of ideas on how bottom of problem. code below
// create initial session state runspace. initialsessionstate initialsession = initialsessionstate.createdefault(); initialsession.importpsmodule(new[] { "msonline" }); // create credential object. pscredential credential = new pscredential("james@myestatehub.com", securepass); // create command connect office 365. command connectcommand = new command("connect-msolservice"); connectcommand.parameters.add((new commandparameter("credential", credential))); // create command office 365 contacts. command getusercommand = new command("get-msolcontact"); using (runspace psrunspace = runspacefactory.createrunspace(initialsession)) { // open runspace. psrunspace.open(); //iterate through each command , executes it. foreach (var com in new command[] { connectcommand, getusercommand }) { var pipe = psrunspace.createpipeline(); pipe.commands.add(com); // execute command , generate results , errors (if any). collection<psobject> results = pipe.invoke(); //error raised here
ok, found out why getting problem not mean understand why need add particular element project file.
<propertygroup condition=" '$(configuration)|$(platform)' == 'debug|anycpu' "> <platformtarget>anycpu</platformtarget> <debugsymbols>true</debugsymbols> <debugtype>full</debugtype> <optimize>false</optimize> <outputpath>bin\debug\</outputpath> <defineconstants>debug;trace</defineconstants> <errorreport>prompt</errorreport> <warninglevel>4</warninglevel> <prefer32bit>false</prefer32bit> </propertygroup> in above entry had add <prefer32bit>false</prefer32bit> element propertygroup. once added element code worked expected.
sadly whilst got code run still working out why needed this.
Comments
Post a Comment