Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
BinaryInk opened this issue Apr 3, 2019 · 10 comments

Comments

@BinaryInk
Copy link

BinaryInk commented Apr 3, 2019

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
Copy link
Contributor

rjmholt commented Apr 4, 2019

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
Copy link
Contributor

rjmholt commented Apr 4, 2019

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
Copy link
Contributor

rjmholt commented Apr 4, 2019

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

  • Copy program into editor and save
  • Highlight all lines
  • Press F8

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

F5 seems to do this as well...

@BinaryInk
Copy link
Author

BinaryInk commented Apr 4, 2019 via email

@SydneyhSmith SydneyhSmith added Triage and removed Triage labels Apr 4, 2019
@msftrncs
Copy link

msftrncs commented May 1, 2019

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
Copy link

msftrncs commented May 2, 2019

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
Copy link

msftrncs commented May 2, 2019

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
Copy link

msftrncs commented May 2, 2019

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
Copy link

msftrncs commented May 2, 2019

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

@rjmholt
Copy link
Contributor

rjmholt commented May 2, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants