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

RB

Vista - Newbie: filter rows with variable

Reply
 
05-14-2007   #1
gurbao


 
 

Newbie: filter rows with variable

Hi,

I want to call a .ps1 script with a parameter which is used as a
parameter to limit the rows read from a file.
I have tried the following:

ps> list.ps1 <searchcriteria>

list.ps1:
gc c:\temp\test.txt | where {$_ -like "*$args*"} | sort

but I cant get it to work. It does not fail but no rows are returned.

What is wrong here?

Also, when the parameter includes @ (e.g. @work), I get I an error:
Unrecognized token in source text.
At line:1 char:12
+ .\list.ps1 @ <<<< work

How can I avoid this without escaping the @ when entering the
parameter?


thanks,


My System SpecsSystem Spec
05-14-2007   #2
Roman Kuzmin


 
 

Re: Newbie: filter rows with variable

1) I guess in this context $args is arguments of a script block {$_ -like
"*$args*"} passed into Where-Object cmdlet (but, strange, all lines should
be returned in your example, IMO...). Try this way:

function foo
{
$myArgs = $args
gc <.> | where {$_ -like "*$myArgs*"} | sort
}

foo '<pattern>'

2) Is this OK?:

.\list.ps1 '@work'
or

.\list.ps1 "@work"

--
Thanks,
Roman Kuzmin


My System SpecsSystem Spec
05-14-2007   #3
Brandon Shell


 
 

Re: Newbie: filter rows with variable

This is known issue... I cannot recall exactly what the exact issue is, but
I know I have it baked in my brain never to use $args directly.

"Roman Kuzmin" <z@z.z> wrote in message
news:edDzeZhlHHA.4032@TK2MSFTNGP02.phx.gbl...
> 1) I guess in this context $args is arguments of a script block {$_ -like
> "*$args*"} passed into Where-Object cmdlet (but, strange, all lines should
> be returned in your example, IMO...). Try this way:
>
> function foo
> {
> $myArgs = $args
> gc <.> | where {$_ -like "*$myArgs*"} | sort
> }
>
> foo '<pattern>'
>
> 2) Is this OK?:
>
> .\list.ps1 '@work'
> or
>
> .\list.ps1 "@work"
>
> --
> Thanks,
> Roman Kuzmin
>
>


My System SpecsSystem Spec
05-14-2007   #4
gurbao


 
 

Re: Newbie: filter rows with variable

On May 14, 12:55 pm, "Roman Kuzmin" <z...@z.z> wrote:
> 1) I guess in this context $args is arguments of a script block {$_ -like
> "*$args*"} passed into Where-Object cmdlet (but, strange, all lines should
> be returned in your example, IMO...). Try this way:
>
> function foo
> {
> $myArgs = $args
> gc <.> | where {$_ -like "*$myArgs*"} | sort
> }
>
> foo '<pattern>'
>
> 2) Is this OK?:
>
> .\list.ps1 '@work'
> or
>
> .\list.ps1 "@work"
>
> --
> Thanks,
> Roman Kuzmin


Hi,

assigning $args to $myargs actually worked :-). Thanks for this.

Calling the script with @work or "@work" returns the error I mention
in the first post.
Calling the script with '@work' (singlequotes) does return the
expected output. But how should I "prepare" my $args so that I do not
have to specify the singlequotes in the parameter?

I tried

$myargs=$args
$myargs = "'"+$myargs+"'"

but that returns the same errormessage as earlier mentioned.

Thanks,

My System SpecsSystem Spec
Reply

RB


Thread Tools


Similar Threads for: Newbie: filter rows with variable
Thread Forum
How do I filter rows in one csv file matching a value in another PowerShell
Alternate rows colors PowerShell
Add-on or tweak for multiple tab rows in IE7... Vista General
help. Digital filter? how to de-filter? USB audio device Vista hardware & devices
How can I ensure that a variable is a built-in powershell variable? 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