[英]Powershell param just doesn't work and it's driving me nuts
I'm trying to use param within Powershell 5 My script is as following: 我试图在Powershell 5中使用参数。我的脚本如下:
param(
[parameter(mandatory=$true)]
[string]$IP,
[parameter(mandatory=$true)]
[string]$Time
)
Write-Host $IP
Write-Host $Time
Suppose the script is named test.ps1, I tried running it in 2 ways: 假设脚本名为test.ps1,我尝试通过两种方式运行它:
.\test.ps1 -IP 10.10.10.10 -Time 10:10
.\test.ps1 -IP "10.10.10.10" -Time "10:10"
None of these work. 这些都不起作用。 Instead, it prompts me to enter a value to IP.
相反,它提示我输入IP值。 The error is as following:
错误如下:
cmdlet test.ps1 at command pipeline position 1
Supply values for the following parameters:
IP:
Obviously this little script is just a phase in integrating a piece of code into a bigger script system. 显然,这个小脚本只是将一段代码集成到更大的脚本系统中的一个阶段。 Help would be appreciated, after hours of searching I don't see what I've done wrong.
感谢您的帮助,经过数小时的搜索,我看不到自己做错了什么。
I can confirm this runs on my computer. 我可以确认它可以在我的计算机上运行。 I had to switch directories to the (ex: cd (directory where script is located). directory where the .ps1 script is located. I ran this from the Windows command window.
我必须将目录切换到(例如:cd(脚本所在的目录).ps1脚本所在的目录。我是从Windows命令窗口运行此目录的。
powershell.exe .\test.ps1 -IP "10.10.10.10" -Time "10:10"
This should work as well. 这也应该起作用。
powershell -File C:\somedirectory\test.ps1 -IP "10.10.10.10" -Time "10:10"
Can kick this off via a .bat file as well. 也可以通过.bat文件开始。 Such as test.bat
如test.bat
In command prompt C:\>test.bat "10.10.10.10" "10:10"
Contents of .bat file below. .bat文件的内容在下面。
@ECHO OFF
PowerShell -File C:\somedirectory\test.ps1 -IP %1 -Time %2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.