Profile

Cover photo
Rob Campbell
25 followers|1,123 views
AboutPostsPhotosVideos

Stream

Rob Campbell

Shared publicly  - 
 
An observation made while working on my Event 5 entry for the scripting games - filter vs foreach-object

Dedupe 100,000 items with a hash table, using foreach-object, vs doing the same operation with a filter:

$array = (1..1e3) * 100

(measure-command {
                 $ht =@{}
                 $array | foreach-object {$ht[$_]=$true}
                 }).totalmilliseconds

(measure-command {
                  filter Dedupe {$ht[$_]=$true}
                  $ht=@{}
                  $array | Dedupe
                  }).totalmilliseconds
3308.6162
552.1184
5
1
Oleg Soroka's profile photoJason Morgan's profile photo
 
Rob, what about filter vs where-object?  I guess I'm not sure what the difference is between using Where and making a filter (or is it a filter function?)  I just ran that exact scenario with:

Measure-Command {$ht=@{}; $array |Where { $ht[$_]=$true } }
592
and

Measure-Command {$ht=@{}; $array | dedupe }
494

I've always used where as a filter in the middle of my pipelines but now I'm starting to think I might gain some, marginal, advantage using a filter.
Add a comment...

Rob Campbell

Shared publicly  - 
4
1
Justin Rich's profile photoJames Tryand's profile photoVern Anderson's profile photo
3 comments
 
That's just awesome I wish Don Jones could see this!!!
Add a comment...

Rob Campbell

Shared publicly  - 
 
Private proxy function - good idea or not?
function Private:get-acl {get-acl select | -expand Access}

Added to your profile, changes the way get-acl works from the console, but not from a child scope (called script, invoke-command, etc).

The private scope means you can use the cmdlet name you're proxying in the function script block without needing $ExecutionContext.InvokeCommand.GetCommand('Get-ChildItem', [System.Management.Automation.Command Types]::Cmdlet)

The Private: options hides the function from child scopes, even the function's own scope can't see it so you don't get the recursion loop.
1
Kirk Munro's profile photoJustin Rich's profile photoChris Duck's profile photoRob Campbell's profile photo
9 comments
 
Yes you would. You can hide the private proxy functions from pasted code easily enough by starting with &{. Then do the paste, and close the brace, and the pasted code will run in a child scope.
Add a comment...
Have him in circles
25 people
Boe Prox's profile photo
Rich Prescott's profile photo
Don Jones's profile photo
Shay Levy's profile photo
Will Steele's profile photo
Kirk Munro's profile photo

Rob Campbell

Shared publicly  - 
 
Maybe?

$test10_vals =@{$false= 'Ten or less';$true = 'More than ten'}
$x = 12
$test10_vals[($x -ge 10)]
 
What's the most elegant way people have found to implement a ternary/conditional operator (like iif, or "expr ? val_if_true : val_if_false") in PowerShell?

Bonus points if you can tell me why it still doesn't exist in the language! ;o(
1
Add a comment...

Rob Campbell

Shared publicly  - 
 
Lots of talk about PS V3 (W8 beta), and scripting "best practices" (SG2012) going around, got me to thinking.

It seems to me that "best practice" (at least a good practice for an enterprise admin) would be to write scripts using Begin and End blocks

http://mjolinor.wordpress.com/2012/03/11/begin-process-end-not-just-for-functions/

and to try to minimize the End block, and splat everything that uses more than one parameter.

When it comes time to make that script run as a background or remote job or in a workflow there's a good chance you'll need to do some re-factoring to get the parameters and variables passed to it.

If you've got the bulk of your code defined in your Begin block as functions, that should translate directly to the initialization script of the job. That leaves you with just the End block to re-factor for the scriptblock of the job. If you've got that reduced to just calling functions from your Begin block and splatting the parameters then that becomes a process of cutting the hash tables out of the End block and pasting them into the local script to populate values and changing the splats in what's left to @using:parameter_hash.

Thoughts?
1
Rob Campbell's profile photo
3 comments
 
Maybe. Right now I'm just kind of thinking out loud, and doing a sanity check to see if there's something I missed that makes this a bad idea.
Add a comment...
People
Have him in circles
25 people
Boe Prox's profile photo
Rich Prescott's profile photo
Don Jones's profile photo
Shay Levy's profile photo
Will Steele's profile photo
Kirk Munro's profile photo
Basic Information
Gender
Male