DotNetFiddle 抛出 "System.Security.SecurityException"
DotNetFiddle throws "System.Security.SecurityException"
由于目前没有 PSFiddle 可用,我想知道是否可以通过将 PS 代码包装在 C# 中来使用 DotNetFiddle 来达到此目的。
我使用了以下代码:
using System;
using System.Collections.ObjectModel;
using System.Management.Automation; //if run on your machine, first ensure Windows SDK is installed (https://www.microsoft.com/en-us/download/details.aspx?id=11310)
public class Program
{
public static void Main()
{
string script = @"
param([string]$name)
""hello $name"" #NB:Double quotes have to be escaped; otherwise all's good
";
RunPSScript(script);
}
private static void RunPSScript(string script) {
using (PowerShell ps = PowerShell.Create())
{
ps.AddScript(script);
ps.AddParameter("name", "Player One");
Collection<PSObject> psOutput = ps.Invoke();
foreach(PSObject item in psOutput) {
if(item != null) {
Console.WriteLine(item.BaseObject.ToString());
}
}
}
}
}
在 DotNetFiddle 中 运行 时,这会引发 System.Security.SecurityException
错误。
可以在这里找到:https://dotnetfiddle.net/B4JLU0
代码在我的本地计算机上 运行 时有效,所以我认为问题是由于 DotNetFiddle.
上的安全性引起的
问题
是否有解决方法来允许这个/避免异常;还是这根本不可能?
完整的错误消息如下:
Run-time exception (line 19): The type initializer for 'System.Management.Automation.Runspaces.RunspaceFactory' threw an exception.
Stack Trace:
[System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
[System.TypeInitializationException: The type initializer for 'System.Management.Automation.Runspaces.RunspaceFactory' threw an exception.]
at Program.RunPSScript(String script): line 19
at Program.Main(): line 14
PowerShell 必须 运行 在用户上下文中,当在 System.Security.Permissions.SecurityPermission
上抛出异常时,很可能意味着当前用户上下文没有必要的权限或信任运行 PowerShell,并且没有创建 运行 空间并进行一些模拟,我想它正在尝试 运行 作为 web 服务用户,这很可能具有最小权限。
我只是在做假设,您可能需要联系 Entech Solutions 的好人以获得明确的答案,但希望这有助于回答您的问题。
由于目前没有 PSFiddle 可用,我想知道是否可以通过将 PS 代码包装在 C# 中来使用 DotNetFiddle 来达到此目的。
我使用了以下代码:
using System;
using System.Collections.ObjectModel;
using System.Management.Automation; //if run on your machine, first ensure Windows SDK is installed (https://www.microsoft.com/en-us/download/details.aspx?id=11310)
public class Program
{
public static void Main()
{
string script = @"
param([string]$name)
""hello $name"" #NB:Double quotes have to be escaped; otherwise all's good
";
RunPSScript(script);
}
private static void RunPSScript(string script) {
using (PowerShell ps = PowerShell.Create())
{
ps.AddScript(script);
ps.AddParameter("name", "Player One");
Collection<PSObject> psOutput = ps.Invoke();
foreach(PSObject item in psOutput) {
if(item != null) {
Console.WriteLine(item.BaseObject.ToString());
}
}
}
}
}
在 DotNetFiddle 中 运行 时,这会引发 System.Security.SecurityException
错误。
可以在这里找到:https://dotnetfiddle.net/B4JLU0
代码在我的本地计算机上 运行 时有效,所以我认为问题是由于 DotNetFiddle.
上的安全性引起的问题
是否有解决方法来允许这个/避免异常;还是这根本不可能?
完整的错误消息如下:
Run-time exception (line 19): The type initializer for 'System.Management.Automation.Runspaces.RunspaceFactory' threw an exception.
Stack Trace:
[System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
[System.TypeInitializationException: The type initializer for 'System.Management.Automation.Runspaces.RunspaceFactory' threw an exception.] at Program.RunPSScript(String script): line 19 at Program.Main(): line 14
PowerShell 必须 运行 在用户上下文中,当在 System.Security.Permissions.SecurityPermission
上抛出异常时,很可能意味着当前用户上下文没有必要的权限或信任运行 PowerShell,并且没有创建 运行 空间并进行一些模拟,我想它正在尝试 运行 作为 web 服务用户,这很可能具有最小权限。
我只是在做假设,您可能需要联系 Entech Solutions 的好人以获得明确的答案,但希望这有助于回答您的问题。