2
votes

I have seen quite a number of solutions, but none addresses my problem. I am trying to import a custom PowerShell module called "DSInternals" to my C# DLL.

https://github.com/MichaelGrafnetter/DSInternals

Everything in my code seems just fine, but when I try to get the available module it's not loaded.

The stream responds with

The term 'Get-ADReplAccount' 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.

Where Am I going wrong with this code?

InitialSessionState init = InitialSessionState.CreateDefault();
init.ImportPSModule(new string[] { @"D:\\DSInternals\\dsinternals.psd1" }); //location of the module files
Runspace runspace = RunspaceFactory.CreateRunspace(init);
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.Commands.AddCommand("Get-ADReplAccount"); //this command is un-recognized

foreach (PSObject result in ps.Invoke())
{
    Console.WriteLine(result); //this always returns null
}
1
this command is un-recognized How do you know that? Any exception thrown? Any error in ps.Streams.Error? - user4003407
The streams responds with "The term 'Get-ADReplAccount' 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.". This cmdlets is included by the DSInternals module - Patrizio Mwange
Add this to your code: init.ThrowOnRunspaceOpenError=true;. - user4003407
Thanks alot @PetSerAl Just what I needed to get back on track. - Patrizio Mwange

1 Answers

0
votes

The issue was the .NET framework version in which the module was built in. Adding a module which was built with a higher version of the .NET framework to the C# class will not work. The module was built in 4.5.1 and I was working with version 2, adding

init.ThrowOnRunspaceOpenError=true;

Helped in catching the cause of the error.

Here is my final code that works

        InitialSessionState init = InitialSessionState.CreateDefault();
        init.ImportPSModule(new string[] { @"D:\\DSInternals\\dsinternals.psd1" }); //location of the module files
        init.ThrowOnRunspaceOpenError = true;
        Runspace runspace = RunspaceFactory.CreateRunspace(init);
        runspace.Open();
        var script =
            "Get-ADReplAccount -SamAccountName peter -Domain BLABLA -Server dc.BLABLA.co.za -Credential $cred -Protocol TCP"; //my powershell script

        _powershell = PowerShell.Create().AddScript(script);
        _powershell.Runspace = runspace;

        var results = _powershell.Invoke();
        foreach (var errorRecord in _powershell.Streams.Progress)
            Console.WriteLine(errorRecord);
        foreach (var errorRecord in _powershell.Streams.Debug)
            Console.WriteLine(errorRecord);
        foreach (var errorRecord in _powershell.Streams.Error)
            Console.WriteLine(errorRecord);
        foreach (var errorRecord in _powershell.Streams.Warning)
            Console.WriteLine(errorRecord);

        var stringBuilder = new StringBuilder();
        foreach (var obj in results)
        {
            stringBuilder.AppendLine(obj.ToString());
        }

        return stringBuilder.ToString();