![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | -inputobject vs. receiving from the pipe... The two examples of the where cmdlet below show different output, where I expected these statements to execute the same. Why does the latter include the sub directory? PS> dir Directory: Microsoft.PowerShell.Core\FileSystem: :\testMode LastWriteTime Length Name ---- ------------- ------ ---- d---- 10/8/2007 5:11 PM sub -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1 -a--- 10/3/2007 10:52 AM 27 sample.ps1 PS> $d = dir PS> PS> $d | where {$_.length -ge 0} Directory: Microsoft.PowerShell.Core\FileSystem: :\testMode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1 -a--- 10/3/2007 10:52 AM 27 sample.ps1 PS> where {$_.length -ge 0} -InputObject $d Directory: Microsoft.PowerShell.Core\FileSystem: :\testMode LastWriteTime Length Name ---- ------------- ------ ---- d---- 10/8/2007 5:11 PM sub -a--- 10/3/2007 11:08 AM 1642 helloworld_signed.ps1 -a--- 10/3/2007 10:52 AM 27 sample.ps1 PS> |
My System Specs![]() |
| | #2 (permalink) | ||||||||||||
| Guest | RE: -inputobject vs. receiving from the pipe... The sub directory doesn't have a length. Could it be due to different handling of the null length -- Richard Siddaway Please note that all scripts are supplied "as is" and with no warranty Blog: http://richardsiddaway.spaces.live.com/ PowerShell User Group: http://www.get-psuguk.org.uk "Hans Dingemans" wrote:
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #3 (permalink) | ||||||||||||
| Guest | RE: -inputobject vs. receiving from the pipe... I've been working with Get-ChildItem lately, one of the things that I found is that a directory does not have a Length property. While I don't understand why there is a difference in these two statements this may shed some light $d | ? {$_.length -eq $null} ## for me returns _only_ the directories in $d where {$_.length -eq $null} -inputobject $d ## for me returns nothing Just why in the second case $length is a valid property i.e. can be compared to 0 and the former is $null (can not be compared) will need to be answered by someone smarter than I thx bob "Hans Dingemans" wrote:
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #4 (permalink) | ||||||||||||
| Guest | Re: -inputobject vs. receiving from the pipe... It's because in one case the 'length' refers to the number of items in the directory, and in the other it refers to the size of the individual (FileInfo) elements where {$_.length -ge 0} -InputObject $d Here $d is an array, which has 3 elements ie its 'length' property is greater than or equal to 0, so the object gets included. When you display it you see all of its individual elements. $d | where {$_.length -ge 3} Here each element of the array gets passed along the pipeline and 'length' is the length property of a fileinfo object Compare these..... where {$_.length -ge 3} -InputObject $d #Length of $d is 3 so the array is included where {$_.length -ge 4} -InputObject $d #Here it's not included, since there's 3 items in the directory -- Jon "Hans Dingemans" <hans_dingemans@xxxxxx> wrote in message news:Odbm17bCIHA.4712@xxxxxx
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #5 (permalink) | ||||||||||||
| Guest | Re: -inputobject vs. receiving from the pipe... "Jon" <Email_Address@xxxxxx> wrote in message news:ehmn8dcCIHA.1208@xxxxxx
Where-Object is that the cmdlet evaluates the script block and outputs the object passed to -InputObject depending on whether the scriptblock evaluated to true or false. That is, the original object in its entirety is output and not flattened. If you want to filter each element in the object and pass only certain objects down the pipepline then use: $d | where { $_.length -ge 0 } as Jon points out. -- Keith | ||||||||||||
My System Specs![]() | |||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||
| Guest | Re: -inputobject vs. receiving from the pipe... Thanx to all. Still very confusing that $_ variable has different context depending on input via the pipe or input via the switch. It feels like we're missing another variable here, say $__ for the containing collection, besides $_ for the running object within the collection. V2.0? Cheers, Hans "Jon" <Email_Address@xxxxxx> wrote in message news:ehmn8dcCIHA.1208@xxxxxx
| ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
| | #7 (permalink) | ||||||||||||
| Guest | Re: -inputobject vs. receiving from the pipe... > Still very confusing that $_ variable has different context depending on
that forms the distinction. Using the | "moves" individual elements of a collection down the pipeline and -InputObject moves an entire collection as a single object into the current stage of the pipeline. (this would be the primary use for the -InputObject switch.) - Steve Maillet (MVP Windows Embedded CE) | ||||||||||||
My System Specs![]() | |||||||||||||
| | #8 (permalink) | ||||||||||||||||||||||||
| Guest | Re: -inputobject vs. receiving from the pipe... Thanx Steve. I now see the exact purpose of it. Before, I was just thinking there were two ways of doing the same thing, so why having two ways. Point taken!! "Steve Maillet (MVP)" <nospam@xxxxxx> wrote in message news:%231A3zndCIHA.972@xxxxxx
| ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pipe a pipe command to a file | WB | VB Script | 4 | 07-15-2008 09:03 PM |
| Add-Member -InputObject $s -MemberType ScriptMetho | B.Williams | PowerShell | 10 | 04-07-2008 03:03 AM |
| getting arguments from pipe | luntain | PowerShell | 6 | 11-23-2007 10:15 AM |
| ForEach-Object and -InputObject - Any use for it? | Alex K. Angelopoulos [MVP] | PowerShell | 8 | 10-27-2006 12:41 AM |
| Get-Member -InputObject returns generic data; bug? | Alex K. Angelopoulos [MVP] | PowerShell | 3 | 06-20-2006 07:40 PM |