File tree 5 files changed +86
-0
lines changed
Cmdlets/Get-Content/OutVariable-and-ReadCount
5 files changed +86
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ Exit-Build {remove z.txt}
3
+
4
+ task Test-1.ps1 {
5
+ ($1 , $2 , $3 , $4 = .\Test-1.ps1 )
6
+ equals $1 ' System.Collections.ArrayList 2 string'
7
+ equals $2 ' hello,world'
8
+ equals $3 ' System.Collections.ArrayList 1 System.Object[]'
9
+ equals $4 ' System.Object[]'
10
+ }
11
+
12
+ task Test-2 - Raw.ps1 {
13
+ ($1 , $2 , $3 , $4 = .\Test-2 - Raw.ps1)
14
+ equals $1 ' System.Collections.ArrayList 1 string'
15
+ equals $2 " hello`n world"
16
+ equals $3 ' System.Collections.ArrayList 1 System.Object[]'
17
+ equals $4 ' System.Object[]'
18
+ }
Original file line number Diff line number Diff line change
1
+ # ` Get-Content ` with ` OutVariable ` and ` ReadCount `
2
+
3
+ Using ` Get-Content ` with ` OutVariable ` and ` ReadCount ` may produce an
4
+ unexpected nested array in the result variable. Using or not using the
5
+ switch ` Raw ` makes no difference.
6
+
7
+ The workaround is not using ` ReadCount ` together with ` OutVariable ` .
8
+
9
+ ** Scripts**
10
+
11
+ - [ Test-1.ps1] ( Test-1.ps1 ) shows the issue without ` Raw `
12
+ - [ Test-2-Raw.ps1] ( Test-2-Raw.ps1 ) shows the same issue with ` Raw `
13
+
14
+ ---
15
+
16
+ - [ PowerShellTraps/issues/19] ( https://github.com/nightroman/PowerShellTraps/issues/19 )
Original file line number Diff line number Diff line change
1
+
2
+ # Make a text file with 2 lines
3
+ $null = New-Item z.txt - Value " hello`n world" - Force
4
+
5
+ # ## Test 1 (expected result)
6
+
7
+ # without -ReadCount
8
+ $null = Get-Content z.txt - OutVariable var1
9
+
10
+ # we get a list with 2 string items
11
+ " $ ( $var1.GetType ()) $ ( $var1.Count ) $ ( $var1 [0 ].GetType()) "
12
+
13
+ # and this gets expected `hello,world`
14
+ $var1 -join ' ,'
15
+
16
+ # ## Test 2 ("unexpected" result)
17
+
18
+ # with -ReadCount
19
+ $null = Get-Content z.txt - OutVariable var2 - ReadCount 0
20
+
21
+ # we get a list with 1 item, a nested array
22
+ " $ ( $var2.GetType ()) $ ( $var2.Count ) $ ( $var2 [0 ].GetType()) "
23
+
24
+ # and this gets "unexpected" `System.Object[]`
25
+ $var2 -join ' ,'
Original file line number Diff line number Diff line change
1
+
2
+ # Make a text file with 2 lines
3
+ $null = New-Item z.txt - Value " hello`n world" - Force
4
+
5
+ # ## Test 1 (expected result)
6
+
7
+ # with -Raw without -ReadCount
8
+ $null = Get-Content z.txt - OutVariable var1 - Raw
9
+
10
+ # we get a list with 1 string item
11
+ " $ ( $var1.GetType ()) $ ( $var1.Count ) $ ( $var1 [0 ].GetType()) "
12
+
13
+ # and this gets expected text "hello`nworld"
14
+ $var1 -join ' ,'
15
+
16
+ # ## Test 2 ("unexpected" result)
17
+
18
+ # with -Raw and -ReadCount
19
+ $null = Get-Content z.txt - OutVariable var2 - Raw - ReadCount 0
20
+
21
+ # we get a list with 1 item, a nested array
22
+ " $ ( $var2.GetType ()) $ ( $var2.Count ) $ ( $var2 [0 ].GetType()) "
23
+
24
+ # and this gets "unexpected" `System.Object[]`
25
+ $var2 -join ' ,'
Original file line number Diff line number Diff line change @@ -129,6 +129,8 @@ to their directory. See also [TESTS]. Some scripts require
129
129
- [ ` Get-ChildItem -LiteralPath -Recurse ` gets nothing for a directory with brackets] ( Cmdlets/Get-ChildItem/Directory-with-brackets )
130
130
- [ ` Get-ChildItem -Recurse ` and missing ` -Path ` unexpected search] ( Cmdlets/Get-ChildItem/Missing-path-and-Recurse )
131
131
- [ ` Get-ChildItem -LiteralPath -Recurse ` ignores ` -Include ` ] ( Cmdlets/Get-ChildItem/v5-LiteralPath-Recurse-ignores-Include )
132
+ - Get-Content
133
+ - [ ` Get-Content ` with ` OutVariable ` and ` ReadCount ` ] ( Cmdlets/Get-Content/OutVariable-and-ReadCount )
132
134
- Get-Event
133
135
- [ ` Get-Event ` erratic failures in v2-4] ( Cmdlets/Get-Event/v2-4-Erratic-failure )
134
136
- Get-Item
You can’t perform that action at this time.
0 commit comments