Skip to content

Get-Process $pid should suggest using -Id $pid #2722

Closed

Description

@lzybkr

The default parameter set for Get-Process accepts -Name by position.

Maybe it's just me, but I forget this all the time, and expect Get-Process 1234 to work.

I think we can make it work in a reasonable way too. If Name is a positive integer and we do not find a process by that name, search again assuming it was a ProcessId.

Edit: instead of making this work (which might not be a good idea always) - I like the suggestion from Keith Hill (@rkeithhill) to give a better error message.

Activity

  1. added
    WG-Cmdlets
    Issue-Enhancement
    Up-for-Grabs
    on Nov 19, 2016
  2. iSazonov commented on Nov 19, 2016

    @iSazonov
    Collaborator

    It's unbelievable but user can actually catching process with name 1234 and actually get a process with Pid 1234. It's bad.

  3. lzybkr commented on Nov 19, 2016

    @lzybkr
    ContributorAuthor

    Right, if finding the process by name fails, only then look by id. Is that not safe and reasonable?

  4. powercode commented on Nov 19, 2016

    @powercode
    Collaborator

    I think he means that there is a risk that I have been really clever and created
    1234.exe.

    It is not running, but when I do

    Get-Process -Name 1234 | Stop-Process
    

    I kill MySuperimportantProcess.exe, with pid 1234.

  5. powercode commented on Nov 19, 2016

    @powercode
    Collaborator

    Maybe there should be a defaulted parameterset NameOrID with this behavior, so that you can specify -Name or -ID and only get by name or by id.

  6. rkeithhill commented on Nov 20, 2016

    @rkeithhill
    Collaborator

    Maybe there should be a defaulted parameterset NameOrID

    That won't help if the arg is specified by position but at least it A) gives a way to specify that you really want to match just by name and B) gives a more correct name for the parameter.

    Conceivably this could be a breaking change if a script does not use the -Name parameter and there just happens to be a process with a name that corresponds to a PID you want Stop (or otherwise interrogate). That said, it is pretty uncommon to run across processes with a name that parses as a integer. And the workaround is simple - use the -Name parameter. I'd be willing to risk the back compat issue for the convenience provided.

    BTW why not give Stop-Process the same treatment? Make a new NameOrId parameter and make it the default parameterset. It annoys me that GPS defaults to name but SPPS defaults to PID. I'm always forgetting to specify the -Name parameter to Stop-Process when I want to stop all instances of an app.

  7. added
    Issue-Discussion
    on Nov 20, 2016
  8. SteveL-MSFT commented on Nov 20, 2016

    @SteveL-MSFT
    Member

    The risk is similar to the discussion the committee had with RFC0011 where files named as integers can produce unexpected results. Perhaps a safe middle ground might be to just tell the user that a process with name 1234 wasn't found and that they should try get-process -pid 1234

  9. iSazonov commented on Nov 20, 2016

    @iSazonov
    Collaborator

    Based on the above. There is a possibility to make destructive actions. This is absolutely unacceptable in a script.
    And looks very tasty for user input but really requires of informing the user.
    I think it makes no sense to do this for a single cmdlet. Let's try to generalize the situation. This is an area of parameter binding. We now offer an alternative parameters before the input of a value, and here we need to offer (informing the user) alternatives after the input of a value (insert a parameter name before the value by tab). Is it possible to do this?

  10. lzybkr commented on Nov 21, 2016

    @lzybkr
    ContributorAuthor

    This dovetails nicely into the parameter alias discussion in #2199.

    If a user can extend the parameter metadata for other commands, they can add aliases or even an ArgumentTransformationAttribute, which would solve this problem nicely without explicitly opting into the potentially destructive and possibly surprising behavior that I'd like.

  11. rkeithhill commented on Nov 21, 2016

    @rkeithhill
    Collaborator

    Regardless of the outcome here, I'd love to see more PowerShell cmdlets do this:

    ... tell the user that a process with name 1234 wasn't found and that they should try get-process -pid 1234

    I like it when I get that sort of helpful message on Ubuntu e.g. when I run a command that isn't available, Ubuntu tells how to install that command with apt-get.

    Also my objection is not about allowing users to add aliases. I just wanted to make sure PowerShell wouldn't add the -rf, -al aliases by default.

  12. powercode commented on Nov 21, 2016

    @powercode
    Collaborator

    How about something like this?

    PS> get-process 19432
    
    Use pid instead of Name?
    The process with name '19432' was not found but 'devenv (pid=19432)' was. Continue?
    [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y
    
    Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
    -------  ------    -----      -----     ------     --  -- -----------
       4899     643  1841044    1038068   3 789,73  19432   1 devenv
    
  13. iSazonov commented on Nov 22, 2016

    @iSazonov
    Collaborator

    Looks good, if it only is in interactive mode but not in script.

  14. 2 remaining items

  15. lzybkr commented on Dec 9, 2016

    @lzybkr
    ContributorAuthor

    I'm convinced that the default behavior should not be changed, but I'm leaving this open until we have a better error message than Cannot find a process with the name "1234".

  16. changed the title [-]Get-Process $pid should work[/-] [+]Get-Process $pid should suggest using `-Id $pid`[/+] on Dec 9, 2016
  17. changed the title [-]Get-Process $pid should suggest using `-Id $pid`[/-] [+]Get-Process $pid should suggest using -Id $pid[/+] on Dec 9, 2016
  18. rkeithhill commented on Dec 9, 2016

    @rkeithhill
    Collaborator

    Agreed. At the very least, the error message could say Cannont find a process with the name "1234". If 1234 is a process Id, use Get-Process -Id 1234. Of course, you would only use that error message when the Name parameter value is all digits.

  19. lzybkr commented on Dec 9, 2016

    @lzybkr
    ContributorAuthor

    Note that #2199 might as well be the issue to cover customizing parameter metadata.

  20. added
    Hacktoberfest
    on Oct 1, 2017
  21. MohiTheFish commented on Feb 23, 2019

    @MohiTheFish
    Contributor

    I am working on this with TylerLeonhardt and Rob Holt (@rjmholt) at HackIllinois 2019. Here is the PR: #8959

  22. KirkMunro commented on Feb 28, 2019

    @KirkMunro
    Contributor

    If Get-Process and Stop-Process were both changed to have their parameter sets designed like Debug-Process and Wait-Process, where the Name and Id parameters are positional, wouldn't that resolve this problem entirely while making -Process cmdlets consistent in design without introducing breaking changes?

    i.e. Today these work:

    Get-Process pwsh
    Stop-Process $pid
    

    and these don't:

    Get-Process $pid
    Stop-Process pwsh
    

    If both Get-Process and Stop-Process had positional Id and Name parameters, respectively, wouldn't that make both scenarios work without breaking existing scripts that use positional parameters? I think it would at least resolve the Stop-Process issue without issue, since strings would be automatically mapped to the Name parameter set. I haven't thought all of the paths for this through, and I have my head half paying attention to a debugger right now, but it seems like a possible solution for this.

  23. iSazonov commented on Feb 28, 2019

    @iSazonov
    Collaborator

    Kirk Munro (@KirkMunro) As discussed above user can fool himself typing without quotes Stop-Process 1234 and kill process with pid 1234 instead of process with name 1234.

  24. KirkMunro commented on Feb 28, 2019

    @KirkMunro
    Contributor

    Too bad, makes for annoying parameter sets.

  25. SteveL-MSFT commented on Mar 6, 2019

    @SteveL-MSFT
    Member

    We could add something to the suggestion framework when a process named 1234 doesn't exist and suggest using the pid if the value is numerical.

  26. iSazonov commented on Mar 6, 2019

    @iSazonov
    Collaborator

    I wonder - what can this system return better than the current message?

  27. SteveL-MSFT commented on Mar 6, 2019

    @SteveL-MSFT
    Member

    Ilya (@iSazonov) Current error looks like:

    get-process : Cannot find a process with the name "1234". Verify the process name and call the cmdlet again.
    At line:1 char:1
    + get-process 1234
    + ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (1234:String) [Get-Process], ProcessCommandException
    + FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
    

    It could look like:

    get-process : Cannot find a process with the name "1234". Verify the process name and call the cmdlet again.
    At line:1 char:1
    + get-process 1234
    + ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (1234:String) [Get-Process], ProcessCommandException
    + FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
    
    Suggestion: Did you mean: Get-Process -id 1234 ?
    
  28. iSazonov commented on Mar 7, 2019

    @iSazonov
    Collaborator

    For clarify, now the error meassage is "Cannot find a process with the name "{0}". Try running with -Id to search by Id of processes"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    HacktoberfestIssue-DiscussionIssue-EnhancementUp-for-GrabsWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions