请参阅Powershell的ValidateScript中的其他参数

时间:2016-03-27 22:12:42

标签: validation powershell

我最初过于复杂化了我想简化的问题:

是否有可能,更重要的是,Microsoft正式支持我在ValidateScript部分中引用除验证参数之外的参数?

以下是我想做的事情:

function test
{
    Param(
        [switch]
        $doThingA = $False,

        [ValidateScript({ 
            If( $doThingA -eq $True -and -not ( Test-Path $_ -PathType Leaf ) )
            {
                Write-Output $False
            }

            Write-Output $True
        })]
        [string]
        $doThingA_FilePath
    )
}

这是powershell的文档中有关ValidateScript参数的说明

  

使用ValidateScript属性时,参数值为   被验证的映射到$ _变量。你可以使用$ _   变量以引用脚本中的参数值。

1 个答案:

答案 0 :(得分:5)

你让我好奇,我做了一些测试。您似乎可以访问ValidateScript脚本块中的其他参数值,但前提是它们已被绑定。

即。这一切都取决于传递参数的顺序 - 应该在ValidateScript脚本块中要访问的所有其他参数之后指定带ValidateScript的参数。

例外是:

    按属性名称绑定时,
  • Switch参数 - 在我的测试中,它们始终返回false
  • 如果ValidateScript的参数使用ValueFromPipeline,则它似乎总是最后绑定,因此它可以访问所有其他参数。

P.S。这绝不是竞争性研究,所以请随意尝试自己。

这是我的测试脚本:

function Test-ParamAccess
{
    [CmdletBinding()]
    Param
    (
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [switch]$Alfa,

        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateScript({
            'Output', [ordered]@{
                Alfa = $Alfa
                Bravo = $_
                Charlie = $Charlie
            } | Format-Table -Autosize | Out-String | Write-Warning
            $true
        })]
        [string]$Bravo,

        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string]$Charlie
    )
}

$Cmdline = @(
     "-Alfa -Bravo 'BRAVO' -Charlie 'CHARLIE'",
     "-Alfa -Charlie 'CHARLIE' -Bravo 'BRAVO'",
     "-Bravo 'BRAVO' -Alfa -Charlie 'CHARLIE'",
     "-Bravo 'BRAVO' -Charlie 'CHARLIE' -Alfa",
     "-Charlie 'CHARLIE' -Alfa -Bravo 'BRAVO'",
     "-Charlie 'CHARLIE' -Bravo 'BRAVO' -Alfa"
)

$Hashtable = @(
      'Alfa = $true ; Bravo = "BRAVO" ; Charlie = "CHARLIE" ;'
      'Alfa = $true ; Charlie = "CHARLIE" ; Bravo = "BRAVO" ;'
      'Bravo = "BRAVO" ; Alfa = $true ; Charlie = "CHARLIE" ;'
      'Bravo = "BRAVO" ; Charlie = "CHARLIE" ; Alfa = $true ;'
      'Charlie = "CHARLIE" ; Alfa = $true ; Bravo = "BRAVO" ;'
      'Charlie = "CHARLIE" ; Bravo = "BRAVO" ; Alfa = $true ;'
) | ForEach-Object {
  Invoke-Expression "[ordered]@{$_}"
}

$Pipeline = @(
    "-Alfa -Charlie 'CHARLIE'",
    "-Charlie 'CHARLIE' -Alfa"
)

$Separator =  '=' * 80

$Separator, 'Arguments via commandline:', $Separator | Write-Host
$Cmdline | ForEach-Object {
    Write-Host "Input: $_"
    Invoke-Expression "Test-ParamAccess $_"
}

$Separator, 'Arguments via splatting:', $Separator | Write-Host
$Hashtable | ForEach-Object {
    'Input:', ($_ | Out-String) | Write-Host
    Test-ParamAccess @_
}

$Separator, 'Arguments via pipeline by property name:', $Separator | Write-Host
$Hashtable | ForEach-Object {
    'Input:', ($_ | Out-String) | Write-Host
    New-Object -TypeName PsCustomObject -Property $_ | Test-ParamAccess
}

$Separator, 'Arguments via pipeline by value:', $Separator | Write-Host 
$Pipeline | ForEach-Object {
    'Input:', ($_ | Out-String) | Write-Host
    Invoke-Expression "'BRAVO' | Test-ParamAccess $_"
}

结果:

================================================================================
Arguments via commandline:
================================================================================
Input: -Alfa -Bravo 'BRAVO' -Charlie 'CHARLIE'
WARNING: Output


Name    Value
----    -----
Alfa    True 
Bravo   BRAVO
Charlie      


Input: -Alfa -Charlie 'CHARLIE' -Bravo 'BRAVO'
WARNING: Output


Name    Value  
----    -----  
Alfa    True   
Bravo   BRAVO  
Charlie CHARLIE


Input: -Bravo 'BRAVO' -Alfa -Charlie 'CHARLIE'
WARNING: Output


Name    Value
----    -----
Alfa         
Bravo   BRAVO
Charlie      


Input: -Bravo 'BRAVO' -Charlie 'CHARLIE' -Alfa
WARNING: Output


Name    Value
----    -----
Alfa         
Bravo   BRAVO
Charlie      


Input: -Charlie 'CHARLIE' -Alfa -Bravo 'BRAVO'
WARNING: Output


Name    Value  
----    -----  
Alfa    True   
Bravo   BRAVO  
Charlie CHARLIE


Input: -Charlie 'CHARLIE' -Bravo 'BRAVO' -Alfa
WARNING: Output


Name    Value  
----    -----  
Alfa           
Bravo   BRAVO  
Charlie CHARLIE


================================================================================
Arguments via splatting:
================================================================================
Input:


Name                           Value                                                                                                                                                
----                           -----                                                                                                                                                
Alfa                           True                                                                                                                                                 
Bravo                          BRAVO                                                                                                                                                
Charlie                        CHARLIE                                                                                                                                              


WARNING: Output


Name    Value
----    -----
Alfa    True 
Bravo   BRAVO
Charlie      


Input:


Name                           Value                                                                                                                                                
----                           -----                                                                                                                                                
Alfa                           True                                                                                                                                                 
Charlie                        CHARLIE                                                                                                                                              
Bravo                          BRAVO                                                                                                                                                


WARNING: Output


Name    Value  
----    -----  
Alfa    True   
Bravo   BRAVO  
Charlie CHARLIE


Input:


Name                           Value                                                                                                                                                
----                           -----                                                                                                                                                
Bravo                          BRAVO                                                                                                                                                
Alfa                           True                                                                                                                                                 
Charlie                        CHARLIE                                                                                                                                              


WARNING: Output


Name    Value
----    -----
Alfa         
Bravo   BRAVO
Charlie      


Input:


Name                           Value                                                                                                                                                
----                           -----                                                                                                                                                
Bravo                          BRAVO                                                                                                                                                
Charlie                        CHARLIE                                                                                                                                              
Alfa                           True                                                                                                                                                 


WARNING: Output


Name    Value
----    -----
Alfa         
Bravo   BRAVO
Charlie      


Input:


Name                           Value                                                                                                                                                
----                           -----                                                                                                                                                
Charlie                        CHARLIE                                                                                                                                              
Alfa                           True                                                                                                                                                 
Bravo                          BRAVO                                                                                                                                                


WARNING: Output


Name    Value  
----    -----  
Alfa    True   
Bravo   BRAVO  
Charlie CHARLIE


Input:


Name                           Value                                                                                                                                                
----                           -----                                                                                                                                                
Charlie                        CHARLIE                                                                                                                                              
Bravo                          BRAVO                                                                                                                                                
Alfa                           True                                                                                                                                                 


WARNING: Output


Name    Value  
----    -----  
Alfa           
Bravo   BRAVO  
Charlie CHARLIE


================================================================================
Arguments via pipeline by property name:
================================================================================
Input:


Name                           Value                                                                                                                                                
----                           -----                                                                                                                                                
Alfa                           True                                                                                                                                                 
Bravo                          BRAVO                                                                                                                                                
Charlie                        CHARLIE                                                                                                                                              


WARNING: Output


Name    Value  
----    -----  
Alfa    False  
Bravo   BRAVO  
Charlie CHARLIE


Input:


Name                           Value                                                                                                                                                
----                           -----                                                                                                                                                
Alfa                           True                                                                                                                                                 
Charlie                        CHARLIE                                                                                                                                              
Bravo                          BRAVO                                                                                                                                                


WARNING: Output


Name    Value  
----    -----  
Alfa    False  
Bravo   BRAVO  
Charlie CHARLIE


Input:


Name                           Value                                                                                                                                                
----                           -----                                                                                                                                                
Bravo                          BRAVO                                                                                                                                                
Alfa                           True                                                                                                                                                 
Charlie                        CHARLIE                                                                                                                                              


WARNING: Output


Name    Value  
----    -----  
Alfa    False  
Bravo   BRAVO  
Charlie CHARLIE


Input:


Name                           Value                                                                                                                                                
----                           -----                                                                                                                                                
Bravo                          BRAVO                                                                                                                                                
Charlie                        CHARLIE                                                                                                                                              
Alfa                           True                                                                                                                                                 


WARNING: Output


Name    Value  
----    -----  
Alfa    False  
Bravo   BRAVO  
Charlie CHARLIE


Input:


Name                           Value                                                                                                                                                
----                           -----                                                                                                                                                
Charlie                        CHARLIE                                                                                                                                              
Alfa                           True                                                                                                                                                 
Bravo                          BRAVO                                                                                                                                                


WARNING: Output


Name    Value  
----    -----  
Alfa    False  
Bravo   BRAVO  
Charlie CHARLIE


Input:


Name                           Value                                                                                                                                                
----                           -----                                                                                                                                                
Charlie                        CHARLIE                                                                                                                                              
Bravo                          BRAVO                                                                                                                                                
Alfa                           True                                                                                                                                                 


WARNING: Output


Name    Value  
----    -----  
Alfa    False  
Bravo   BRAVO  
Charlie CHARLIE


================================================================================
Arguments via pipeline by value:
================================================================================
Input:
-Alfa -Charlie 'CHARLIE'


WARNING: Output


Name    Value  
----    -----  
Alfa    True   
Bravo   BRAVO  
Charlie CHARLIE


Input:
-Charlie 'CHARLIE' -Alfa


WARNING: Output


Name    Value  
----    -----  
Alfa    True   
Bravo   BRAVO  
Charlie CHARLIE