powershell - 安装支持 PowerShell 2.0 和 3.0 的 PowerShell 模块

标签 powershell

我有一个将安装在多台服务器上的 PowerShell 模块。该模块本身利用 PowerShell 2.0 功能,但需要能够在 PowerShell 2.0 环境和 PowerShell 3.0 环境中导入。

我想将单个模块安装到可支持 PowerShell 2.0 或 3.0 的 PSModulePath 中。就目前而言,2.0 模块 list 如下所示:

@{
    ModuleToProcess = "Foo.psm1" # Specific to PowerShell 2.0
    ModuleVersion = "1.0"
    GUID = "8C0AB478-81F7-4B54-B440-521201EAEC1C"
    PowerShellVersion = "2.0"
    FunctionsToExport = "*"
    CmdletsToExport = "*"
    AliasesToExport = "*"
    VariablesToExport = "*"
}

不幸的是,在 3.0 中他们删除了 ModuleToProcess并将其替换为 RootModule :

@{
    RootModule = "Foo.psm1" # Specific to PowerShell 3.0
    ModuleVersion = "1.0"
    GUID = "8C0AB478-81F7-4B54-B440-521201EAEC1C"
    PowerShellVersion = "2.0"
    FunctionsToExport = "*"
    CmdletsToExport = "*"
    AliasesToExport = "*"
    VariablesToExport = "*"
}

我的模块当前已部署,因此它们看起来像:

C:\FooModules
  Foo-v1.0
    Foo
      Foo.psm1
      Foo.psd1
  Foo-v1.3
    Foo
      Foo.psm1
      Foo.psd1

我的 PSModulePath 配置为包含 C:\FooModules\Foo-v1.0C:\Foomodules\Foo-v1.3 .

如果我只针对 2.0 或 3.0,这将非常有效。但情况是我需要同时支持两者。有没有办法做到这一点,除了编写一整套预处理来安装 2.0 或 3.0 特定 list 之外?我真的不想做这样的事情。

最佳答案

据我所知,v2 list 和 v3 list 之间没有必要的更改。使用 v2 list 来保持向后兼容性。

ModuleToProcess 在 v3 中工作得很好。

Note: In Windows PowerShell 2.0, this key was called "ModuleToProcess." You can use the "RootModule" parameter name or its "ModuleToProcess" alias.

来源:Get-Help New-ModuleManifest -Parameter RootModule

关于powershell - 安装支持 PowerShell 2.0 和 3.0 的 PowerShell 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20859486/

相关文章:

windows - 当进程不存在时如何使用powershell杀死进程而不会出错

powershell - Windows 7 SP1 上的 Powershell v4 中缺少 CMDLETS

windows - 通过 Powershell 或 Batch 将文件从 Windows 转换为 UNIX

powershell - 新-AzureRmVM : Parameter set cannot be resolved using the specified named parameters

powershell - 未引用的关键规则和最佳实践

string - 在Powershell中迭代字符串的通用列表(Lync聊天室成员)

powershell - 如何找出 : Domain Administrator or Not?

powershell - 使用Oauth凭据的New-PSSession Cmdlet

Powershell: boolean 函数的命名约定