2016-11-02 31 views
1

我试图在PowerShell 5 我的脚本中使用参数是如下:PowerShell的PARAM是行不通的,它的驾驶我坚果

param(
[parameter(mandatory=$true)] 
[string]$IP, 

[parameter(mandatory=$true)] 
[string]$Time 
) 
Write-Host $IP 
Write-Host $Time 

假设脚本名为test.ps1, 我尝试以两种方式运行它:

.\test.ps1 -IP 10.10.10.10 -Time 10:10 
.\test.ps1 -IP "10.10.10.10" -Time "10:10" 

没有这些工作。 相反,它会提示我输入IP值。 错误是如下:

cmdlet test.ps1 at command pipeline position 1 
Supply values for the following parameters: 
IP: 

显然这个脚本是在一段代码集成到一个更大的脚本系统只是一个阶段。 帮助将不胜感激,经过几个小时的搜索,我没有看到我做错了什么。

+2

适合我,适合复制和粘贴你的脚本:http://i.imgur.com/1NKn9AL.png你是从命令提示符还是远程机器上运行它?什么是更大的系统? – TessellatingHeckler

+0

可以确认它也适用于我。你可能会张贴一些关于你如何调用代码的截图,以及你看到的是什么? –

回答

2

我可以证实这在我的电脑上运行。我只好目录切换到(例如:。其中的.ps1脚本位于CD(目录下的脚本所在地)目录我跑这从Windows命令窗口

powershell.exe .\test.ps1 -IP "10.10.10.10" -Time "10:10" 

这应该工作以及。

powershell -File C:\somedirectory\test.ps1 -IP "10.10.10.10" -Time "10:10" 

可以通过.bat文件揭开这一关为好。下面.bat文件的test.bat的

In command prompt C:\>test.bat "10.10.10.10" "10:10" 

等内容。

@ECHO OFF 
PowerShell -File C:\somedirectory\test.ps1 -IP %1 -Time %2