-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get-Content/OutVariable-and-ReadCount, #19
- Loading branch information
1 parent
0067931
commit ca1508c
Showing
5 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
Exit-Build {remove z.txt} | ||
|
||
task Test-1.ps1 { | ||
($1, $2, $3, $4 = .\Test-1.ps1) | ||
equals $1 'System.Collections.ArrayList 2 string' | ||
equals $2 'hello,world' | ||
equals $3 'System.Collections.ArrayList 1 System.Object[]' | ||
equals $4 'System.Object[]' | ||
} | ||
|
||
task Test-2-Raw.ps1 { | ||
($1, $2, $3, $4 = .\Test-2-Raw.ps1) | ||
equals $1 'System.Collections.ArrayList 1 string' | ||
equals $2 "hello`nworld" | ||
equals $3 'System.Collections.ArrayList 1 System.Object[]' | ||
equals $4 'System.Object[]' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# `Get-Content` with `OutVariable` and `ReadCount` | ||
|
||
Using `Get-Content` with `OutVariable` and `ReadCount` may produce an | ||
unexpected nested array in the result variable. Using or not using the | ||
switch `Raw` makes no difference. | ||
|
||
The workaround is not using `ReadCount` together with `OutVariable`. | ||
|
||
**Scripts** | ||
|
||
- [Test-1.ps1](Test-1.ps1) shows the issue without `Raw` | ||
- [Test-2-Raw.ps1](Test-2-Raw.ps1) shows the same issue with `Raw` | ||
|
||
--- | ||
|
||
- [PowerShellTraps/issues/19](https://github.com/nightroman/PowerShellTraps/issues/19) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
# Make a text file with 2 lines | ||
$null = New-Item z.txt -Value "hello`nworld" -Force | ||
|
||
### Test 1 (expected result) | ||
|
||
# without -ReadCount | ||
$null = Get-Content z.txt -OutVariable var1 | ||
|
||
# we get a list with 2 string items | ||
"$($var1.GetType()) $($var1.Count) $($var1[0].GetType())" | ||
|
||
# and this gets expected `hello,world` | ||
$var1 -join ',' | ||
|
||
### Test 2 ("unexpected" result) | ||
|
||
# with -ReadCount | ||
$null = Get-Content z.txt -OutVariable var2 -ReadCount 0 | ||
|
||
# we get a list with 1 item, a nested array | ||
"$($var2.GetType()) $($var2.Count) $($var2[0].GetType())" | ||
|
||
# and this gets "unexpected" `System.Object[]` | ||
$var2 -join ',' |
25 changes: 25 additions & 0 deletions
25
Cmdlets/Get-Content/OutVariable-and-ReadCount/Test-2-Raw.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
# Make a text file with 2 lines | ||
$null = New-Item z.txt -Value "hello`nworld" -Force | ||
|
||
### Test 1 (expected result) | ||
|
||
# with -Raw without -ReadCount | ||
$null = Get-Content z.txt -OutVariable var1 -Raw | ||
|
||
# we get a list with 1 string item | ||
"$($var1.GetType()) $($var1.Count) $($var1[0].GetType())" | ||
|
||
# and this gets expected text "hello`nworld" | ||
$var1 -join ',' | ||
|
||
### Test 2 ("unexpected" result) | ||
|
||
# with -Raw and -ReadCount | ||
$null = Get-Content z.txt -OutVariable var2 -Raw -ReadCount 0 | ||
|
||
# we get a list with 1 item, a nested array | ||
"$($var2.GetType()) $($var2.Count) $($var2[0].GetType())" | ||
|
||
# and this gets "unexpected" `System.Object[]` | ||
$var2 -join ',' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters