How to use AppDomainInitializer in Powershell

Welcome Forums General PowerShell Q&A How to use AppDomainInitializer in Powershell

This topic contains 1 reply, has 2 voices, and was last updated by

3 years, 1 month ago.

  • Author
  • #43858

    Topics: 1
    Replies: 0
    Points: 0
    Rank: Member

    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

    Topics: 13
    Replies: 4872
    Points: 1,843
    Helping HandTeam Member
    Rank: Community Hero

    Could you just use embedded C#?

The topic ‘How to use AppDomainInitializer in Powershell’ is closed to new replies.