Saturday, 15 May 2010

Hosting powershell import-module failure -


I'm trying to host PowerShell and the "ServerAdmin" module is required to run the Get-Windowsfeature OS server 2008 R2 is I can successfully run "import-module serverAdmin" on PS comamnd signal, so I know the machine config is good. However, I can not get it to work in my custom C # host. I've tried two approaches, first shown below, using the import-module command, the error says "specified module 'server admin 'Was not loaded because any module file was not found in any module directory. " The second approach, using InitialSessionState.ImportModule (), also fails. No error has been reported from that method, but the get-windowsfeature command is ignorant.

The first method:

  var ps = PowerShell.Create (); Var cmd = ps.AddCommand ("import-module"); Cmd.AddArgument ("ServerManager"); Ps.Invoke (); Console.WriteLine ("Errors"); // produces "the specified module 'ServerAdmin' was not loaded because no valid module file was found in any module directory" foreach (var error in ps.streams.Error) {Console.WriteLine (error.ToString ( )); }   

The second approach:

  var ps = PowerShell.Create (); Var initial = InitialSessionState.CreateDefault (); Initial.ImportPSModule (new [] {"ServerManager"}); Var Ranspace = Ranspace Factor. Criterion (initial); Runspace.Open (); Ps.Runspace = Ranspace; Ps.AddCommand ("Nights - WindowsFeature"); Var result = ps.Invoke (); // throws an exception because Get-WindowsFeature is not a foreach (result as a result of result) {Console.WriteLine (results); }    

found the problem I was creating x86 or x64 or any Was obviously x86 default and of course server-2008 was happy to run it in wow, but the server admin module is not available in that context.

No comments:

Post a Comment