PowerShell 参数 - "术语 'param'不被识别为 cmdlet 的名称" [英] PowerShell parameters - "The term 'param' is not recognized as the name of a cmdlet"

查看:65
本文介绍了PowerShell 参数 - "术语 'param'不被识别为 cmdlet 的名称"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:

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

我对 PowerShell 还很陌生,因此目前我非常困惑.要么是我看不懂,要么是凌晨.

I'm quite new to PowerShell, and I'm severely confused because of this at the moment. Either because I don't understand it, or because it's early in the morning.

推荐答案

如果您的脚本需要参数,Param 关键字必须是脚本中的第一条语句.

If your script requires arguments, the Param keyword must be the first statement in the script.

但是,您可以稍后使用 Read-Host cmdlet 要求用户输入:

However, you can ask the user for input later by using the Read-Host cmdlet:

$processId = Read-Host "Enter the PID of the process to kill"

这篇关于PowerShell 参数 - "术语 'param'不被识别为 cmdlet 的名称"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆