PowerShell 参数 - “术语‘param’未被识别为 cmdlet 的名称”

2024-05-22

考虑:

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#

当我尝试运行它时,我遇到了:

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 还很陌生,目前我对此感到非常困惑。要么是因为不懂,要么是因为时间还早。


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

但是,您可以稍后使用Read-Host cmdlet:

$processId = Read-Host "Enter the PID of the process to kill"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PowerShell 参数 - “术语‘param’未被识别为 cmdlet 的名称” 的相关文章

随机推荐