2016-09-26 74 views
0

需要從UNC路徑運行PowerShell腳本。即\\ myserver \文件夾\ a.ps1。該腳本需要一個參數。腳本的前幾行是如下使用參數運行位於UNC路徑上的Powershell腳本文件

param(
[Parameter(Mandatory)] 
[ValidateSet('DEV','TEST','STAGING')]  
[String]$Server 
) 

但是當我鍵入\\ MYSERVER \ folderA \ a.ps1 -S在我的計算機上的PowerShell窗口,按下tab鍵自動完成將無法正常工作,也沒有驗證集(服務器參數不會綁定到參數路徑)。如果我在本地複製腳本並嘗試這個自動完成和validateset的作品。我應該怎麼做才能夠通過UNC路徑調用腳本並仍然使驗證集正常工作?謝謝。

回答

0

我的測試腳本:

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並簽署您的腳本。

+0

謝謝Farhad。我想要做的是驗證設置爲當我點擊標籤時向用戶顯示值。如果您有本地腳本並鍵入PS Z:\> Desktop \ shares \ test.ps1 - 服務器並點擊選項卡,驗證設置值將顯示給用戶。 – yonaire