Example of the function in action:
Echo-ArgValue 'one','two'
$MyArrayArgument:one two
But when you take that line of code out it changes thus:
Echo-ArgValue 'one','two'
$MyArrayArgument:one
function Echo-ArgValue {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$true)]
[string[]]
$MyArrayArgument
)
[void]$MyArrayArgument.Length
write-host('$MyArrayArgument:{0}' -f $MyArrayArgument)
}
So what is the ideal way to get the desired (first) behavior?
--
Bern McCarty
Bentley Systems, Inc.
However, I would be very curious what's the reason why the
[void...Length statement causes that it works.
- Larry
Two very different behaviors are observed. They cannot both be right can
they? If they are indeed both right, what is the explanation for that? If
not, which behavior is right and which one is the bug? If it is a bug then an
explanation of the bug could lead to learning how to better live with it.
--
Bern McCarty
Bentley Systems, Inc.
"Larry__Weiss" wrote:
> .
>
function show-bug {
$A = 'one','two'
'$A:{0}' -f $A
$A.length
'$A:{0}' -f $A
}
It is interesting that this variation also shows the bug
function show-bug {
$A = 'one','two'
'$A:{0}' -f $A
$A.xxxxxxxxxxxxxxxxx
'$A:{0}' -f $A
}
- Larry
you may check this in another way. Type in console this:
gps explorer
dir | ft
looks pretty well? Try this:
function test {
gps explorer
dir | ft
}
and call this function. This is the same as:
gps explorer; dir | ft
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv
"Bern McCarty" <Be...@newsgroups.nospam> rakstīja ziņojumā
"news:FDE48D2C-7335-4090...@microsoft.com"...
$A = 'one','two'
'$A:{0}' -f $A
$A.xxxxxxxxxxxxxxxxx
'$A:{0}' -f $A
$A = 'one','two'
'$A:{0}' -f $A
$A.xxxxxxxxxxxxxxxxx
'$A:{0}' -f $A
I don't see how a pipeline is involved.
- Larry
"Larry__Weiss" <l...@airmail.net> rakstīja ziņojumā
"news:u21uELfb...@TK2MSFTNGP06.phx.gbl"...
should be equivalent to
[string]::Format('$A:{0}',$A)
And this sequence seems to work out more consistently to what I'd expect
$A = 'one','two'
[string]::Format('$A:{0}',$A)
$A.xxxxxxxxxxxxxxxxx
[string]::Format('$A:{0}',$A)
as executed below
PS C:> $A = 'one','two'
PS C:> [string]::Format('$A:{0}',$A)
$A:one
PS C:> $A.xxxxxxxxxxxxxxxxx
PS C:> [string]::Format('$A:{0}',$A)
$A:one
PS C:>
and again, coding it with
'$A:{0}' -f $A
I get
PS C:> $A = 'one','two'
PS C:> '$A:{0}' -f $A
$A:one
PS C:> $A.xxxxxxxxxxxxxxxxx
PS C:> '$A:{0}' -f $A
$A:one two
PS C:>
Am I right that these two are equivalent?
'$A:{0}' -f $A
[string]::Format('$A:{0}',$A)
I'm asking because (these are your examples) I didn't know that
$A.xxxxxxxxxxxxxxxxx
'$A:{0}' -f $A
is the same as
$A.xxxxxxxxxxxxxxxxx, $A:{0} -f $A
stej
"stej" <cerna...@seznam.cz> rakstīja ziņojumā
"news:96559512-7afd-4a6d...@l13g2000yqb.googlegroups.com"...
PS C:> $b = 7,8,9
PS C:> '[{0}]' -f $b
[7]
PS C:> '[{0}]' -f $b
[7]
PS C:> '[{0}]' -f $b.length
[3]
PS C:> '[{0}]' -f $b
[7 8 9]
PS C:> '[{0}]' -f $b
[7 8 9]
PS C:>
Why are the last two expansions of
'[{0}]' -f $b
not the same as the first two?
- Larry
PS C:> $b = 7,8,9
PS C:> '[{0}]' -f $b
[7]
PS C:> '[{0}]' -f $b.length
[3]
PS C:> '[{0}]' -f $b
[7 8 9]
PS C:> $b = $b
PS C:> '[{0}]' -f $b
[7]
PS C:>
Stranger and stranger!
- Larry
Larry__Weiss wrote:
> I think I'm going to open a bug report on this at
> https://connect.microsoft.com/powershell
>
PS C:> $b = 7,8,9
PS C:> '[{0}][{1}][{2}]' -f $b
[7][8][9]
PS C:> '[{0}]' -f $b.length
[3]
PS C:> '[{0}][{1}][{2}]' -f $b
Error formatting a string: Index (zero based) must be greater than or equal to
zero and less than the size of the argument list..
At line:1 char:21
+ '[{0}][{1}][{2}]' -f <<<< $b
+ CategoryInfo : InvalidOperation: (7 8 9:PSObject) [], RuntimeEx
ception
+ FullyQualifiedErrorId : FormatError
PS C:> '[{0}][{1}][{2}]' -f @($b)
[7][8][9]
PS C:>
- Larry
Larry__Weiss wrote:
> Here's an extra wrinkle
>
> PS C:> $b = 7,8,9
> PS C:> '[{0}]' -f $b
> [7]
> PS C:> '[{0}]' -f $b.length
> [3]
> PS C:> '[{0}]' -f $b
> [7 8 9]
> PS C:> $b = $b
> PS C:> '[{0}]' -f $b
> [7]
> PS C:>
>
> Stranger and stranger!
>
>
https://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=518276
"-f format operator bug when right-side value is an array variable"
Please leave feedback there if you can reproduce it, and/or, have comments to share.
Thx!
- Larry
Definitely a bug Larry.
PS C:\projects\powershell> $b = [int[]]@(1,2)
PS C:\projects\powershell> $b.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32[]
System.Array
PS C:\projects\powershell> "{0}" -f $b
System.Int32[]
PS C:\projects\powershell> $b.length
2
PS C:\projects\powershell> "{0}" -f $b
1 2
Well spotted. I've notified the team and will report back.
-Oisin
www.nivot.org
PowerShell MVP
I opened a new bug issue that is related to this at
https://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=518276
"-f format operator bug when right-side value is an array variable"
- Larry
"Larry__Weiss" <l...@airmail.net> rakstija zinojuma
"news:uTAzjWVd...@TK2MSFTNGP06.phx.gbl"...
"Larry__Weiss" <l...@airmail.net> rakstija zinojuma
"news:uTAzjWVd...@TK2MSFTNGP06.phx.gbl"...
> $a = 1,2,3
# THIS is what we expect when we put an array in a string in
PowerShell
> "$a"
1 2 3
# THIS is therefore a bug
> "{0}" -f $a
1
# So is this:
> "{0},{1}" -f $a,"one"
System.Object[],one
I originally posted a "workaround" to use @() as a fix -- it causes
the original behavior every time by splatting the array ... but I
realized afterward that THAT is the buggy behavior:
# This isn't the way arrays are supposed to get formatted:
> "{0},{1}" -f @($a),"one"
System.Object[],one
# THIS is the way arrays are supposed to get formatted:
> "{0},{1}" -f ([PSObject]$a),"one"
1 2 3,one
You can force this behavior by always casting arrays to [PSObject]
before using them. You can even strong type them too:
> [PSObject][Int[]]$a = 1,2,3
> "$a"
1 2 3
> $OFS = "-"
> "$a"
1-2-3
> "{0} {1}" -f $a, "GO!"
1-2-3 GO!
--
Joel "Jaykul" Bennett
http://HuddledMasses.org
Upstate NY PowerShell UserGroup:
http://upnypug.wordpress.com
"{0}" -f $a
should be equivalent to
[string]::Format('{0}',$a)
And, in fact that's what I get:
PS C:> $a = 1,2,3
PS C:> "{0}" -f $a
1
PS C:> [string]::Format('{0}',$a)
1
Yet, using the cast to [PSObject]
PS C:> $a = 1,2,3
PS C:> "{0}" -f [PSObject]$a
1 2 3
PS C:> [string]::Format('{0}',[PSObject]$a)
1
Should that last command have output
1 2 3
as well ?
- Larry