创建powershell参数默认值为当前目录 [英] Create powershell parameter default value is current directory

查看:76
本文介绍了创建powershell参数默认值为当前目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望创建一个默认值为当前目录"(.)的参数.

例如Get-ChildItemPath参数:

PS>Get-Help Get-ChildItem -Full

<块引用>

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

 需要吗?错误的位置?1默认值 当前目录接受管道输入?true (ByValue, ByPropertyName)接受通配符?真的

我创建了一个带有 Path 参数的函数,它接受来自管道的输入,默认值为 .:

<预><代码><#.概要使用通过管道提供的路径执行某些操作..PARAMETER 路径指定一个或多个位置的路径.允许使用通配符.默认位置是当前目录 (.).#>函数调用-PipelineTest {[cmdletbinding()]参数([参数(强制=$False,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)][string[]]$Path='.')开始 {}过程 {$路径|Foreach-对象{$Item = Get-Item $_写主机项目:$项目"}}结尾 {}}

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

PS>Get-Help Invoke-PipelineTest -Full

<块引用>

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

 需要吗?错误的位置?1默认值                .接受管道输入?true (ByValue, ByPropertyName)接受通配符?错误的

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

顺便说一句,在哪里设置接受通配符属性?

解决方案

使用 PSDefaultValue 属性定义默认值的自定义描述.使用SupportsWildcards 属性将参数标记为接受通配符?.

<#.概要使用通过管道提供的路径执行某些操作..PARAMETER 路径指定一个或多个位置的路径.允许使用通配符.默认位置是当前目录 (.).#>函数调用-PipelineTest {[cmdletbinding()]参数([参数(强制=$False,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)][PSDefaultValue(Help='Description for default value.')][支持通配符()][string[]]$Path='.')}

I'm hoping to create a parameter who's default value is the 'current directory' (.).

For example, the Path parameter of Get-ChildItem:

PS> Get-Help Get-ChildItem -Full

-Path Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).

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

I created a function with a Path parameter that accepts input from the pipeline, with a default value of .:

<#
.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 {}
}

However, the . isn't interpreted as the 'current directory' in help:

PS> Get-Help Invoke-PipelineTest -Full

-Path Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).

    Required?                    false
    Position?                    1
    Default value                .
    Accept pipeline input?       true (ByValue, ByPropertyName)
    Accept wildcard characters?  false

What's the right way to set the Path parameter's default value to the current directory?

Incidentally, where does one set the Accept wildcard character property?

解决方案

Use PSDefaultValue attribute to define custom description for default value. Use SupportsWildcards attribute to mark parameter as 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='.'
    )
}

这篇关于创建powershell参数默认值为当前目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆