![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | "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 Specs![]() |
| | #2 (permalink) |
| | 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, Get-Service | Where{ $_.Name -match 'sched' } About function - let me think a few minutes |
My System Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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, PS C:\> function grep Quote: Quote: >> { >> param( [string]$p, [string]$v ) >> $input | where{ $_.$p -match $v } >> } >> 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 } >> } >> 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" } >> } >> Status Name DisplayName ------ ---- ----------- Running WinRM Windows Remote Management (WS-Manag... Maybe I lost something? |
My System Specs![]() |
| | #5 (permalink) |
| | 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>> Quote: > Status Name DisplayName > ------ ---- ----------- > Running Schedule Task Scheduler > 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 Specs![]() |
| | #6 (permalink) |
| | 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, >> > > PS C:\> function grep > Quote: Quote: >>> { >>> param( [string]$p, [string]$v ) >>> $input | where{ $_.$p -match $v } >>> } > > 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 } >>> } > > 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" } >>> } > > Status Name DisplayName > ------ ---- ----------- > Running WinRM Windows Remote Management (WS-Manag... > Maybe I lost something? > |
My System Specs![]() |
| | #7 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||