cra*_*aig 3 powershell powershell-3.0
我希望创建一个参数,其默认值是'当前目录'(.).
例如,Path参数Get-ChildItem:
PS> Get-Help Get-ChildItem -Full
-Path指定一个或多个位置的路径.允许使用通配符.默认位置是当前目录(.).
Run Code Online (Sandbox Code Playgroud)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 {}
}
但是,.在帮助中不会将其解释为"当前目录":
PS> Get-Help Invoke-PipelineTest -Full
-Path指定一个或多个位置的路径.允许使用通配符.默认位置是当前目录(.).
Run Code Online (Sandbox Code Playgroud)Required? false Position? 1 Default value . Accept pipeline input? true (ByValue, ByPropertyName) Accept wildcard characters? false
将Path参数的默认值设置为当前目录的正确方法是什么?
顺便说一句,一个人在哪里设置Accept wildcard character财产?
使用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='.'
    )
}
| 归档时间: | 
 | 
| 查看次数: | 1398 次 | 
| 最近记录: |