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
Try
{
$appDomainSetup = New-Object -TypeName System.AppDomainSetup
$appDomainSetup.AppDomainInitializer = ${function:Execute}
$appDomainSetup.AppDomainInitializerArguments = @("Test1", "Test2", "Test3")
$appDomain = [AppDomain]::CreateDomain("TestDomain", $null, $appDomainSetup)
}
Finally
{
If ($appDomain -ne $null)
{
[AppDomain]::Unload($appDomain)
}
}
}
Main
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 .net - How to use AppDomainInitializer in Powershell - Stack Overflow.