How to add version info to my powershell script?

蓝咒 提交于 2020-12-12 01:49:13

问题


I have a script, test.ps1, shown below. But I would like to be able to run: .\test.ps1 -version and have it return the current version of the script to me.

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.


回答1:


I would like to be able to run a .\test.ps1 -version and have it return the current version of the script to me. 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. 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:

Get-Help .\test1.ps1 -Full

Hope this helps.



来源:https://stackoverflow.com/questions/60958300/how-to-add-version-info-to-my-powershell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!