![](/img/trans.png)
[英]Is there a reason why .split in PowerShell doesn't work for me?
[英]Powershell param just doesn't work and it's driving me nuts
我试图在Powershell 5中使用参数。我的脚本如下:
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:
显然,这个小脚本只是将一段代码集成到更大的脚本系统中的一个阶段。 感谢您的帮助,经过数小时的搜索,我看不到自己做错了什么。
我可以确认它可以在我的计算机上运行。 我必须将目录切换到(例如:cd(脚本所在的目录).ps1脚本所在的目录。我是从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文件开始。 如test.bat
In command prompt C:\>test.bat "10.10.10.10" "10:10"
.bat文件的内容在下面。
@ECHO OFF
PowerShell -File C:\somedirectory\test.ps1 -IP %1 -Time %2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.