Skip to content

Commit f872e16

Browse files
Merge pull request #664 from FH-Inway/select-psfobject-with-non-object-variables
Select psfobject with non-object variables
2 parents aab5bf9 + e5a80b7 commit f872e16

5 files changed

Lines changed: 36 additions & 8 deletions

File tree

PSFramework/bin/PSFramework.dll

0 Bytes
Binary file not shown.

PSFramework/bin/PSFramework.pdb

4 KB
Binary file not shown.

PSFramework/bin/PSFramework.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PSFramework/tests/functions/utility/Select-PSFObject.Tests.ps1

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Describe "Select-PSFObject Unit Tests" -Tag "UnitTests" {
1+
Describe "Select-PSFObject Unit Tests" -Tag "UnitTests" {
22
BeforeAll {
33
$object = [PSCustomObject]@{
44
Foo = 42
@@ -18,6 +18,8 @@
1818
Bar = 88
1919
Tara = 28
2020
}
21+
22+
$variable = 42
2123
}
2224

2325
Describe "Basic DSL functionalities" {
@@ -34,17 +36,26 @@
3436
($object2 | Select-PSFObject -Property 'Foo size KB:1:1').Foo | Should -Be "41 KB"
3537
}
3638
}
37-
38-
Describe "Selects from other variables" {
39-
It "picks values from other variables" {
39+
40+
Describe "Selects from other object variables" {
41+
It "picks values from other object variables" {
4042
($object2 | Select-PSFObject -Property 'Tara from object').Tara | Should -Be 21
4143
}
4244

4345
It "picks values from the properties of the right object in a list" {
4446
($object2 | Select-PSFObject -Property 'Tara from List where Foo = Bar').Tara | Should -Be 28
4547
}
4648
}
47-
49+
50+
Describe "Selects from other non-object variables" {
51+
It "picks values from other non-object variables" {
52+
($object2 | Select-PSFObject -Property '$variable as Tara').Tara | Should -Be 42
53+
}
54+
It "uses the variable name as property name" {
55+
($object2 | Select-PSFObject -Property '$variable').variable | Should -Be 42
56+
}
57+
}
58+
4859
Describe "Display Settings are applied" {
4960
It "sets the correct properties to show in whitelist mode" {
5061
$obj = [PSCustomObject]@{ Foo = "Bar"; Bar = 42; Right = "Left" }

library/PSFramework/Parameter/SelectParameter.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace PSFramework.Parameter
77
{
88
/// <summary>
9-
/// Class that automatically parses input chosen for the -Property parameter of Select-PSUObject
9+
/// Class that automatically parses input chosen for the -Property parameter of Select-PSFObject
1010
/// </summary>
1111
public class SelectParameter : ParameterClass
1212
{
@@ -23,6 +23,14 @@ public SelectParameter(string Value)
2323
{
2424
InputObject = Value;
2525

26+
// check if Value is a variable name (i.e. starts with $ and is a single word)
27+
// if so, the variable value is stored in a property with the same name
28+
if (Value.StartsWith("$") && !Value.Contains(" "))
29+
{
30+
Value = Value + " as " + Value.Substring(1);
31+
}
32+
33+
// check if Value is a property name (i.e. is a single word)
2634
if (!Value.Contains(" "))
2735
{
2836
this.Value = Value;
@@ -119,7 +127,16 @@ public SelectParameter(string Value)
119127
}
120128

121129
if (propertyName != ".")
122-
stringValue = String.Format("{0}.{1}", stringValue, valueName);
130+
{
131+
if (valueName.StartsWith("$"))
132+
{
133+
stringValue = valueName;
134+
}
135+
else
136+
{
137+
stringValue = String.Format("{0}.{1}", stringValue, valueName);
138+
}
139+
}
123140

124141
// <guid> = $_
125142
// "(<size>(<cast>(<value>))<sizeName>)"

0 commit comments

Comments
 (0)