我的测试脚本:
param(
[Parameter(Mandatory)]
[ValidateSet('DEV','TEST','STAGING')]
[String]$Server
)
echo $server
我的结果:
PS Z:\> \\net-02\shares\test.ps1
cmdlet test.ps1 at command pipeline position 1
Supply values for the following parameters:
Server: server
\\net-02\shares\test.ps1 : Cannot validate argument on parameter 'Server'. The argument "server" does not belong to
the set "DEV,TEST,STAGING" specified by the ValidateSet attribute. Supply an argument that is in the set and then try
the command again.
At line:1 char:1
+ \\net-02\shares\test.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [test.ps1], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,test.ps1
正如你所看到的验证工作。
PS Z:\> \\net-02\shares\test.ps1
cmdlet test.ps1 at command pipeline position 1
Supply values for the following parameters:
Server: dev
dev
我怀疑你的执行策略有问题,将它设置为无限制并再次测试。
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
确保您将其更改回remotesigned并签署您的脚本。
谢谢Farhad。我想要做的是验证设置为当我点击标签时向用户显示值。如果您有本地脚本并键入PS Z:\> Desktop \ shares \ test.ps1 - 服务器并点击选项卡,验证设置值将显示给用户。 – yonaire