Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Passing arguments to a function

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 12-11-2006   #1 (permalink)
Marco Shaw
Guest


 

Passing arguments to a function

Is this happening just because it is a Monday?

$args is to provide arguments to a script, but isn't it also to provide
arguments to a function?

What am I missing here?

PS C:\> ps|where{$_.processname -eq "outlook"}

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
1276 20 46804 8704 487 59.97 3048 OUTLOOK


PS C:\> function getproc {
>> $output=ps|where{$_.processname -eq "outlook"}
>> $output
>> }
>>

PS C:\> . getproc outlook

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
1276 20 46804 8748 487 60.01 3048 OUTLOOK


PS C:\> function getproc {
>> $output=ps|where{$_.processname -eq "$args"}
>> $output
>> }
>>

PS C:\> . getproc outlook <--returns *nothing*
PS C:\> function getproc {
>> $output=ps|where{$_.processname -eq "$args[0]"}
>> $output
>> }
>>

PS C:\> . getproc outlook <--returns *nothing*
PS C:\>



My System SpecsSystem Spec
Old 12-11-2006   #2 (permalink)
Maximilian Hänel
Guest


 

Re: Passing arguments to a function

Hi Marco,

> Is this happening just because it is a Monday?
>
> $args is to provide arguments to a script, but isn't it also to provide
> arguments to a function?
>
> What am I missing here?


Where-Object binds its own args$ variable. In order to get your function
running you have to save the original $args variable first and use that
instead:

function getproc {
$a=$args
$output=ps|where{$_.processname -eq "$a"}
$output
}


hth

Max
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing arguments from .BAT to PowerShell script wofat68 PowerShell 8 08-02-2008 08:40 AM
Passing function params by ref? Duncan Smith PowerShell 1 01-04-2008 06:15 AM
Passing arguments into scripts? Dr Herbie PowerShell 4 12-30-2006 06:13 AM
passing arguments VB PowerShell 1 12-19-2006 08:23 AM
Executing PS1 script from CMD/SchTasks and passing arguments Terry E Dow PowerShell 1 11-27-2006 05:45 PM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51