【问题标题】:Use .pwsh instead of .ps1 extension for PowerShell Core scripts?对 PowerShell Core 脚本使用 .pwsh 而不是 .ps1 扩展名?
【发布时间】:2020-01-02 09:44:31
【问题描述】:

我同时使用PowerShell 和PowerShell Core。 并非我为“大”PowerShell 编写的所有脚本都在 PowerShell Core 中工作,反之亦然。

为了避免混乱,我在考虑是否应该使用两个文件扩展名:

  • .ps1: PowerShell
  • .pwsh: PowerShell 核心

在 Windows 中,扩展比在 shebang (#!/bin/mytool) 有助于解决问题的 Unix 系统中更为重要。 p>

使用两个扩展是“最佳实践”还是有更好的选择?

附加:我不确定当我从命令行/终端调用脚本时,.pwsh 脚​​本是否会被 PowerShell 执行。

我在 Unix / Linux 上下文中发现了一个类似的问题。 https://unix.stackexchange.com/questions/182882/use-sh-or-bash-extension-for-bash-scripts

【问题讨论】:

    标签: powershell file-extension powershell-core


    【解决方案1】:

    添加一行

    #requires -PSEdition Core
    

    在 PowerShell Core 脚本的开头和一行

    #requires -PSEdition Desktop
    

    在常规 PowerShell 脚本的开头。

    对于在两个版本中运行的脚本,只需省略该行。

    来自documentation:

    -PSEdition <PSEdition-Name>

    指定脚本所需的 PowerShell 版本。 PowerShell Core 的有效值为 Core,Windows PowerShell 的有效值为 Desktop。

    不要使用不同的扩展,因为这会影响功能。例如,PowerShell 不会使用 .ps1 以外的扩展名对脚本进行点源处理。

    【讨论】:

    • 谢谢。有用 :-)。当脚本适用于两个版本时,请跳过该行,不是吗?我也会保留扩展名 .ps1
    • “当脚本适用于两个版本时,就跳过这一行,不是吗?”正确。
    猜你喜欢
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    相关资源
    最近更新 更多