You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#开发的netstandard2.0二进制模块中判断PowerShell版本

在.NET Standard 2.0 PowerShell二进制模块中区分Windows PowerShell 5.1与PowerShell 7+

在PowerShell脚本场景中,我们可以借助自动变量快速区分运行环境是Windows PowerShell 5.1还是PowerShell 7及以上版本:

脚本中的实现方式

方式1:通过$PSVersionTable判断版本号

if ($PSVersionTable.PSVersion -ge '7.0') {
    # 执行PowerShell 7+相关操作
    return
}

# 执行PowerShell 5.1相关操作

方式2:通过$IsCoreCLR判断运行时

if ($IsCoreCLR) {
    # 执行PowerShell 7+相关操作
    return
}

# 执行PowerShell 5.1相关操作

二进制模块中的实现方案

针对基于.NET Standard 2.0开发的PowerShell二进制模块(项目文件参考如下),可以通过以下几种方式实现版本区分:

参考项目文件

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>library</OutputType>
    <TargetFramework>netstandard2.0</TargetFramework>
    <Nullable>enable</Nullable>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="PowerShellStandard.Library" Version="5.1.1" PrivateAssets="all" />
  </ItemGroup>

</Project>

方案1:通过PSVersionInfo获取版本号

直接利用System.Management.Automation.PSVersionInfo类的静态属性,和脚本逻辑保持一致:

using System.Management.Automation;

public class SampleCmdlet : Cmdlet
{
    protected override void ProcessRecord()
    {
        if (PSVersionInfo.PSVersion >= new Version(7, 0))
        {
            // 执行PowerShell 7+专属逻辑
            WriteObject("当前环境为PowerShell 7及以上版本");
            return;
        }

        // 执行Windows PowerShell 5.1专属逻辑
        WriteObject("当前环境为Windows PowerShell 5.1");
    }
}

方案2:检测CoreCLR运行时

Windows PowerShell 5.1基于.NET Framework,而PowerShell 7+基于.NET Core/.NET 5+系列,因此可以通过运行时描述判断:

using System.Runtime.InteropServices;
using System.Management.Automation;

public class SampleCmdlet : Cmdlet
{
    protected override void ProcessRecord()
    {
        string frameworkDesc = RuntimeInformation.FrameworkDescription;
        bool isCoreRuntime = frameworkDesc.StartsWith(".NET Core") || 
                             frameworkDesc.StartsWith(".NET ") && !frameworkDesc.Contains("Framework");

        if (isCoreRuntime)
        {
            // 执行PowerShell 7+专属逻辑
            WriteObject("当前环境为PowerShell 7及以上版本");
            return;
        }

        // 执行Windows PowerShell 5.1专属逻辑
        WriteObject("当前环境为Windows PowerShell 5.1");
    }
}

方案3:获取PowerShell自动变量$IsCoreCLR

通过Cmdlet的会话状态直接获取PowerShell内置的$IsCoreCLR变量:

using System.Management.Automation;

public class SampleCmdlet : Cmdlet
{
    protected override void ProcessRecord()
    {
        bool? isCoreClr = SessionState.PSVariable.GetValue("IsCoreCLR") as bool?;

        if (isCoreClr.HasValue && isCoreClr.Value)
        {
            // 执行PowerShell 7+专属逻辑
            WriteObject("当前环境为PowerShell 7及以上版本");
            return;
        }

        // 执行Windows PowerShell 5.1专属逻辑
        WriteObject("当前环境为Windows PowerShell 5.1");
    }
}

内容的提问来源于stack exchange,提问作者Santiago Squarzon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 20:23:12