Quantcast
Channel: Project Customization and Programming forum
Viewing all articles
Browse latest Browse all 5347

exceptions for Importing a Powershell Module from C#.Net

$
0
0

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:

 

  1. 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





Viewing all articles
Browse latest Browse all 5347

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>