Print all properties

350 views
Skip to first unread message

Georgios Petrou

unread,
Jan 17, 2013, 1:09:16 PM1/17/13
to psake...@googlegroups.com
How can I easily print all properties I define for psake?

For example I have:

properties {   
    $property1 = "Value1"
    $property2 = "Value2"
    $property3 = "Value3"
    $property4 = "Value4"
}

I can print each property by using its name, but if I want to do something like 
$properties | ForEach {write-host "$_.Name = $_.Value"}
I get System.Collections.Hashtable.Name = System.Collections.Hashtable.Value

Can someone help?

James Kovacs

unread,
Jan 17, 2013, 1:31:27 PM1/17/13
to psake...@googlegroups.com
There is some strangeness in PowerShell string interpolation, which is what you're doing when you say "$_.Name = $_.Value". The following works:

$properties.Keys | foreach { write-host "$_ = $($properties[$_])" }

Because it's a hashtable, you can't iterate through it, but you can index it by all its keys. Also note the extra $() around the $properties[$_].

HTH,
James

Georgios Petrou

unread,
Jan 17, 2013, 1:38:26 PM1/17/13
to psake...@googlegroups.com
Thanks for the quick reply.

I tried the following: 

properties {   
    $property1 = "Value1"
    $property2 = "Value2"
    $property3 = "Value3"
    $property4 = "Value4"
}

Task Default -Depends Test

Task ? {
    Write-Host "Properties default values" 
    $properties.Keys | foreach { write-host "$_ = $($properties[$_])" }
    
}

Task Test {
    Write-Host "TEST"
}

However when I run I get 

psake version 4.2.0
Copyright (c) 2010 James Kovacs

Executing ?
Properties default values

Build Succeeded!

----------------------------------------------------------------------
Build Time Report
----------------------------------------------------------------------
Name   Duration        
----   --------        
?      00:00:00.0405996
Total: 00:00:00.0745816

James Kovacs

unread,
Jan 17, 2013, 1:54:22 PM1/17/13
to psake...@googlegroups.com
"properties" in psake is actually a function that takes a script block. The actual storage of the properties is in a context, which allows us to nest calls to psake. (e.g. psake can call itself to execute nested builds without stomping all over itself.) To get at the properties in the current context, you would need to do something like this:
$properties = $psake.context.Peek().properties
This is internal implementation details and not something you should rely on. It would be useful to create a Write-Properties function to dump out this information. Patches always welcome.

James

Georgios Petrou

unread,
Jan 18, 2013, 5:15:28 AM1/18/13
to psake...@googlegroups.com
Actually, Write-Host $psake.context.Peek().properties seems to be working fine for what I wanted to do.
It outputs:

Executing ?
Properties default values
   
    $property1 = "Value1"
    $property2 = "Value2"
    $property3 = "Value3"
    $property4 = "Value4"


Build Succeeded!

Thanks again for the help.

Mike Duran

unread,
Feb 14, 2015, 1:05:10 PM2/14/15
to psake...@googlegroups.com
This doesn't seem to work if you override the properties from the command line
Reply all
Reply to author
Forward
0 new messages