1

I am able to get a C# code sample to run within a Powershell v2.0 script as follows:

$Source = @"
using System;

namespace CSharpInPowershell
{
    public static class Sample
    {
        public static void TryDataTable()
        {
            Console.WriteLine("Hello World");
            Console.ReadLine();
        }
    }
}
"@

Add-Type -TypeDefinition $Source -Language CSharp

[CSharpInPowershell.Sample]::TryDataTable()

However, I am getting an error when trying to add a data table:

Add-Type -AssemblyName System.Data

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

namespace CSharpInPowershell
{
    public static class Sample
    {
        public static void TryDataTable()
        {
            Console.WriteLine("Hello World");
            DataTable table = new DataTable();
            Console.ReadLine();
        }
    }
}
"@

Add-Type -TypeDefinition $Source -Language CSharp

[CSharpInPowershell.Sample]::TryDataTable()  

The error I get is the following:

Add-Type : c:\Users(userid)\AppData\Local\Temp\qbefurwr.0.cs(2) : The type or namespace name 'Data' does not exist in the namespace 'System' (are you missing an assembly reference?) c:\Users(userid)\AppData\Local\Temp\qbefurwr.0.cs(1) : using System; c:\Users(userid)\AppData\Local\Temp\qbefurwr.0.cs(2) : >>> using System.Data; c:\Users(userid)\AppData\Local\Temp\qbefurwr.0.cs(3) : namespace CSharpInPowershell At line:1 char:9 + Add-Type <<<< -TypeDefinition $Source -Language CSharp + CategoryInfo : InvalidData: (c:\Users(userid)...bly reference?):CompilerError) [Add-Type], Exception + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : Cannot add type. There were compilation errors. At line:1 char:9 + Add-Type <<<< -TypeDefinition $Source -Language CSharp + CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException + FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

As you may see, I tried to add a reference using Add-Type -AssemblyName System.Data. My goal is to be able to use C# code samples within a Powershell script. I know I could re-write this all in Powershell, but I'm trying to get this type of scripting to work.

How can I get the assembly reference for System.Data recognized within the C# code?

UPDATE: Thanks to @SomeShinyObject, I have the following working script:

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

namespace CSharpInPowershell
{
    public static class Sample
    {
        public static void TryDataTable()
        {
            Console.WriteLine("Hello World");
            DataTable table = new DataTable();
            Console.ReadLine();
        }
    }
}
"@

Add-Type -TypeDefinition $Source -Language CSharp `
         -ReferencedAssemblies System.Data, System.XML

[CSharpInPowershell.Sample]::TryDataTable()

1 Answer 1

2

You'll need to use the ReferencedAssemblies parameter.

#When I tested, System.XML also needed to be added for it to work so I included it
Add-Type -TypeDefinition $Source -Language CSharpVersion3 -ReferencedAssemblies System.Data, System.XML
Sign up to request clarification or add additional context in comments.

Comments

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.