Reputation: 33
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. Either because I don't understand it, or because it's early in the morning.
Upvotes: 3
Views: 8997
Reputation: 33
The final result after removing the parameters and using the Read-Host cmdlet:
notepad
Get-Process notepad
$ProcessID = Read-Host "Enter the PID of the process to kill"
Stop-Process $ProcessID
It works flawlessly.
Upvotes: 0
Reputation: 59923
If your script requires arguments, the Param
keyword must be the first statement in the script.
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"
Upvotes: 3