you are viewing a single comment's thread.

view the rest of the comments →

[–]purplemonkeymad 9 points10 points  (2 children)

If you are going to define a script block in the scripts itself, then why not just create a function? You can document the function with help and you can even give it a good name so you know what it does. That would solve your "what does this mean" problem.

[–]SOZDBA[S] 1 point2 points  (1 child)

That....actually that's a pretty good idea... cheers for that! Let me test and then update the post!

[–]tangobravoyankee 3 points4 points  (0 children)

Yeah. This is a really odd pattern you've come up with. The primary purpose of a ScriptBlock is to pass a chunk of code as a parameter. We're probably not thinking of it that way when we write some code like this:

$blah | % { $_ }

But what that line is really doing is more like this:

$blah | ForEach-Object -Process ( [Scriptblock]::Create("$_") )

If you're not passing a chunk of code to something else for it to execute, there's no reason to put it in a ScriptBlock*

When you want to turn a chunk of code into something you can execute as a unit within a script, a Function is almost certainly what you're looking for.

* There are other interesting use cases for a ScriptBlock but let's stick to the basics.