创建powershell参数默认值为当前目录

时间:2015-06-17 14:00:15

标签: powershell powershell-v3.0

我希望创建一个参数,其默认值是'当前目录'(.)。

例如,Path的{​​{1}}参数:

Get-ChildItem
  

-Path           指定一个或多个位置的路径。允许使用通配符。默认位置是当前           目录(。)。

PS> Get-Help Get-ChildItem -Full

我创建了一个带有 Required? false Position? 1 Default value Current directory Accept pipeline input? true (ByValue, ByPropertyName) Accept wildcard characters? true 参数的函数,该参数接受来自管道的输入,默认值为Path

.

但是,<# .SYNOPSIS Does something with paths supplied via pipeline. .PARAMETER Path Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.). #> Function Invoke-PipelineTest { [cmdletbinding()] param( [Parameter(Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)] [string[]]$Path='.' ) BEGIN {} PROCESS { $Path | Foreach-Object { $Item = Get-Item $_ Write-Host "Item: $Item" } } END {} } 不会被解释为帮助中的“当前目录”:

.
  

-Path           指定一个或多个位置的路径。允许使用通配符。默认位置是当前目录(。)。

PS> Get-Help Invoke-PipelineTest -Full

Required? false Position? 1 Default value . Accept pipeline input? true (ByValue, ByPropertyName) Accept wildcard characters? false 参数的默认值设置为当前目录的正确方法是什么?

顺便提一下,在哪里设置Path属性?

1 个答案:

答案 0 :(得分:7)

使用PSDefaultValue属性为默认值定义自定义说明。使用SupportsWildcards属性将参数标记为Accept wildcard characters?

<#
.SYNOPSIS
Does something with paths supplied via pipeline.
.PARAMETER Path
Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).
#>
Function Invoke-PipelineTest {
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
        [PSDefaultValue(Help='Description for default value.')]
        [SupportsWildcards()]
        [string[]]$Path='.'
    )
}