[玩转系统] 基于注释的帮助示例
作者:精品下载站 日期:2024-12-14 02:32:22 浏览:13 分类:玩电脑
基于注释的帮助示例
本主题包含演示如何对脚本和函数使用基于注释的帮助的示例。
示例 1:基于注释的函数帮助
以下示例函数包括基于注释的帮助。
function Add-Extension
{
param ([string]$Name,[string]$Extension = "txt")
$name = $name + "." + $extension
$name
<#
.SYNOPSIS
Adds a file name extension to a supplied name.
.DESCRIPTION
Adds a file name extension to a supplied name.
Takes any strings for the file name or extension.
.PARAMETER Name
Specifies the file name.
.PARAMETER Extension
Specifies the extension. "Txt" is the default.
.INPUTS
None. You can't pipe objects to Add-Extension.
.OUTPUTS
System.String. Add-Extension returns a string with the extension or file name.
.EXAMPLE
PS> Add-Extension -name "File"
File.txt
.EXAMPLE
PS> Add-Extension -name "File" -extension "doc"
File.doc
.EXAMPLE
PS> Add-Extension "File" "doc"
File.doc
.LINK
Online version: http://www.fabrikam.com/add-extension.html
.LINK
Set-Item
#>
}
以下输出显示了 Get-Help
命令的结果,该命令显示了 Add-Extension
函数的帮助。
PS> Get-Help Add-Extension -full
NAME
Add-Extension
SYNOPSIS
Adds a file name extension to a supplied name.
SYNTAX
Add-Extension [[-Name] <String>] [[-Extension] <String>] [<CommonParameters>]
DESCRIPTION
Adds a file name extension to a supplied name. Takes any strings for the file name or extension.
PARAMETERS
-Name
Specifies the file name.
Required? false
Position? 0
Default value
Accept pipeline input? false
Accept wildcard characters?
-Extension
Specifies the extension. "Txt" is the default.
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters?
<CommonParameters>
This cmdlet supports the common parameters: -Verbose, -Debug,
-ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
-OutBuffer and -OutVariable. For more information, type
"get-help about_commonparameters".
INPUTS
None. You can't pipe objects to Add-Extension.
OUTPUTS
System.String. Add-Extension returns a string with the extension or file name.
-------------------------- EXAMPLE 1 --------------------------
PS> Add-Extension -name "File"
File.txt
-------------------------- EXAMPLE 2 --------------------------
PS> Add-Extension -name "File" -extension "doc"
File.doc
-------------------------- EXAMPLE 3 --------------------------
PS> Add-Extension "File" "doc"
File.doc
RELATED LINKS
Online version: http://www.fabrikam.com/add-extension.html
Set-Item
示例 2:基于注释的脚本帮助
以下示例函数包括基于注释的帮助。
请注意结束 #>
和 Param
语句之间的空行。在没有 Param
语句的脚本中,帮助主题中的最后一个注释和第一个函数声明之间必须至少有两个空行。如果没有这些空行,Get-Help
会将帮助主题与函数(而不是脚本)关联起来。
<#
.SYNOPSIS
Performs monthly data updates.
.DESCRIPTION
The Update-Month.ps1 script updates the registry with new data generated
during the past month and generates a report.
.PARAMETER InputPath
Specifies the path to the CSV-based input file.
.PARAMETER OutputPath
Specifies the name and path for the CSV-based output file. By default,
MonthlyUpdates.ps1 generates a name from the date and time it runs, and
saves the output in the local directory.
.INPUTS
None. You can't pipe objects to Update-Month.ps1.
.OUTPUTS
None. Update-Month.ps1 doesn't generate any output.
.EXAMPLE
PS> .\Update-Month.ps1
.EXAMPLE
PS> .\Update-Month.ps1 -inputpath C:\Data\January.csv
.EXAMPLE
PS> .\Update-Month.ps1 -inputpath C:\Data\January.csv -outputPath C:\Reports09\January.csv
#>
param ([string]$InputPath, [string]$OutPutPath)
function Get-Data { }
以下命令获取脚本帮助。由于脚本不在 Path 环境变量中列出的目录中,因此获取脚本帮助的 Get-Help
命令必须指定脚本路径。
PS> Get-Help c:\ps-test\update-month.ps1 -full
NAME
C:\ps-test\Update-Month.ps1
SYNOPSIS
Performs monthly data updates.
SYNTAX
C:\ps-test\Update-Month.ps1 [-InputPath] <String> [[-OutputPath]
<String>] [<CommonParameters>]
DESCRIPTION
The Update-Month.ps1 script updates the registry with new data
generated during the past month and generates a report.
PARAMETERS
-InputPath
Specifies the path to the CSV-based input file.
Required? true
Position? 0
Default value
Accept pipeline input? false
Accept wildcard characters?
-OutputPath
Specifies the name and path for the CSV-based output file. By
default, MonthlyUpdates.ps1 generates a name from the date
and time it runs, and saves the output in the local directory.
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters?
<CommonParameters>
This cmdlet supports the common parameters: -Verbose, -Debug,
-ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
-OutBuffer and -OutVariable. For more information, type,
"get-help about_commonparameters".
INPUTS
None. You can't pipe objects to Update-Month.ps1.
OUTPUTS
None. Update-Month.ps1 doesn't generate any output.
-------------------------- EXAMPLE 1 --------------------------
PS> .\Update-Month.ps1
-------------------------- EXAMPLE 2 --------------------------
PS> .\Update-Month.ps1 -inputpath C:\Data\January.csv
-------------------------- EXAMPLE 3 --------------------------
PS> .\Update-Month.ps1 -inputpath C:\Data\January.csv -outputPath
C:\Reports09\January.csv
RELATED LINKS
示例 3:Param 语句中的参数描述
此示例演示如何在函数或脚本的 Param
语句中插入参数描述。当参数描述简短时,此格式最有用。
function Add-Extension
{
param
(
[string]
# Specifies the file name.
$name,
[string]
# Specifies the file name extension. "Txt" is the default.
$extension = "txt"
)
$name = $name + "." + $extension
$name
<#
.SYNOPSIS
Adds a file name extension to a supplied name.
...
#>
}
结果与示例 1 的结果相同。Get-Help
将参数描述解释为带有 .Parameter
关键字。
示例 4:重定向到 XML 文件
您可以为函数和脚本编写基于 XML 的帮助主题。虽然基于注释的帮助更容易实现,但如果您希望更精确地控制帮助内容或将帮助主题翻译成多种语言,则需要基于 XML 的帮助。以下示例显示 Update 的前几行-Month.ps1
脚本。该脚本使用 .ExternalHelp
关键字指定脚本的基于 XML 的帮助主题的路径。
# .ExternalHelp C:\MyScripts\Update-Month-Help.xml
param ([string]$InputPath, [string]$OutPutPath)
function Get-Data { }
以下示例显示了在函数中使用 .ExternalHelp
关键字。
function Add-Extension
{
param ([string] $name, [string]$extension = "txt")
$name = $name + "." + $extension
$name
# .ExternalHelp C:\ps-test\Add-Extension.xml
}
示例 5:重定向到不同的帮助主题
以下代码摘自 PowerShell 中内置 Help
函数的开头部分,该函数一次显示一屏帮助文本。由于 Get-Help cmdlet 的帮助主题描述了帮助功能,因此帮助函数使用 .ForwardHelpTargetName
和 .ForwardHelpCategory
关键字将用户重定向到 Get-Help cmdlet帮助主题。
function help
{
<#
.FORWARDHELPTARGETNAME Get-Help
.FORWARDHELPCATEGORY Cmdlet
#>
[CmdletBinding(DefaultParameterSetName='AllUsersView')]
param(
[Parameter(Position=0, ValueFromPipelineByPropertyName=$true)]
[System.String]
${Name},
...
}
以下命令使用此功能。当用户为 Help
功能键入 Get-Help
命令时,Get-Help
显示 Get-Help 的帮助主题
cmdlet。
PS> get-help help
NAME
Get-Help
SYNOPSIS
Displays information about Windows PowerShell cmdlets and concepts.
...
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag