简体   繁体   English

PowerShell参数-“术语'param'不被识别为cmdlet的名称”

[英]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.

相关问题 dlurl:术语“dlurl”未被识别为 cmdlet、function、脚本文件或可运行程序的名称 - dlurl : The term 'dlurl' is not recognized as the name of a cmdlet, function, script file, or operable program 将PowerShell变量作为cmdlet参数传递 - Passing a PowerShell variable as a cmdlet parameter PowerShell-在另一个cmdlet中使用变量 - PowerShell - Use variable in another cmdlet 如何在Powershell中的字符串中插入cmdlet - How to insert a cmdlet into a string in powershell Fat Free Framework使用$ args [&#39;name&#39;]或PARAM.name作为路由参数? - Fat Free Framework use $args['name'] or PARAM.name for route parameters? 如何在 Powershell 中的 Copy-Item cmdlet 的文件路径中包含变量 - How to Include a Variable in the File Path of a Copy-Item cmdlet in Powershell 从一个名字 <Param> 变成变量 - Putting a Name from a <Param> into a Variable 如果参数超出参数,则声明参数不起作用 - Declaring parameters does not work if anything is above param 如何在Powershell Active Directory cmdlet中引用对象的属性? - How do I reference an object's property in a Powershell Active Directory cmdlet? Powershell Remove-Variable cmdlet,我需要在每个函数/脚本块的末尾调用它吗? - Powershell Remove-Variable cmdlet, do I need to call it at the end of each function/scriptblock?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM