このトピックには、スクリプトと関数にコメントベースのヘルプを使用する方法を示す例が含まれています。
例 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
#>
}
次の出力は、Add-Extension 関数のヘルプを表示する Get-Help コマンドの結果を示しています。
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 ステートメントがないスクリプトでは、ヘルプ トピックの最後のコメントと最初の関数宣言の間に少なくとも 2 行の空白行が必要です。 これらの空白行がない場合、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:\Reports\2009\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:\Reports\2009\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 関数の先頭からの抜粋であり、ヘルプ テキストの画面が一度に 1 つ表示されます。 Get-Help コマンドレットのヘルプ トピックにはヘルプ関数が記述されているため、ヘルプ関数は .FORWARDHELPTARGETNAME キーワードと .FORWARDHELPCATEGORY キーワードを使用して、ユーザーを Get-Help コマンドレットのヘルプ トピックにリダイレクトします。
function help
{
<#
.FORWARDHELPTARGETNAME Get-Help
.FORWARDHELPCATEGORY Cmdlet
#>
[CmdletBinding(DefaultParameterSetName='AllUsersView')]
param(
[Parameter(Position=0, ValueFromPipelineByPropertyName=$true)]
[System.String]
${Name},
...
}
次のコマンドでは、この機能を使用します。 ユーザーが help 関数の Get-Help コマンドを入力すると、Get-HelpGet-Help コマンドレットのヘルプ トピックが表示されます。
PS> Get-Help help
NAME
Get-Help
SYNOPSIS
Displays information about Windows PowerShell cmdlets and concepts.
...
PowerShell