2

I have a powershell script that is called by chef that works on windows 2012r2, but fails on windows 2016 (powershell 5.1.14393.1884) .

I paste into the powershell window the following 2 commands

$letters = New-Object System.Collections.ArrayList
$letters.AddRange( ('F','G') );

And I get this error

You cannot call a method on a null-valued expression.

Looking at the output, it seems that the order of the commands is reversed. AddRange is before New-Object.

enter image description here

Things I've tried

Add ; to the end of each command

$letters = New-Object System.Collections.ArrayList;
$letters.AddRange( ('F','G') );

Call ArrayList with ($null) in case the constructor requires it

$letters = New-Object System.Collections.ArrayList($null);

and

$letters = New-Object System.Collections.ArrayList(,$null);

Cast AddRange to [void] as recommended here

$letters = New-Object System.Collections.ArrayList;
[void] $letters.AddRange( ('F','G') );

Why does powershell 5.1 run these commands out of order? Is this a bug?

Update

To clarify, the commands run out of order when I type/paste them into a powershell window. I get the exact same error when chef runs the commands. Since chef is 'shelling-out' it is effectively doing typing for me.

Update

There are actually 2 issues going on. andyb identified the known bug where ctrl +v behaves differently than right clicking to paste in windows 10/windows server 2016

9
  • 2
    So they are being pasted in the wrong order? Commented Nov 19, 2017 at 17:40
  • 2
    Any particular reason why you're using an ArrayList? PowerShell has excellent native array support. You could just write that as $letters = @('F', 'G') and get an array with those letters in it. Beyond that, can you include the script in its entirety or at least a MCVE that exhibits the behavior you're experiencing? Commented Nov 19, 2017 at 17:51
  • It ran without error on 5.1.14393.1770 (Windows 10) Commented Nov 19, 2017 at 23:19
  • 1
    Looks to me like the problem is with the pasting. They appear in the wrong order in the image. Commented Nov 19, 2017 at 23:21
  • @AustinFrench Updated question. Yes pasting/typing the commands does flip the order. Chef gets the same error when it 'types' the command for me. Commented Nov 20, 2017 at 0:02

1 Answer 1

5

This does appear to be a known issue with the PowerShell PSReadLine line editing function that has been included in Windows 10.

When using the right-mouse button to paste content that has UNIX line endings i.e. LF instead of CRLF, PSReadLine interprets the LF as 'CTRL-ENTER', which translates to Insert Line Above.

Pasting with CTRL-V, works correctly, even with LF formatted text.

This does not directly explain the chef behavior, unless chef works by sending mouse-click events to the PowerShell console host, and it is also sending LF formatted text. If this IS the case, then the fix would be to modify the chef behaviour, so that it sends CRLF instead of just LF.

References:

PSReadLine: https://github.com/lzybkr/PSReadLine

LF Paste Bug: https://github.com/lzybkr/PSReadLine/issues/579

Sign up to request clarification or add additional context in comments.

1 Comment

There are 2 separate issues going on. You identified the first one. The chef error turns out to be a slightly different error which I will continue to research.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.