How to use AppDomainInitializer in Powershell

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 1 year, 8 months ago.

  • Author
  • #43858

    René Vajkay

    Hi guys,

    I am trying to create an AppDomain in a Powershell script. I implemented the exact same in C# and it seems to be working fine. However, the Powershell version always fails with an exception. The script code is:

    function Execute($arguments)
        Write-Host "Arguments: " $arguments[0] $arguments[1] $arguments[2]
    function Main
        $appDomain = $null
            $appDomainSetup = New-Object -TypeName System.AppDomainSetup
            $appDomainSetup.AppDomainInitializer = ${function:Execute}
            $appDomainSetup.AppDomainInitializerArguments = @("Test1", "Test2", "Test3")
            $appDomain = [AppDomain]::CreateDomain("TestDomain", $null, $appDomainSetup)
            If ($appDomain -ne $null)

    And the exception is: Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at System.AppDomain.nCreateDomain(String friendlyName, AppDomainSetup setup, Evidence providedSecurityInfo, Evidence creatorsSecurityInfo, IntPtr parentSecur ityDescriptor) ...

    We figured out that Powershell is creating a dynamic method from the delegate in AppDomainSetup. But I don't know to change this behavior or any other way how I can use AppDomainInitializer in Powershell?

    This question has been originally asked on

  • #44694

    Don Jones

    Could you just use embedded C#?

You must be logged in to reply to this topic.