【问题标题】:c# - How to get the PowerShell module version from inside the module code at runtimec# - 如何在运行时从模块代码中获取 PowerShell 模块版本
【发布时间】:2021-03-27 05:30:55
【问题描述】:

我有一个用于实现 PowerShell 命令的 C# PSCmdlet 类,我想在运行命令时获取我的模块版本。

我不想从程序集位置获取版本,因为我需要加载实际版本(它可能会有所不同,例如,如果我在升级我的模块时保持 PowerShell 打开,则程序集将指向升级后的版本而且我不会得到已经加载的那个)。

对于当前会话,我需要 Get-Module 之类的东西,但来自我的 C# 命令代码。

我该怎么做?

【问题讨论】:

  • 在您的每个会话中,加载所有默认/需要的模块和其他内容,然后在您的会话中将其与您在该会话中加载的内容进行比较。

标签: c# powershell module version pscmdlet


【解决方案1】:

由于我在差不多一年后一直在同一个十字军东征中,所以我想出了:

$version = Split-Path -Leaf $MyInvocation.MyCommand.ScriptBlock.Module.ModuleBase

【讨论】:

    【解决方案2】:

    根据我的评论详细说明。所以,在 PowerShell 中,可能是这样的......

    # Put this in your profile (ISE/PowerShell/VSCode)
    $AutomaticModules     = Get-Module
    
    $AutomaticModules
    # Results
    <#
    ModuleType Version    Name                                ExportedCommands
    ---------- -------    ----                                ----------------
    Binary     1.0.0.0    CimCmdlets                          {Export-BinaryMiLog, ...
    Script     1.1.0      ClassExplorer                       {Find-Member, Find-Na...
    ...
    #>
    
    # Get only modules loaded during the session
    Compare-Object -ReferenceObject (Get-Module) -DifferenceObject $AutomaticModules -Property Name -PassThru |
    Where -Property Name -ne 'AutomaticModules'
    
    # Results
    <#
    ModuleType Version    Name                                ExportedCommands
    ---------- -------    ----                                ----------------
    Manifest   3.0.0.0    Microsoft.PowerShell.Security       {ConvertFrom-SecureString, ...
    Manifest   3.0.0.0    Microsoft.WSMan.Management          {Connect-WSMan, Disable-WSMa...
    #>
    
    Import-Module -Name IsePester
    
    Compare-Object -ReferenceObject (Get-Module) -DifferenceObject $AutomaticModules -Property Name -PassThru |
    Where -Property Name -ne 'AutomaticModules'
    
    # Results
    <#
    ModuleType Version    Name                                ExportedCommands
    ---------- -------    ----                                ----------------
    Script     0.0        IsePester                           {Add-PesterMenu, Get-PesterM...
    Manifest   3.0.0.0    Microsoft.PowerShell.Security       {ConvertFrom-SecureString, C...
    Manifest   3.0.0.0    Microsoft.WSMan.Management          {Connect-WSMan, Disable-WSMa...
    #>
    

    所以,作为一个函数:

    function Get-SessionModule
    {
        Param
        (
            [String]$ModuleName
        )
    
        (Compare-Object -ReferenceObject (Get-Module) -DifferenceObject $AutomaticVModules -Property Name -PassThru |
        Where -Property Name -ne 'AutomaticVModules') -Match $ModuleName
    }
    
    Get-SessionModule -ModuleName IsePester
    # Results
    <#
    ModuleType Version    Name                                ExportedCommands
    ---------- -------    ----                                ----------------
    Script     0.0        IsePester                           {Add-PesterMenu, Ge... 
    #>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-08
      • 2021-07-24
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 2014-09-16
      相关资源
      最近更新 更多