PowerShell / vscode-powershell

Provides PowerShell language and debugging support for Visual Studio Code
https://marketplace.visualstudio.com/items/ms-vscode.PowerShell
MIT License
1.69k stars 481 forks source link

Integrated PowerShell -- Nested ForEach-Object Failing to Concatenate Strings #1846

Open BinaryInk opened 5 years ago

BinaryInk commented 5 years ago

Issue Type: Bug

Simply put, it's not putting in $currDept (from the first ForEach-Object loop) in the string as expected, except for the last pass. Code and Outputs below.

CODE---------------------

$SearchBaseSuffix = 'ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx'

$Departments = 'Alpha
Beta
Gamma
Zulu'.Split("`n")

$OS = 'Windows 7','Windows 10'
$FormFactors = 'Convertibles','Laptops','Desktops'

$Departments | % {
    $currDept = $_
    Write-Host $currDept

    $OS | % {
        $currOS = $_

        $FormFactors | % {
            $currForm = $_
            write-host "ou=$currDept,ou=$currForm,ou=$currOS,$SearchBaseSuffix"
            # Get-ADComputer -Filter * -Prop 'Description' -SearchBase "ou=$$currDept,ou=$currForm,ou=$currOS,$SearchBaseSuffix" | ft
        }

    }

}

OUTPUTS------------

Integrated Powershell Output (Incorrect):

Alpha
,ou=Convertibles,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Laptops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Desktops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Convertibles,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Laptops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Desktops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
Beta
,ou=Convertibles,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Laptops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Desktops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Convertibles,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Laptops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Desktops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
Gamma
,ou=Convertibles,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Laptops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Desktops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Convertibles,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Laptops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
,ou=Desktops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
Zulu
ou=Zulu,ou=Convertibles,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Zulu,ou=Laptops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Zulu,ou=Desktops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Zulu,ou=Convertibles,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Zulu,ou=Laptops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Zulu,ou=Desktops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx

Expected Output (Tested, Confirmed in a 'regular' PowerShell session within Code and in a separate PowerShell terminal):

Alpha
ou=Alpha,ou=Convertibles,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Alpha,ou=Laptops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Alpha,ou=Desktops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Alpha,ou=Convertibles,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Alpha,ou=Laptops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Alpha,ou=Desktops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
Beta
ou=Beta,ou=Convertibles,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Beta,ou=Laptops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Beta,ou=Desktops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Beta,ou=Convertibles,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Beta,ou=Laptops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Beta,ou=Desktops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
Gamma
ou=Gamma,ou=Convertibles,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Gamma,ou=Laptops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Gamma,ou=Desktops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Gamma,ou=Convertibles,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Gamma,ou=Laptops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Gamma,ou=Desktops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
Zulu
ou=Zulu,ou=Convertibles,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Zulu,ou=Laptops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Zulu,ou=Desktops,ou=Windows 7,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Zulu,ou=Convertibles,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Zulu,ou=Laptops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx
ou=Zulu,ou=Desktops,ou=Windows 10,ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx

Extension version: 1.12.0 VS Code version: Code 1.32.3 (a3db5be9b5c6ba46bb7555ec5d60178ecc2eaae4, 2019-03-14T23:43:35.476Z) OS version: Windows_NT x64 10.0.17763

System Info
|Item|Value| |---|---| |CPUs|Intel(R) Core(TM) i5-4670 CPU @ 3.40GHz (4 x 3392)| |GPU Status|2d_canvas: enabled
checker_imaging: disabled_off
flash_3d: enabled
flash_stage3d: enabled
flash_stage3d_baseline: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
native_gpu_memory_buffers: disabled_software
rasterization: enabled
surface_synchronization: enabled_on
video_decode: enabled
webgl: enabled
webgl2: enabled| |Memory (System)|7.91GB (1.91GB free)| |Process Argv|| |Screen Reader|no| |VM|0%|
rjmholt commented 5 years ago

Thanks for opening an issue @BinaryInk.

Can you please provide your environment information. The easiest way is by pasting this into your Integrated Console:

& {"### VSCode version: $(code -v)"; "`n### VSCode extensions:`n$(code --list-extensions --show-versions | Out-String)"; "`n### PSES version: $($pseditor.EditorServicesVersion)"; "`n### PowerShell version:`n$($PSVersionTable | Out-String)"}
rjmholt commented 5 years ago

Can you also tell us where the script is located (is it on the file system, a new folder, or put directly into the terminal) and how you executed it (did you press F5 for example?)

rjmholt commented 5 years ago

Ah I think I have a way to reproduce this behaviour:

If you press the up key, this gets put into PSReadLine properly and it all works.

F5 seems to do this as well...

BinaryInk commented 5 years ago

Nailed it!

Sorry, sent that out before I left work for the day. I was using the highlight and “run selection” (F8) method as you found out, didn’t think to mention it in my report, my bad!

I was just using VSCode as a scratch pad, so no saved file, just in memory.

I can provide environment information tomorrow if it is still needed.

From: Robert Holt notifications@github.com Sent: Wednesday, April 3, 2019 8:00 PM To: PowerShell/vscode-powershell vscode-powershell@noreply.github.com Cc: BinaryInk brady.greenwood@live.com; Mention mention@noreply.github.com Subject: Re: [PowerShell/vscode-powershell] Integrated PowerShell -- Nested ForEach-Object Failing to Concatenate Strings (#1846)

Ah I think I have a way to reproduce this behaviour:

If you press the up key, this gets put into PSReadLine properly and it all works.

F5 seems to do this as well...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/PowerShell/vscode-powershell/issues/1846#issuecomment-479710436, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ALft_tUKycyXV_wu0zeU3GAOCpAACLjGks5vdU6QgaJpZM4cbms-.

msftrncs commented 5 years ago

If F8 just ran the code though PSReadLine everything would work right? :)

I found this because I was looking to see if anyone suggested that yet because that would then cause the 'selected' code to be highlighted in the terminal.

msftrncs commented 5 years ago

Interesting, 1 change makes it work correctly, instead of the string split() method, use the unary -split operator:

$SearchBaseSuffix = 'ou=Workstations,ou=Machines in Production,dc=xxxx,dc=xxx'

$Departments = -split 'Alpha
Beta
Gamma
Zulu'

$OS = 'Windows 7','Windows 10'
$FormFactors = 'Convertibles','Laptops','Desktops'

$Departments | % {
    $currDept = $_
    Write-Host $currDept

    $OS | % {
        $currOS = $_

        $FormFactors | % {
            $currForm = $_
            write-host "ou=$currDept,ou=$currForm,ou=$currOS,$SearchBaseSuffix"
            # Get-ADComputer -Filter * -Prop 'Description' -SearchBase "ou=$$currDept,ou=$currForm,ou=$currOS,$SearchBaseSuffix" | ft
        }

    }

}
msftrncs commented 5 years ago

Ah, its caused because, at least on Windows, F8 copies `r`n on each line, but when PSReadLine replays it, the line ends are just `n. The string Split() method with an operand of "`n" is passing the extra `r through.

msftrncs commented 5 years ago

Realizing that its an issue of `r`n line endings. this can be reproduced in an actual full powershell session, storing the sample script in a normal Windows file with CRLF endings.

So this is not an issue of the PowerShell extension, but a shortfall in the script to be able to handle different conditions. The best option is probably something along the lines of building the array with separated string elements, or using the -split operator with '\r?\n' or '\s+' for the delimiter pattern.

msftrncs commented 5 years ago

… or add the .trim() method to the $Departments reference ...

rjmholt commented 5 years ago

That's a really great investigation, thanks @msftrncs!