电源外壳参数 - "The term 'param' is not recognized as the name of a 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#

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

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"

删除参数并使用读取主机 cmdlet 后的最终结果:

notepad
Get-Process notepad
$ProcessID = Read-Host "Enter the PID of the process to kill"
Stop-Process $ProcessID

它完美无缺。

相关内容

  • 没有找到相关文章

最新更新