【问题标题】:Unable to create a Powershell Alias in a binary Module无法在二进制模块中创建 Powershell 别名
【发布时间】:2013-01-09 12:18:55
【问题描述】:

我想从我创建的二进制模块中导出一个别名。对于脚本模块,您可以使用 Export-ModuleMember。二进制模块是否有等价物?

我的清单 (.psd1) 如下所示:

@{
    ModuleToProcess = 'MyModule.psm1'
    NestedModules = 'MyModule.dll'
    ModuleVersion = '1.0'
    GUID = 'bb0ae680-5c5f-414c-961a-dce366144546'
    Author = 'Me'
    CompanyName = 'ACME'
    Copyright = '© ACME'
} 

编辑Keith Hill 提供了一些帮助,但仍然无济于事。这是所有涉及的文件

我的模块脚本(.psm1):

export-modulemember -function Get-TestCommand -alias gtc

最后,我的 DLL 中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;

namespace MyModule
{
    [Cmdlet(VerbsCommon.Get, "TestCommand")]
    [OutputType(typeof(string))]
    public class GetTestCommand : PSCmdlet
    {
        protected override void ProcessRecord()
        {
            WriteObject("One");
            WriteObject("Two");
            WriteObject("Three");
        }
    }
}

如果我有这个并启动 PowerShell,然后import-module MyModule 最后运行get-module,我会得到这个:

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Script     MyModule                  {}

如果我注释掉 psm1 文件中的 export-modulemember 位并重复上述步骤,我会得到:

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Script     MyModule                  Get-TestCommand

那么,我在这里做错了什么?

【问题讨论】:

  • Get-Module 的输出默认不显示别名。试试这个:Get-Module MyModule | Foreach {$_.ExportedAliases} 或简单的gmo MyModule | fl
  • 与我有关的是缺少的 ExportedCommands,而不是别名
  • PSD1 文件中的 CmdletsToExport 设置在哪里?也不要使用 Export-ModuleMember 从 PSM1 文件中导出 cmdlet。

标签: powershell


【解决方案1】:

执行此操作的典型方法是将 .PSM1 设为 ModuleToProcess 并将 Set-Alias / Export-ModuleMember -Alias * 放入该 PSM1 文件中。然后将您的 DLL 引用放入 PSD1 的 NestedModules 成员中,例如:

@{
    ModuleToProcess = 'MyModule.psm1'
    NestedModules   = 'MyModule.dll'
    ModuleVersion = '1.0'
    GUID = 'bb0ae680-5c5f-414c-961a-dce366144546'
    Author = 'Me'
    CompanyName = 'ACME'
    Copyright = '© ACME'
    FormatsToProcess = 'MyModule.format.ps1xml'
    AliasesToExport = '*'
    CmdletsToExport = @(
        'Get-Foo',
        'Set-Foo',
        ...
    )
} 

【讨论】:

  • 我试过了,但没有任何运气。如果我将 .psm1 文件完全留空,我在 .dll 中的函数仍然可以正确导出。只要我在 .psm1 中放置一个导出模块,无论我给出什么参数,我的函数都不再导出。
  • .PSM1 的 Export-ModuleMember 不应影响从 DLL 导出的内容。这由清单文件的CmdletsToExport = @('get-foo', 'set-foo', ...) 成员控制。顺便说一句,虽然您可以只使用“*”,但您的模块将更好地使用 PowerShell V3 的新模块自动加载功能。
  • 不,仍然没有快乐。我已更新问题以反映当前状态
  • 好吧,我只能说,这就是我们为 PowerShell 社区扩展做这件事的方式。在那里工作得很好。你可以在这里看到 PSD1:pscx.codeplex.com/SourceControl/changeset/view/100892#1357945 和 PSM1:pscx.codeplex.com/SourceControl/changeset/view/100892#1011399
  • 谢谢基思。您的帮助非常宝贵
【解决方案2】:

好的,我现在已经开始工作了。事实证明,我犯了一些错误,所有这些都导致了这个问题。

  • 问题 1:我不太了解 PowerShell!
  • 问题 2:我混淆了函数和 Cmdlet。我需要指定Export-ModuleMember -Cmdlet Get-TestCommand,而不是Export-ModuleMember -Function Get-TestCommand。这解释了为什么每次我取消注释 Export-ModuleMember 行时我的 ExportedCommand 都会消失。 Keith Hill 的 PSCE 文件链接帮助我发现了这一点。

    Functionality in a DLL = -Cmdlet
    Functionality in .psm1 = -Function
    
  • 问题 3:如果您没有定义任何别名,导出别名也无济于事。您需要先使用Set-Alias 设置别名,然后使用Export-Module 将其导出。这是我的一个愚蠢的错误

所以,最后,将我的 .psm1 文件更改为如下所示的文件解决了问题

Set-Alias gtc Get-TestCommand
Export-ModuleMember -Alias * -Function * -Cmdlet *

我要感谢 Keith 的回答。正是因为他的努力,我才能做到这一点

【讨论】:

    猜你喜欢
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多