PowerShell 参数 - "The term 'param' is not recognized as the name of a cmdlet"
PowerShell parameters - "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"
删除参数并使用 Read-Host cmdlet 后的最终结果:
notepad
Get-Process notepad
$ProcessID = Read-Host "Enter the PID of the process to kill"
Stop-Process $ProcessID
它完美运行。
考虑:
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"
删除参数并使用 Read-Host cmdlet 后的最终结果:
notepad
Get-Process notepad
$ProcessID = Read-Host "Enter the PID of the process to kill"
Stop-Process $ProcessID
它完美运行。