【问题标题】:Validate parameter value using another parameter value使用另一个参数值验证参数值
【发布时间】:2016-05-29 10:19:11
【问题描述】:

我正在尝试在运行函数之前验证路径是否存在。 文件夹路径没有默认值,但文件名默认应为template.csv。有没有办法通过 ValidateScript 属性,根据另一个参数值来验证一个参数值?

以下代码返回变量 $TemplateDir 尚未设置的错误。我也不完全确定它是否会测试默认文件名值。

function get-Template {   
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateScript({Test-Path $_})]
        [string]$TemplateDir,

        [Parameter(Mandatory = $false)]
        [ValidateScript({Test-Path ($TemplateDir + "\" + $_)})]
        [string]$TemplateFile = "template.csv"
    )

    ...

}

有什么建议吗?

【问题讨论】:

    标签: powershell powershell-4.0


    【解决方案1】:

    您可以使用DynamicParam 块设置动态参数,这取决于另一个强制参数的值:

    function Get-FilePath
    {
        Param (
            [Parameter(Mandatory = $true, Position = 0)]
            [ValidateScript({Test-Path $_})]
            [string]$TemplateDir
        )
    
        DynamicParam {
            # Set up parameter attribute
            $fileParamAttribute = New-Object System.Management.Automation.ParameterAttribute
            $fileParamAttribute.Position = 3
            $fileParamAttribute.Mandatory = $false
            $fileParamAttribute.HelpMessage = "Please supply a file name"
    
            # Set up ValidateSet param with actual file name values
            $fileValidateParam = New-Object System.Management.Automation.ValidateSetAttribute @(Get-ChildItem $TemplateDir -File |Select-Object -ExpandProperty Name)
    
            # Add the parameter attributes to an attribute collection
            $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
            $attributeCollection.Add($fileParamAttribute)
            $attributeCollection.Add($fileValidateParam)
    
            # Create the actual $TemplateFile parameter
            $fileParam = New-Object System.Management.Automation.RuntimeDefinedParameter('TemplateFile', [string], $attributeCollection)
    
            # Push the parameter(s) into a parameter dictionary
            $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
            $paramDictionary.Add('TemplateFile', $fileParam)
    
            # Return the dictionary
            return $paramDictionary
        }
    
        begin{
            # Check if a value was supplied, otherwise set it
            if(-not $PSBoundParameters.ContainsKey('TemplateFile'))
            {
                $TemplateFile = 'template.csv'
            }
    
            $myPath = Join-Path $TemplateDir -ChildPath $TemplateFile
        }
    
        end {
            return $myPath
        }
    }
    

    这还将为您提供 -TemplateFile 参数的自动制表符完成

    您可以通过Get-Help about_Functions_Advanced_Parameters阅读更多关于DynamicParam的信息

    【讨论】:

    • 谢谢@mathias-r-jessen!非常有见地(对于 PS 相当新的人)。您包含 begin{} 和 end{} 块的原因是什么?
    • 我在您的代码中提出了一些小建议。最重要的是,$TemplateFile var 在提供有效文件名时没有被填充,因此返回路径不完整。我无法让 ValidateSet 和选项卡完成工作。一旦我提供了-TemplateDir-TemplateFile 就会隐藏,不会自动完成制表符。但是,它运行良好......最后,默认文件名现在没有被测试。我可以在begin {} 块中轻松地对此进行测试,但我认为最合适的方法是通过 ValidateScript,我还可以在其中测试 Dir 路径。这可能吗?
    • edits 被拒绝,偏离了您的初衷(?)。不幸的是,很少有信息可以继续,但希望您能提供反馈。干杯
    猜你喜欢
    • 2010-10-18
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    相关资源
    最近更新 更多