iT邦幫忙

2021 iThome 鐵人賽

DAY 14
0
自我挑戰組

Powershell 入门系列 第 14

Powershell 入门添加参数帮助信息

  • 分享至 

  • xImage
  •  

我们写的脚本不仅仅是自己使用,有时需要分享给别人使用。这种情况下,帮助信息可以更好地帮助使用者,使用你的脚本。

今天我们就一起来看一下,如果给脚本添加帮助信息。

示例脚本:
我们通过在 parameter 中添加 Helpmessage 来添加帮助信息。

function test-net_port {
    [cmdletbinding()]
    param(
        [parameter(mandatory=$true,Helpmessage="Enter the name of a computer to check connectivity to.")]
        [string[]]$computerName,
        [parameter(mandatory=$false)]
        [int] $port
    )
    foreach ($computer in $computerName) {
        Write-Verbose "Now testing $computer."
        if ($port -eq "")
        {
            $ping = Test-NetConnection -ComputerName $computer -InformationLevel Quiet -WarningAction SilentlyContinue
        } else {
            $ping = Test-NetConnection -ComputerName $computer -InformationLevel Quiet -WarningAction SilentlyContinue -Port $port
        }
        if ($ping){
            Write-Output $ping
        } else {
            Write-Verbose "Ping failed on $computer. Check the network connection."
            Write-Output $ping
        }
    }
}

在 ISE 中运行脚本:

PS C:\Users\v-peizhiyu> test-net_port
cmdlet test-net_port at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
computerName[0]: !?
Enter the name of a computer to check connectivity to.
computerName[0]: 

上一篇
Powershell 入门之 可选参数和必选参数
下一篇
Powershell 入门参数属性(1)
系列文
Powershell 入门21
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言