2016-09-26 22 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