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

-match within where-object against $args[0]

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 12-02-2006   #1 (permalink)
Joe D
Guest


 

-match within where-object against $args[0]

I can do the following in powershell and it works fine:
get-process | where-object {$_.Name -match "^svc*"}

However, if I create a function that takes arguments, I cannot get it to
work. I've tried all of the following and none of them work properly:

function findproc { get-process | where-object {$_.Name -match
"^$args[0]*"} }
findproc svc

function findproc { get-process | where-object {$_.Name -match
"^$args*"} }
findproc svc

function findproc { get-process | where-object {$_.Name -match
"^$args[0]"} }
findproc svc

function findproc { get-process | where-object {$_.Name -match "^$args"} }
findproc svc

function findproc { get-process | where-object {$_.Name -match $args[0]
} }
findproc ^svc*

Any suggestions? Am I doing something wrong?

My System SpecsSystem Spec
Old 12-02-2006   #2 (permalink)
dreeschkind
Guest


 

RE: -match within where-object against $args[0]

$args is not visible inside the where-object filterscript (every scriptblock
has its own $args). Try something like this:

function findproc {
$name = $args[0];
get-process | where-object { $_.Name -match $name }
}

--
greetings
dreeschkind

"Joe D" wrote:

> I can do the following in powershell and it works fine:
> get-process | where-object {$_.Name -match "^svc*"}
>
> However, if I create a function that takes arguments, I cannot get it to
> work. I've tried all of the following and none of them work properly:
>
> function findproc { get-process | where-object {$_.Name -match
> "^$args[0]*"} }
> findproc svc
>
> function findproc { get-process | where-object {$_.Name -match
> "^$args*"} }
> findproc svc
>
> function findproc { get-process | where-object {$_.Name -match
> "^$args[0]"} }
> findproc svc
>
> function findproc { get-process | where-object {$_.Name -match "^$args"} }
> findproc svc
>
> function findproc { get-process | where-object {$_.Name -match $args[0]
> } }
> findproc ^svc*
>
> Any suggestions? Am I doing something wrong?

My System SpecsSystem Spec
Old 12-04-2006   #3 (permalink)
Andrew Watt [MVP]
Guest


 

Re: -match within where-object against $args[0]

On Sat, 2 Dec 2006 11:03:00 -0800, Joe D
<JoeD@discussions.microsoft.com> wrote:

>I can do the following in powershell and it works fine:
> get-process | where-object {$_.Name -match "^svc*"}


You can do it more simply:

PS C:\PowerShellScripts> function findproc {get-process $args[0]}
PS C:\PowerShellScripts> findproc svc*

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
225 6 3164 5384 60 376.44 1284 svchost
677 14 2436 7656 37 231.06 1332 svchost
2459 597 22940 44112 163 919.06 1528 svchost
108 6 1548 3560 31 6.19 1656 svchost
255 7 2464 5040 38 6.38 1828 svchost

Or, even more simply:

get-process svc*

There are subtle differences between wildcards and regular expressions
that can catch you out.

In this case the wildcard "svc*" is equivalent to the regular
expression "^svc.*" and gives you what I assume you want.

Andrew Watt MVP
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this a bug with @args RickB PowerShell 4 04-09-2008 07:58 AM
$args IT Staff PowerShell 7 01-19-2008 12:18 AM
$args.count IT Staff PowerShell 0 12-27-2007 08:53 PM
Bug? Shouldn't Stop-Process automatically match Id if object is a process? Alex K. Angelopoulos [MVP] PowerShell 3 06-21-2006 05:35 AM
Adding canonical aliases for Compare-Object, Measure-Object, New-Object Alex K. Angelopoulos [MVP] PowerShell 2 05-26-2006 07:58 AM


Update your Vista Drivers Update Your Drivers Now!!

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