Question

I have this code in which I load a snapin (from MS Dynamics NAV in this case):

            using (Runspace runspace = RunspaceFactory.CreateRunspace())
        {
            runspace.Open();

            using (var ps = PowerShell.Create())
            {
                ps.Runspace = runspace;

                ps.AddScript("Add-PSSnapin Microsoft.Dynamics.Nav.Management")
                  .AddScript("Get-NAVServerInstance");

                //This does not work. Says unknown cmdlet Get-NAVServerInstance
                //ps.AddCommand("Add-PSSnapin").AddArgument("Microsoft.Dynamics.Nav.Management")
                //  .AddCommand("Get-NAVServerInstance");

                var output = ps.Invoke();
            }
        }

This code works when I use the AddScript method as shown in the code. But why does AddCommand method not work (see commented code)? Looks like the snapin is not loaded, because the error says that the Get-NAVServerInstance cmdlet is unknown. How is this supposed to work?

I know I can create a runspace with an InitialSessionState on which I have imported the snapin. Then the ps.AddCommand("Get-NAVServerInstance") is working. But when I want to create a remote runspace session (using WSManConnectionInfo) I can't find a way to supply an initialSessionState.

UPDATE: So it seems that AddCommand only can be used for cmdlets available when the runspace is opened (or created?). Using an InitialSessionState or RunspaceConfiguration instance with RunspaceFactory.CreateRunspace(...) will do. So this code works:

    var config = RunspaceConfiguration.Create();
    PSSnapInException warning;
    config.AddPSSnapIn("Microsoft.Dynamics.Nav.Management", out warning);

    using (Runspace runspace = RunspaceFactory.CreateRunspace(config))
    {
        runspace.Open();

        using (var ps = PowerShell.Create())
        {
            ps.Runspace = runspace;
            ps.AddCommand("Get-NAVServerInstance");
            var output = ps.Invoke();
        }
    }

But my problem is in that case, that I can't specify a WSManConnectionInfo instance.

So how can I create a runspace with a remote connection with a snapin (installed on the remote machine) loaded? How to supply a configuration for a remote connection?

Was it helpful?

Solution

I finally found a hint how to configure a remote session (see https://superuser.com/a/518567).

You need to register a session configuration on the remote computer with

Register-PSSessionConfiguration -Name MyShell -StartupScript 'MyInitScript.ps1'

Then you can set the shellUri parameter of WSManConnectionInfo to http://schemas.microsoft.com/powershell/MyShell

The runspace you create this way will have the commands available which are imported by the MyInitScript.ps1 startup script.

So now this code will work:

        string shell = "http://schemas.microsoft.com/powershell/MyShell";
        var target = new Uri("https://myserver:port/wsman");
        var secured = new SecureString();
        foreach (char letter in "password")
        {
            secured.AppendChar(letter);
        }
        secured.MakeReadOnly();

        var credential = new PSCredential("username", secured);
        var connectionInfo = new WSManConnectionInfo(target, shell, credential);

        using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
        {
            runspace.Open();

            using (var ps = PowerShell.Create())
            {
                ps.Runspace = runspace;
                ps.AddCommand("Get-NAVServerInstance");
                var output = ps.Invoke();
            }
        }

OTHER TIPS

Try invoking AddScript like so:

.AddScript("...", false)

this will execute the command in the global scope instead of a new local scope.

I think the proper way to do this is to use the RunspaceConfiguration class. It has an AddPSSnapin method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top