hi all,
I write a code in C#.Net for importing a powerShell Module and calling its functions
the code is:
static public string test(string scriptText) { InitialSessionState initial = InitialSessionState.CreateDefault(); initial.ImportPSModule(new[] { "Microsoft.DHCP.PowerShell.Admin" }); Runspace runspace = RunspaceFactory.CreateRunspace(initial); runspace.Open(); RunspaceInvoke invoker = new RunspaceInvoke(runspace); Collection<PSObject> results = invoker.Invoke(scriptText); StringBuilder stringBuilder = new StringBuilder(); foreach (PSObject obj in results) { stringBuilder.Append(obj.ToString()); { return (stringBuilder.ToString()); }
but in runnspace.open(); give this exception:
- Exception:Thrown: “cannot invoke this function because the current host does not implement it.” (System.Management.Automation.Host.HostException)
and in Collection<PSObject> results = invoker.Invoke(scriptText); gives this exception:
Exception:Thrown: The term 'Get-DHCPServer' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.(system.management.Automation.CommandNotFoundException)
Get-DHCPServer win-qkheb9s51i8 : is a function of this module .
while I can Import this to PowerShell and call its functions correctly.
how I can solve these exceptions?
thanks,
mary