C#:新的SelfSignedCertificate powershell cmdlet无法使用新的应用程序域工作

C#:新的SelfSignedCertificate powershell cmdlet无法使用新的应用程序域工作,powershell,Powershell,我试图通过创建新的appdomain来执行下面的代码,但它似乎不起作用。未在certlm.msc中创建新条目。请参阅随附的屏幕截图。[CertLM][1] 如果我使用当前域,那么它将按预期工作 我正在使用Windows10。还试图传递CurrentDomain证据和其他细节,但没有成功。新域和当前域的属性在我看来很好。还使用了process explorer,在这两种情况下似乎都正确加载了Microsoft.CertificateServices.PKIClient.cmdlet.dll **E

我试图通过创建新的appdomain来执行下面的代码,但它似乎不起作用。未在certlm.msc中创建新条目。请参阅随附的屏幕截图。[CertLM][1]

如果我使用当前域,那么它将按预期工作

我正在使用Windows10。还试图传递CurrentDomain证据和其他细节,但没有成功。新域和当前域的属性在我看来很好。还使用了process explorer,在这两种情况下似乎都正确加载了Microsoft.CertificateServices.PKIClient.cmdlet.dll

**EXE code:**

 class Program
    {
        static void Main(string[] args)
        {
            MessageBox.Show("Running powershell script");            

            InitialSessionState iss = InitialSessionState.CreateDefault();       

            using (System.Management.Automation.Runspaces.Runspace space = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(iss))
            {
                space.Open();

                using (System.Management.Automation.PowerShell shell = System.Management.Automation.PowerShell.Create())
                {
                    string sScriptContent = "New-SelfSignedCertificate -DnsName localhost -CertStoreLocation \"cert:\\LocalMachine\\My\"";

                    shell.Runspace = space;    

                    shell.AddScript(sScriptContent, true);

                   IAsyncResult result = shell.BeginInvoke();

                   WaitHandle[] wait = { result.AsyncWaitHandle };
                  WaitHandle.WaitAny(wait);
                }

                space.Close();
            }
        }

    }

**Sample application that calls above EXE**

public class Class1
    {
        static void Main(string[] args)
        {
            /* Not Working.*/
            System.AppDomain newDomain = System.AppDomain.CreateDomain("NewApplicationDomain");
            newDomain.ExecuteAssembly("PowerShellCertificateTest.exe");

            //Unload the application domain:
            System.AppDomain.Unload(newDomain);

            // Working using current domain...
            System.AppDomain.CurrentDomain.ExecuteAssembly("PowerShellCertificateTest.exe");

           Console.ReadLine();
        }

    }


  [1]: https://i.stack.imgur.com/KzCz0.jpg

设置下面的配置选项useLegacyV2RuntimeActivationPolicy解决了该问题。但仍在试图弄清楚CurrentDomain如何使用此配置设置

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>