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 - "Dynamic" properties in functions.

Reply
 
Old 03-19-2008   #1 (permalink)
dvl


 
 

"Dynamic" properties in functions.

Hi,

I'm trying to write something like this:

Function grep
{
param([string]$prop, [string]$expr)
$input | where { $_.$prop -like "$expr" }
}

Example of use:

get-services | grep "Name", "sched*"

It shoud return only the "schedule" service, but returns all of them.

The following command returns what I want (only the "schedule" service
record):

$x="Name";$y="sched*";get-service | where { $_.$x -like "$y" }

The question is: why it doesn't work in a function?

Regards,

My System SpecsSystem Spec
Old 03-19-2008   #2 (permalink)
ajax76


 
 

Re: "Dynamic" properties in functions.

On 20 ÍÁÒ, 03:45, dvl <d...@xxxxxx> wrote:
Quote:

> Hi,
>
> I'm trying to write something like this:
>
> Function grep
> {
> š param([string]$prop, [string]$expr)
> š $input | where { $_.$prop -like "$expr" }
>
> }
>
> Example of use:
>
> get-services | grep "Name", "sched*"
>
> It shoud return only the "schedule" service, but returns all of them.
>
> The following command returns what I want (only the "schedule" service
> record):
>
> $x="Name";$y="sched*";get-service | where { $_.$x -like "$y" }
>
> The question is: why it doesn't work in a function?
>
> Regards,
What about this:
Get-Service | Where{ $_.Name -match 'sched' }

About function - let me think a few minutes
My System SpecsSystem Spec
Old 03-19-2008   #3 (permalink)
Shay Levi


 
 

Re: "Dynamic" properties in functions.



Try:


function grep($prop,$expr){
while ($input.MoveNext()) {
if($input.current.$prop -like $expr){
Write-Output $input.current
}
}
}



PS > get-service | grep Name sched*


Status Name DisplayName
------ ---- -----------
Running Schedule Task Scheduler



PS > get-process | grep ProcessName ls*

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
651 12 5452 6316 47 244.59 668 lsass




-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Hi,
>
> I'm trying to write something like this:
>
> Function grep
> {
> param([string]$prop, [string]$expr)
> $input | where { $_.$prop -like "$expr" }
> }
> Example of use:
>
> get-services | grep "Name", "sched*"
>
> It shoud return only the "schedule" service, but returns all of them.
>
> The following command returns what I want (only the "schedule" service
> record):
>
> $x="Name";$y="sched*";get-service | where { $_.$x -like "$y" }
>
> The question is: why it doesn't work in a function?
>
> Regards,
>

My System SpecsSystem Spec
Old 03-19-2008   #4 (permalink)
ajax76


 
 

Re: "Dynamic" properties in functions.

On 20 ÍÁÒ, 03:45, dvl <d...@xxxxxx> wrote:
Quote:

> Hi,
>
> I'm trying to write something like this:
>
> Function grep
> {
> š param([string]$prop, [string]$expr)
> š $input | where { $_.$prop -like "$expr" }
>
> }
>
> Example of use:
>
> get-services | grep "Name", "sched*"
>
> It shoud return only the "schedule" service, but returns all of them.
>
> The following command returns what I want (only the "schedule" service
> record):
>
> $x="Name";$y="sched*";get-service | where { $_.$x -like "$y" }
>
> The question is: why it doesn't work in a function?
>
> Regards,
All is work:

PS C:\> function grep
Quote:
Quote:

>> {
>> param( [string]$p, [string]$v )
>> $input | where{ $_.$p -match $v }
>> }
>>
PS C:\> get-service | grep 'name' 'winrm'

Status Name DisplayName
------ ---- -----------
Running WinRM Windows Remote Management (WS-Manag...


PS C:\> function grep
Quote:
Quote:

>> {
>> param( [string]$p, [string]$v )
>> $input | where{ $_.$p -like $v }
>> }
>>
PS C:\> get-service | grep 'name' 'winrm'

Status Name DisplayName
------ ---- -----------
Running WinRM Windows Remote Management (WS-Manag...


PS C:\> function grep
Quote:
Quote:

>> {
>> param( [string]$p, [string]$v )
>> $input | where{ $_.$p -like "$v" }
>> }
>>
PS C:\> get-service | grep 'name' 'winrm'

Status Name DisplayName
------ ---- -----------
Running WinRM Windows Remote Management (WS-Manag...

Maybe I lost something?
My System SpecsSystem Spec
Old 03-19-2008   #5 (permalink)
Shay Levi


 
 

Re: "Dynamic" properties in functions.


Another version, shorter


function grep($prop,$expr){
begin{}; process{ if($_.$prop -like $expr){$_} }
}


PS> gsv | grep Name sched*


Status Name DisplayName
------ ---- -----------
Running Schedule Task Scheduler



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Try:
>
> function grep($prop,$expr){
> while ($input.MoveNext()) {
> if($input.current.$prop -like $expr){
> Write-Output $input.current
> }
> }
> }
PS>> get-service | grep Name sched*
PS>>
Quote:

> Status Name DisplayName
> ------ ---- -----------
> Running Schedule Task Scheduler
>
PS>> get-process | grep ProcessName ls*
PS>>
Quote:

> Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
> ------- ------ ----- ----- ----- ------ -- -----------
> 651 12 5452 6316 47 244.59 668 lsass
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
Quote:

>> Hi,
>>
>> I'm trying to write something like this:
>>
>> Function grep
>> {
>> param([string]$prop, [string]$expr)
>> $input | where { $_.$prop -like "$expr" }
>> }
>> Example of use:
>> get-services | grep "Name", "sched*"
>>
>> It shoud return only the "schedule" service, but returns all of them.
>>
>> The following command returns what I want (only the "schedule"
>> service record):
>>
>> $x="Name";$y="sched*";get-service | where { $_.$x -like "$y" }
>>
>> The question is: why it doesn't work in a function?
>>
>> Regards,
>>

My System SpecsSystem Spec
Old 03-19-2008   #6 (permalink)
Shay Levi


 
 

Re: "Dynamic" properties in functions.


You're OK ajax76, you made me wonder why I complicated it

dvl, It doesn't work because:

1. There is a typo. get-services should be get-service.
2. When assigning values to cmdlet parameters you should delimit them with
a space instead of a comma (... | grep Name sched*).

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> On 20 мар, 03:45, dvl <d...@xxxxxx> wrote:
>
Quote:

>> Hi,
>>
>> I'm trying to write something like this:
>>
>> Function grep
>> {
>> param([string]$prop, [string]$expr)
>> $input | where { $_.$prop -like "$expr" }
>> }
>>
>> Example of use:
>>
>> get-services | grep "Name", "sched*"
>>
>> It shoud return only the "schedule" service, but returns all of them.
>>
>> The following command returns what I want (only the "schedule"
>> service record):
>>
>> $x="Name";$y="sched*";get-service | where { $_.$x -like "$y" }
>>
>> The question is: why it doesn't work in a function?
>>
>> Regards,
>>
> All is work:
>
> PS C:\> function grep
>
Quote:
Quote:

>>> {
>>> param( [string]$p, [string]$v )
>>> $input | where{ $_.$p -match $v }
>>> }
> PS C:\> get-service | grep 'name' 'winrm'
>
> Status Name DisplayName
> ------ ---- -----------
> Running WinRM Windows Remote Management (WS-Manag...
> PS C:\> function grep
>
Quote:
Quote:

>>> {
>>> param( [string]$p, [string]$v )
>>> $input | where{ $_.$p -like $v }
>>> }
> PS C:\> get-service | grep 'name' 'winrm'
>
> Status Name DisplayName
> ------ ---- -----------
> Running WinRM Windows Remote Management (WS-Manag...
> PS C:\> function grep
>
Quote:
Quote:

>>> {
>>> param( [string]$p, [string]$v )
>>> $input | where{ $_.$p -like "$v" }
>>> }
> PS C:\> get-service | grep 'name' 'winrm'
>
> Status Name DisplayName
> ------ ---- -----------
> Running WinRM Windows Remote Management (WS-Manag...
> Maybe I lost something?
>

My System SpecsSystem Spec
Old 03-19-2008   #7 (permalink)
dvl


 
 

Re: "Dynamic" properties in functions.

Thanks for the reply.

It's all my fault. I've failed to specify function parameters correctly.

function f($a, $b)

invoked like this: f a,b

will result in $a="a,b" and empty $b.

Silly error.

Regards,


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Restrict Dynamic Port Mapping for "Remote" Powershell Sessions PowerShell
Cannot access properties in "local area connection properties" win Vista networking & sharing
"Dynamic" properties PowerShell
MS Plugin "Save to PDF" no longer functions correctly Vista General
IDEA: scriptmethod "method overload" (and functions too) 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