为什么PowerShell的类不加载管理单元 [英] Why does PowerShell class not load a snapin

查看:287
本文介绍了为什么PowerShell的类不加载管理单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个code中,我(在这种情况下,从MS动态NAV)加载的管理单元:

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();
            }
        }

当我使用AddScript方法,如在code这code工作。 但是,为什么AddCommand方法无法正常工作(见注释code)?看起来没有装载管理单元,因为错误表示,将Get-NAVServerInstance小命令是未知的。 这是如何工作的?

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?

我知道我可以创建一个运行空间与我所导入的管理单元的InitialSessionState。然后ps.AddCommand(GET-NAVServerInstance)的工作。 但是,当我想创建远程运行空间会话(使用WSManConnectionInfo)我不能找到一种方法来提供一个initialSessionState。

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.

更新: 这样看来,只有AddCommand可以用于当运行空间被打开可用小命令(或产生?)。使用InitialSessionState或RunspaceConfiguration实例与RunspaceFactory.CreateRunspace(...)就行了。所以这个code工作:

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();
        }
    }

但我的问题是,在这种情况下,我不能指定WSManConnectionInfo实例。

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?

推荐答案

我终于找到了一丝如何配置远程会话(见 HTTP: //superuser.com/a/518567 )。

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

您需要与注册在远程计算机上的会话配置

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

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

然后就可以设置WSManConnectionInfo的shellUri参数 http://schemas.microsoft.com/powershell/ MyShell

的运行空间创建这种方式将其由MyInitScript.ps1启动脚本导入可用的命令。

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

所以,现在这个code将工作:

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();
            }
        }

这篇关于为什么PowerShell的类不加载管理单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆