我的測試腳本:
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