I am trying to run C# code with custom .NET assembly in PowerShell, but cannot get it to work.
Here is my code:
# Adding custom assemblies to the PowerShell assemblies
Add-Type -Verbose -Path "C:\MyCustomAssembly.dll"
# Adding reference of standard .NET assemblies
$Refs = @("C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Xml.dll",
"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.Entity.dll",
"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Runtime.Serialization.dll")
$Source = @"
using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using MyCustomAssembly;
namespace CSharpCode
{
public class WebClient
{
public void ConsumePostMethod()
{ ... }
}
}
"@
Add-Type -ReferencedAssemblies $Refs -TypeDefinition $Source -Language CSharp -PassThru
[CSharpCode.WebClient]::ConsumePostMethod()
And this is the error that I am getting:
Add-Type : c:\Users\MyUser\AppData\Local\Temp\znllnhvt.0.cs(5) : The type or namespace name 'MyCustomAssembly' could not be found (are you missing a using directive
or an assembly reference?)
c:\Users\MyUser\AppData\Local\Temp\znllnhvt.0.cs(4) : using System.Runtime.Serialization;
c:\Users\MyUser\AppData\Local\Temp\znllnhvt.0.cs(5) : >>> using MyCustomAssembly;
c:\Users\MyUser\AppData\Local\Temp\znllnhvt.0.cs(6) :
At line:109 char:1
+ Add-Type -ReferencedAssemblies $Refs -TypeDefinition $Source -Language CSharp -P ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (c:\Users\MyUser...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:109 char:1
+ Add-Type -ReferencedAssemblies $Refs -TypeDefinition $Source -Language CSharp -P ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
Unable to find type [CSharpCode.WebClient]: make sure that the assembly containing this type is loaded.
At line:111 char:1
+ [CSharpCode.WebClient]::ConsumePostMethod()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (CSharpCode.WebClient:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
The weird thing is if I run this command below, it shows that MyCustomAssembly.dll is loaded in the session. Can anybody help?!
[System.AppDomain]::CurrentDomain.GetAssemblies() | Where {$_.Location} | ForEach {Split-Path -Leaf $_.Location} | Sort
Accessibility.dll
MyCustomAssembly.dll
Microsoft.Build.Framework.dll
Microsoft.CSharp.dll
...
Add-Type
does not use any currently loaded assemblies as reference implicitly. So, if you want to use any type from custom assembly in Add-Type
source code, then you should add this assembly to -ReferencedAssemblies
parameter explicitly.