Register
: :
Login
Home
Forums
Blogs
Podcast
Directories
Scripts
Downloads
Community
User Group Support
Learning Resources
We have a new sponsor! Introducting
Pragma Systems
. See the home page for details.
Unanswered
Active Topics
Forums
Search
Members
Forums
>
Using PowerShell
>
General PowerShell
foreach-object with () and {}
Last Post 11 Mar 2010 05:28 PM by
EBGreen
. 3 Replies.
Sort:
Oldest First
Most Recent First
Prev
Next
You are not authorized to post a reply.
Author
Messages
dd
New Member
Posts:1
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.
PowerShellJedi
Basic Member
Posts:298
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.
EBGreen
Basic Member
Posts:429
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!"
PowerShellJedi
Basic Member
Posts:298
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.
PowerShellCommunity.org
--Community Announcements and Assistance
--Completely Unrelated
--User Groups
--Community Business
----Suggestion Box
Using PowerShell
--General PowerShell
--Books, Tools, and Videos
--Exchange Server
--Active Directory
--System Center Family
--Non-Microsoft Products
--SharePoint
--SQL Server
--Working with .NET
--Peer Review
--Testing, Testing...
PowerShell Development
--Cmdlet Development
--PSDrive Provider Development
--Hosting the Shell
Looking Ahead
--Using PowerShell v2.0
--Developing for PowerShell v2.0
Forums
>
Using PowerShell
>
General PowerShell
Active Forums 4.1
Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008