r/PowerShell • u/[deleted] • Dec 07 '19
PowerShell Traps - Collection of PowerShell traps and oddities
https://github.com/nightroman/PowerShellTraps3
u/OlivTheFrog Dec 07 '19
Hi l33t_dOnut
I'm starting to read docs with comments and your tests. very interessting. Im' learning lot of useful things.
Thanks for sharing with the community.
regards
Olivier
2
16
u/jsiii2010 Dec 08 '19 edited Dec 08 '19
You don't need @() to create arrays:
$array = 1,2,3Anonymous functions:
& { param ($message) "the message is $message" } 'hi'
the message is hitriple scriptblock foreach-object:
dir | foreach { $sum = 0 } { $sum++ } { "the sum is $sum" }
the sum is 511Combining multiple statements into one pipeline with $(). Also a less often used version of foreach-object.
$( echo hi; echo there ) | measure | foreach count
2Using assembly and namespace and popping open a dialog box:
using assembly System.Windows.Forms
using namespace System.Windows.Forms
[messagebox]::show('hello world')This is annoying. Passing an array to start-job (or start-threadjob in PS 6). You have to add (, ) to the -argumentlist.
start-job { param ($var) $var.count } -argumentlist (,(1,2,3)) | receive-job -wait -auto
3This is a nice way to pipe from foreach () without defining a function, and you don't have to use a lot of memory or wait for it to finish:
& { foreach ($i in 1..5) { $i } } | measureThere's actually two kinds of exceptions in powershell, command-terminating (usually pipeline terminating) and script-terminating. After the 2nd script-terminating exception from "throw", you don't get the 'hi' that follows. Both of these would work with try/catch.
1/0; 'hi'
Attempted to divide by zero.
At line:1 char:1
+ 1/0; 'hi'
+ ~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
hi
throw; 'hi'
ScriptHalted
At line:1 char:1
+ throw; 'hi'
+ ~~~~~
+ CategoryInfo : OperationStopped: (:) [], RuntimeException
+ FullyQualifiedErrorId : ScriptHaltedOh, I didn't realize there was a link, lol.
11
u/da_chicken Dec 07 '19
Here's more:
An array on the left hand side of a logical operator with a scalar on the right will filter, not compare.
@(1,2,3) -eq 2will return2, notfalse. This most commonly comes up when you want to check if a variable is null. Therefore, best practice is to always put$nullon the left hand side.Arrays and strings are immutable in .Net. That means the
+=operator for both data types causes a tremendous amount of memory pressure as the entire object needs to be recreated.
Compare the execution time:
Measure-Command {
$x = 1..100000
$y = foreach ($i in $x) { $i }
}
Measure-Command {
$x = 1..100000
$y = @()
foreach ($i in $x) { $y += $i }
}3
u/Reverent Dec 08 '19
I didn't see anything about different behavior when an array is a size of one.
If you ingest a CSV with a single element and try to iterate it, bad things happen. This even propogates to Microsoft. Their bulk CSV import for azure ad fails if there's only one element.
4
u/clockKing_out Dec 08 '19
Oh, OK. You found this. I thought you did this TOO and I was like “this guy’s doing it all single handedly?!?!”