简体   繁体   English

如何将版本信息添加到我的 powershell 脚本中?

[英]How to add version info to my powershell script?

I have a script, test.ps1 , shown below.我有一个脚本test.ps1 ,如下所示。 But I would like to be able to run: .\\test.ps1 -version and have it return the current version of the script to me.但我希望能够运行: .\\test.ps1 -version并让它将脚本的当前版本返回给我。

Is there a way to do this?有没有办法做到这一点?

<#
.SYNOPSIS

Test
  
.DESCRIPTION

 Desc

.INPUTS

 None

.OUTPUTS

  None
  
.NOTES

  Author  : me
  Version : 1.0
  Purpose : PowerShell script test
  
#>

This did not work.这没有用。

I would like to be able to run a .\\test.ps1 -version and have it return the current version of the script to me.我希望能够运行 .\\test.ps1 -version 并将脚本的当前版本返回给我。 Is there a way to do this?有没有办法做到这一点?

You can achieve this using a non-mandatory switch parameter to output the NOTES in your comment-based help.您可以使用非强制性开关参数来实现此目的,以在基于注释的帮助中输出NOTES Here's an example:下面是一个例子:

<#
.SYNOPSIS

Test

.DESCRIPTION

Desc

.INPUTS

None

.OUTPUTS

None

.NOTES

    Author : me 
    Version : 1.0 
    Purpose : PowerShell script test

#>

param(
    [parameter(Mandatory=$false, HelpMessage="Display script version")]
    [switch]
    $version
)

begin {
    if ($version) {
        (Get-Help $MyInvocation.InvocationName -Full).PSExtended.AlertSet
        exit
    }
}
process { }
end { }

Now, when you run the following:现在,当您运行以下命令时:

.\test1.ps1 -version

...you'll see your NOTES: ...你会看到你的笔记:

Author : me
Version : 1.0
Purpose : PowerShell script test

In addition, users can also see your version information (NOTES) using:此外,用户还可以使用以下方式查看您的版本信息 (NOTES):

Get-Help .\test1.ps1 -Full

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM