Re: Function Args

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



The $args[0] in function foo {gps | where {$_.name -eq $args[0]}} is args to
the script block {$_.name -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{$_.name -eq
$firstArg}}.

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

"ebgreen" <ebgreen@xxxxxxxxxxxxxxxxxxxxxxxxx> ¼¶¼g©ó¶l¥ó·s»D:86B0E7F2-F261-48F0-9979-7E48250FCCA3@xxxxxxxxxxxxxxxx
>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
> {
> $Args[0]
> }
> When you call the function with a parameter, the paramter is displayed:
> 14:55:09 C:\> showit foo
> 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
> notepad
>
> 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
> {
> param([string]$proc)
> 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
> notepad
> 47 2 1012 3408 30 0.09 5068
> notepad
>
>
> 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
> understand.


.



Relevant Pages

  • Re: Display Properties Problem
    ... it and where in the registry this happens so we can learn more neat stuff. ... If you're just trying to get your Display Properties tabs back, ... I opened it with Notepad. ...
    (microsoft.public.windowsxp.basics)
  • Re: DrvResetPDEV ??
    ... > I'm printing from notepad. ... >>> driver itself. ... >> there are property pages to display. ...
    (microsoft.public.development.device.drivers)
  • Re: irregular character spacing
    ... Microsoft Word will add periodical "random" spacings between letters? ... Notepad does not support these ... that's an artefact of the display resolution. ...
    (microsoft.public.mac.office.word)
  • Re: write into a file
    ... > Notepad is displaying as it should on the Windows platform. ... not display correctly will ... http://www.PhySci.org/ Open-source software suite ...
    (comp.lang.java.help)
  • Re: Notepad Popup?
    ... I.e. why notepad instead of a browser etc. ... you could display the plain text this way. ... I got tired of all the spam. ... >>> Is there anyway to make a text file link that will put the text in ...
    (microsoft.public.frontpage.client)