Calling a method of custom C# dll from powershell script

3

I'm new to PowerShell. I'm trying to call a method of my custom C# dll. I failed due to multiple reasons.

Here is my code:

[System.Reflection.assembly]::LoadFile("E:\Mulukutla\Migration.dll")

$MyClass = New-Object DataMigration

$MyClass.MigrateData("$from\$name","$to\$name")

C# class library I used is build in 4.0 and powershell v2.0

Exception calling "LoadFile" with "1" argument(s): "This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded. (Exception from HRESULT: 0x8013101B)" At E:\Mulukutla\myCodev1.ps1:3 char:39 + [System.Reflection.assembly]::LoadFile <<<< ("E:\Mulukutla\Migration.dll") + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

New-Object : Cannot find type [DataMigration]: make sure the assembly containing this type is loaded. At E:\mulukutla\myCodev1.ps1:4 char:21 + $MyClass= New-Object <<<< DataMigration + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException

+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

Please help me by correcting, what is the mistake I'm doing?

powershell
asked on Stack Overflow Nov 12, 2012 by user1817061

1 Answer

2

invoke powershell from cmd by :

set COMPLUS_Version=v4.0.30319
powershell

or set the content of 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config' as follows(you may need to create this file if not exist):

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0" />    
    </startup> 
</configuration>
answered on Stack Overflow Nov 12, 2012 by Jackie

User contributions licensed under CC BY-SA 3.0