【发布时间】: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