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

cra*_*aig 3 powershell powershell-3.0

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

例如,Path参数Get-ChildItem:

PS> Get-Help Get-ChildItem -Full
Run Code Online (Sandbox Code Playgroud)

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

    Required?                    false
    Position?                    1
    Default value                Current directory
    Accept pipeline input?       true (ByValue, ByPropertyName)
    Accept wildcard characters?  true
Run Code Online (Sandbox Code Playgroud)

我创建了一个带有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 {}
}
Run Code Online (Sandbox Code Playgroud)

但是,.在帮助中不会将其解释为"当前目录":

PS> Get-Help Invoke-PipelineTest -Full
Run Code Online (Sandbox Code Playgroud)

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

    Required?                    false
    Position?                    1
    Default value                .
    Accept pipeline input?       true (ByValue, ByPropertyName)
    Accept wildcard characters?  false
Run Code Online (Sandbox Code Playgroud)

Path参数的默认值设置为当前目录的正确方法是什么?

顺便说一句,一个人在哪里设置Accept wildcard character财产?

use*_*407 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='.'
    )
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为微软无法更新文档?*\*叹\** (3认同)