Function Args

Discussion in 'Scripting' started by ebgreen, Jan 18, 2006.

  1. ebgreen

    ebgreen Guest

    I was trying to make a quick and dirty process killer and I ran across
    something I don't understand. Here is a function that will do what I expect
    it to:
    function showit
    When you call the function with a parameter, the paramter is displayed:
    14:55:09 C:\> showit foo

    This is a function that works
    function showNotepad
    get-process | where{$_.ProcessName -eq "notepad"}
    It will display the data on the notepad process if you have notepad open:
    14:59:28 C:\> showNotepad

    Handles NPM(K) PM(K) WS(K) VS(M) CPU(s) Id ProcessName
    ------- ------ ----- ----- ----- ------ --
    47 2 1012 3404 30 0.09 5068

    This function does not work:
    function doNotShow
    get-process | where{$_.ProcessName -eq $Args[0]}
    If you run it looking for notepad you get nothing back:
    15:03:56 C:\> doNotShow notepad
    15:04:01 C:\>

    And then lastly, this does work:
    function showProcess
    get-process | where{$_.ProcessName -eq $proc}
    You get the info on running processes named whatever you pass to it:
    15:09:05 C:\> showProcess notepad

    Handles NPM(K) PM(K) WS(K) VS(M) CPU(s) Id ProcessName
    ------- ------ ----- ----- ----- ------ --
    47 2 1036 3428 30 0.19 4320
    47 2 1012 3408 30 0.09 5068

    Now I suspect this behavior has to do with the explicit cast that the param
    does, but I was looking for some confirmation just to better help me
    ebgreen, Jan 18, 2006
    1. Advertisements

  2. This works:

    MSH> function dontShow { $n = $args[0]; "name is $n"; get-process | where {
    $_.ProcessName -ne $n}}

    I wonder if you have found a bug. I've noticed that processing things like
    $args[0] and the value of that is say "foo" can sometimes results in
    $args[0] evaluating to "foo[0]".
    Keith Hill [MVP], Jan 18, 2006
    1. Advertisements

  3. The $args[0] in function foo {gps | where {$ -eq $args[0]}} is args to
    the script block {$ -eq $args[0]}. In this case, $args[0] will be
    $null. Since there isn't a process on the machine whose name is null, you
    don't get any return.

    You could try function foo {$firstArg = $args[0]; gps | where{$ -eq
    Kevin Loo [MSFT], Jan 18, 2006
  4. Ahh! The light goes on. Is that new for beta 3?
    Keith Hill [MVP], Jan 18, 2006
  5. I don't think so. That $args[0] refers to the script block in function foo
    defined in my previous posting has been the behavior prior to Beta 3.

    Kevin Loo [MSFT]
    Microsoft Command Shell Development
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.

    Kevin Loo [MSFT], Jan 18, 2006
  6. ebgreen

    ebgreen Guest

    When put that way it does make sense. Thank you for clearing it up.
    ebgreen, Jan 19, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.