1

I have the following powershell script:

# C-Sharp source for taking our screenshot
$Source = @" 
using System;
using System.Linq;

namespace Testing
{
    public static class TestClass
    {
        public static void DoTest()
        {
             var myLinq = "hello world";
        }
    }
}
"@

$assem = @("System.Core")
$null = Add-Type -ReferencedAssemblies $assem -TypeDefinition $Source -Language Csharp

If I run this via powershell.exe powershell.exe C:\test.ps1 I get the following error:

Cannot add type. There were compilation errors. c:\Users\Administrator\AppData\Local\Temp\c9er05ba.0.cs(10) : The type or n
amespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

c:\Users\Administrator\AppData\Local\Temp\c9er05ba.0.cs(9) :         {

c:\Users\Administrator\AppData\Local\Temp\c9er05ba.0.cs(10) : >>>              var myLinq = "hello world";

c:\Users\Administrator\AppData\Local\Temp\c9er05ba.0.cs(11) :   }

However, if I copy and paste this script into PowerGUI it runs just fine.

The closest link I could find that was helpful was this one. So I tried changing my -language parameter to be csharpversion3, but now I get this:

Cannot add type. There were compilation errors. (0) : An assembly with the same identity 'System.Core, Version=3.5.0.0, Cul
ture=neutral, PublicKeyToken=b77a5c561934e089' has already been imported. Try removing one of the duplicate references.

(1) : using System;

Any ideas how I can get this script to run properly?

1
  • 1
    See Christian's comments on the config file you need for PowerShell to use the .Net v4 runtime. With those options (which I always use) your code compiles without the -Language set to CSharpVersion3, though you do need to use the variable myLinq to avoid a compiler warning. Oct 18, 2012 at 12:42

3 Answers 3

5

Stick with the -language csharpversion3 but drop the -ReferencedAssemblies $assem bit - that's what's giving you the duplicate import.

I also had to add -IgnoreWarnings to get your fragment to compile, but I guess that's because you're not posting the whole example.

In summary, the command:

Add-Type  -TypeDefinition $Source -Language CsharpVersion3 -IgnoreWarnings

will compile that code without error.

1
  • Thanks that's perfect. So it seems that specifying -language CSharpVersion3 causes it to automatically import those libraries, so I was causing duplicates. Makes perfect sense!
    – Kyle
    Oct 18, 2012 at 12:30
1

This work on Powershell V 2.0 using the powershell.exe.config

<?xml version="1.0"?>
<configuration>
      <startup useLegacyV2RuntimeActivationPolicy="true">
          <supportedRuntime version="v4.0.30319"/>
          <supportedRuntime version="v2.0.50727"/>
      </startup>
</configuration>  

Powershell code:

$Source = @" 
using System;
using System.Linq;

namespace Testing
{
    public static class TestClass
    {
        public static void DoTest()
        {
             var myLinq = "hello world";
             Console.WriteLine(myLinq.ToString());
    }
    }
}
"@

$null = Add-Type -ReferencedAssemblies system.core -TypeDefinition $Source -Language Csharp
0

I am not entirely sure Powershell understand the usage of 'var'. Replace your code as follows;

string myLinq = "hello world";

and see how you get on.

1
  • I'm positive that would work, but isn't Add-Type just trying to compile the C# code into a usable assembly? Shouldn't that be independent of powershell? I guess I would just like a better understanding of why that wouldn't work.
    – Kyle
    Oct 18, 2012 at 12:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.