[英]PowerShell parameters - “The term 'param' is not recognized as the name of a cmdlet”
Consider: 考虑:
notepad # Starts Notepad
Get-Process notepad # Finds the processes named notepad
param(
[Parameter(Mandatory=$true)][string]$ProcessID
) #This should request user input, and store it in a variable#
Stop-Process $ProcessID # Stops the input process ID#
When I try to run this, I'm met with: 当我尝试运行此程序时,遇到了:
param : The term 'param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:3 char:1
+ param(
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (param:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Stop-Process : Cannot bind parameter 'InputObject'. Cannot convert the "A" value of type "System.String" to type "System.Diagnostics.Process".
At line:6 char:14
+ Stop-Process $ProcessID
+ ~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Stop-Process], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.StopProcessCommand
I'm quite new to PowerShell, and I'm severely confused because of this at the moment. 我对PowerShell还是很陌生,由于这个原因,我感到非常困惑。 Either because I don't understand it, or because it's early in the morning.
是因为我听不懂,还是因为是凌晨。
If your script requires arguments, the Param
keyword must be the first statement in the script. 如果您的脚本需要参数,则
Param
关键字必须是脚本中的第一条语句。
However, you can ask the user for input later by using the Read-Host
cmdlet: 但是,您可以稍后使用
Read-Host
cmdlet要求用户输入:
$processId = Read-Host "Enter the PID of the process to kill"
The final result after removing the parameters and using the Read-Host cmdlet: 删除参数并使用Read-Host cmdlet后的最终结果:
notepad
Get-Process notepad
$ProcessID = Read-Host "Enter the PID of the process to kill"
Stop-Process $ProcessID
It works flawlessly. 它完美地工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.