6

So.. very odd problem.

Using VS2015 and .net 4.52

I developed this C# powershell code, it is running a script and catches the output. like this:

        using (PowerShell powerShellInstance = PowerShell.Create())
        {
            powerShellInstance.AddScript(scriptContents);

            Collection<PSObject> PSOutput = powerShellInstance.Invoke(); 

            if (powerShellInstance.Streams.Information.Count > 0)
            {

                    foreach (var item in powerShellInstance.Streams.Information)
                    {
                        //do something with info
                    }


                }

            }
        }

Compiles and runs (on a Windows 10 pro machine), no problems.

Until I got a new machine (surface pro 4, so also windows 10 pro) and tried to compile the code, I get this error:

'PSDataStreams' does not contain a definition for 'Information' and no extension method 'Information' accepting a first argument of type 'PSDataStreams' could be found (are you missing a using directive or an assembly reference?)

This is all TFS based, so I'm sure it is the same code.

If I goto definition on the two machines the problem becomes obvious:

enter image description here

So, I commented out the not compiling code and ran it, to see what was happening runtime:

enter image description here

So the property IS there.. Anybody got a good explanation for this?

BTW: the msdn documentation does not mention an Information property..

Flores
  • 8,226
  • 5
  • 49
  • 81
  • Did you have Powershell 5 Preview installed on the old system, but not the new one? Or perhaps different versions of it between the two? – alroc Dec 20 '15 at 12:01
  • No, this a clean win 10 machine, with only studio installed – Flores Dec 20 '15 at 19:22

1 Answers1

9

You might be referencing the wrong version of the System.Management.Automation.dll assembly on the Surface Pro.

On my Windows 10 Pro installation (upgraded from Windows 8.1), I have two versions, one in each GAC:

2 ps versions

The one selected in the picture is the "old" one, lives in the old GAC (C:\windows\assembly) and identifies itself with the File Version 6.1.7600.16385.

The other one (which contains the correct version of PSDataStreams and exposes the Information stream) lives in C:\Windows\Microsoft.NET\assembly and has File Version 10.0.10240.16384 as of writing.

Community
  • 1
  • 1
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Huh.. I'm referencing C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll Are there three? – Flores Dec 20 '15 at 19:12
  • But as you can see in the first screenshot I'm referencing exactly the same file.. that is.. the same location.. – Flores Dec 20 '15 at 19:16
  • Weird stuff.. There are indeed three, the two you mention and the one I used. They are all different. File sizes ~1mb, ~3mb and ~ 6mb. – Flores Dec 20 '15 at 19:18
  • Btw.. since when are there 2 GAC's? – Flores Dec 20 '15 at 19:19
  • The assembly version of all are the same: 3.0.0.0. but the file version is different.. I guess that is the source of the problem – Flores Dec 20 '15 at 19:20
  • 2
    @Flores 2 GAC's since release of CLR version 4.0. The one from the PowerShell 3.0 SDK (the one in Referenced Assemblies) won't work, the Information stream is introduced in PowerShell 5.0 – Mathias R. Jessen Dec 20 '15 at 19:32