header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

We have a new sponsor!  Introducting Pragma Systems.  See the home page for details.

foreach-object with () and {}
Last Post 11 Mar 2010 05:28 PM by EBGreen. 3 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
ddUser is Offline
New Member
New Member
Posts:1
Avatar

--
11 Mar 2010 02:41 PM  
helo,
I'm annoyed with what seems to me a strange behaviour of the foreach-object block.
When using the following code in the shell
".dir", $null, ".pdf" | Foreach-object (Write-host "Hello within ()") {"begin scriptblock within {}"} {"process scriptblock within {}"} {"end scriptblock within {}"} (write-host " last block in line within ()")

I obtain this result:

Hello within ()
 last block in line within ()
 begin scriptblock within {}
 process scriptblock within {}
 process scriptblock within {}
 process scriptblock within {}
 end scriptblock within {}

I have no problems with everything in the {} scriptblocks.
But I do not understand why the instructions in ( ) are not considered as errors and why the last block with ( )
is executed before the scriptblocks.

The question arises because I try to find out how to interpret correctly the syntax rules returned by the get-help.
PowerShellJediUser is Offline
Basic Member
Basic Member
Posts:298
Avatar

--
11 Mar 2010 05:21 PM  
I am not familiar with the "within"...

But the Syntax for ForEach-Object looks more like this...


$array = ".dir", $null, ".pdf"
$array | ForEach-Object{
$_
}


ForEach-Object handles each object 1 at a time "$_" means "The current pipeline object".

So, What ever your code ends up looking like you will have to use $_ inside of ForEach-Object also you need to use the "{}" brackets.
PoSH is a Automation Technology surfaced as a scripting language, not a "spice" ;-)

Innotask Technologies
Tech Support for small & mid sized companies.
EBGreenUser is Online
Basic Member
Basic Member
Posts:429
Avatar

--
11 Mar 2010 05:28 PM  
Just to followup on what Jedi had to say, essentially you are doing something contrary to how the documentation says to use the cmdlet.
"Look Ma...no strings!"
PowerShellJediUser is Offline
Basic Member
Basic Member
Posts:298
Avatar

--
11 Mar 2010 05:28 PM  
You might want to do a google/bing search for ForEach-Object Examples
PoSH is a Automation Technology surfaced as a scripting language, not a "spice" ;-)

Innotask Technologies
Tech Support for small & mid sized companies.
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 footer
footer