English 中文(简体)
为什么要 壳体重新评价参数在价值变化时的瓦解
原标题:Why Does PowerShell Re-evaluate the Validation of a Parameter When Its Value Changes
  • 时间:2024-03-06 02:52:25
  •  标签:
  • powershell

我目前正在撰写一份简单的PowerShell(5)书。 它应当吸收用户的意见,然后根据投入执行一些任务。 我将Validate写到投入参数,以验证用户的投入。 根据PowerShell文件,验证是在编码操作之前进行的,因此,我改变了用户投入的价值。 然而,这造成了错误。

考虑下面的简单例子。


  [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [ValidateScript({ $_ -gt 0 })]
        [int]$InputParameter
    )

    process {
       
        $InputParameter = -1

      
    }
} 

Running TEST-FUNCTION 0 Yields an error

TEST-FUNCTION : Cannot validate argument on parameter  InputParameter . The " $_ -gt 0 " validation script for the
argument with value "0" did not return a result of True. Determine why the validation script failed, and then try the
command again.
At line:1 char:15
+ TEST-FUNCTION 0
+               ~
    + CategoryInfo          : InvalidData: (:) [TEST-FUNCTION], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,TEST-FUNCTION

这具有意义,因为它没有通过最初的确认。

However, running TEST-FUNCTION 1 also yields an error.

The variable cannot be validated because the value -1 is not a valid value for the InputParameter variable.
At line:12 char:9
+         $InputParameter = -1
+         ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ValidationMetadataException
    + FullyQualifiedErrorId : ValidateSetFailure

The two function calls give two different errors. I’m not sure why I can’t change the value of the parameter after it has passed validation. Is there any workaround that can prevent the second round of evaluation? Thanks!

问题回答

由于属性已附在https://learn.microsoft.com/en-us/dotnet/api/system. management.automation.psvariable?view=powershellsdk-7.4.0” rel=“nofollow noreferer”>PSVariable 元数据,简单的例子不需要:

[ValidateScript({ $_ -gt 0 })]
[int] $InputParameter = 1

(Get-Variable InputParameter).Attributes |
    Where-Object TypeId -EQ ([System.Management.Automation.ValidateScriptAttribute])

# ErrorMessage ScriptBlock TypeId
# ------------ ----------- ------
#               $_ -gt 0   System.Management.Automation.ValidateScriptAttribute

自<代码>。 《<>ttributes/code>是一种收集,但你可以将其删除,但事实上,在你的职责中,更便于使用不同的名称:

$attributeToRemove = (Get-Variable InputParameter).Attributes |
    Where-Object TypeId -EQ ([System.Management.Automation.ValidateScriptAttribute])

$InputParameter = -1
# MetadataError: The variable cannot be validated because the value -1 is not a valid....

$null = (Get-Variable InputParameter).Attributes.Remove($attributeToRemove)

$InputParameter = -1 # OK




相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签