Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Passing arguments to a function

Reply
 
Old 12-11-2006   #1 (permalink)
Marco Shaw


 
 

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


 
 

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
Reply

Thread Tools


Similar Threads
Thread Forum
Passing arguments to external command through PS PowerShell
Need help - passing string arguments with quotes PowerShell
Passing arguments from .BAT to PowerShell script PowerShell
Passing arguments into scripts? PowerShell
passing arguments PowerShell


Vista Forums 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 Ltd

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