Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 676 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Powershell pitfalls

#1
What Powershell pitfalls you have fall into? :-)

Mine are:

# -----------------------------------
function foo()
{
@("text")
}

# Expected 1, actually 4.
(foo).length

# -----------------------------------
if(@($null, $null))
{
Write-Host "Expected to be here, and I am here."
}

if(@($null))
{
Write-Host "Expected to be here, BUT NEVER EVER."
}

# -----------------------------------

function foo($a)
{
# I thought this is right.
#if($a -eq $null)
#{
# throw "You can't pass $null as argument."
#}

# But actually it should be:
if($null -eq $a)
{
throw "You can't pass $null as argument."
}
}

foo @($null, $null)

# -----------------------------------

# There is try/catch, but no callstack reported.
function foo()
{
bar
}

function bar()
{
throw "test"
}

# Expected:
# At bar() line:XX
# At foo() line:XX
#
# Actually some like this:
# At bar() line:XX
foo


Would like to know yours to walk them around :-)
Reply

#2
My personal favorite is

function foo() {
param ( $param1, $param2 = $(throw "Need a second parameter"))
...
}

foo (1,2)

For those unfamiliar with powershell that line throws because instead of passing 2 parameters it actually creates an array and passes one parameter. You have to call it as follows

foo 1 2
Reply

#3
Another fun one. Not handling an expression by default writes it to the pipeline. Really annoying when you don't realize a particular function returns a value.

function example() {
param ( $p1 ) {
if ( $p1 ) {
42
}
"done"
}

PS> example $true
42
"done"
Reply

#4
This works. But almost certainly not in the way you think it's working.

PS> $a = 42;
PS> [scriptblock]$b = { $a }
PS> & $b
42

Reply

#5
Here is something Ive stumble upon lately (PowerShell 2.0 CTP):

$items = "item0", "item1", "item2"

$part = ($items | select-string "item0")

$items = ($items | where {$part -notcontains $_})

what do you think that $items be at the end of the script?

I was expecting "item1", "item2" but instead the value of $items is: "item0", "item1", "item2".
Reply

#6
alex2k8, I think this example of yours is good to talk about:

# -----------------------------------
function foo($a){
# I thought this is right.
#if($a -eq $null)
#{
# throw "You can't pass $null as argument."
#}
# But actually it should be:
if($null -eq $a)
{
throw "You can't pass $null as argument."
}
}
foo @($null, $null)

PowerShell can use some of the comparators against arrays like this:

$array -eq $value
## Returns all values in $array that equal $value

With that in mind, the original example returns two items (the two $null values in the array), which evalutates to $true because you end up with a collection of more than one item. Reversing the order of the arguments stops the array comparison.

This functionality is very handy in certain situations, but it is something you need to be aware of (just like array handling in PowerShell).
Reply

#7
This one has tripped me up before, using $o.SomeProperty where it should be $($o.SomeProperty).
Reply

#8
# $x is not defined
[70]: $x -lt 0
True
[71]: [int]$x -eq 0
True

So, what's $x..?
Reply

#9
Functions 'foo' and 'bar' looks equivalent.

function foo() { $null }
function bar() { }

E.g.

(foo) -eq $null
# True

(bar) -eq $null
# True

But:

foo | %{ "foo" }
# Prints: foo

bar | %{ "bar" }
# PRINTS NOTHING

Returning $null and returning nothing is not equivalent dealing with pipes.

---
This one is inspired by **Keith Hill** example...

function bar() {}

$list = @(foo)
$list.length
# Prints: 0

# Now let's try the same but with a temporal variable.
$tmp = foo
$list = @($tmp)
$list.length
# Prints: 1


Reply

#10
# The pipeline doesn't enumerate hashtables.
$ht = @{"foo" = 1; "bar" = 2}
$ht | measure

# Workaround: call GetEnumerator
$ht.GetEnumerator() | measure

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through