C# DotNetFiddle抛出;System.Security.SecurityException“;

C# DotNetFiddle抛出;System.Security.SecurityException“;,c#,powershell,securityexception,C#,Powershell,Securityexception,由于目前没有可用的PSFiddle,我想知道是否可以通过用C#包装PS代码来使用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-u

由于目前没有可用的PSFiddle,我想知道是否可以通过用C#包装PS代码来使用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());
                }
            }
        }
    }
}
使用系统;
使用System.Collections.ObjectModel;
使用系统、管理、自动化//如果在您的计算机上运行,请首先确保安装了Windows SDK(https://www.microsoft.com/en-us/download/details.aspx?id=11310)
公共课程
{
公共静态void Main()
{
字符串脚本=@“
参数([string]$name)
“hello$name”“#注意:必须转义双引号;否则一切都好
"; 
RunPSScript(脚本);
}
私有静态void RunPSScript(字符串脚本){
使用(PowerShell ps=PowerShell.Create())
{
ps.AddScript(脚本);
ps.AddParameter(“名称”、“玩家一”);
集合psOutput=ps.Invoke();
foreach(psOutput中的PSObject项){
如果(项!=null){
Console.WriteLine(item.BaseObject.ToString());
}
}
}
}
}
在DotNetFiddle中运行时,会抛出
System.Security.SecurityException
错误

这可以在这里找到:

代码在本地机器上运行时有效,因此我认为问题是由于DotNetFiddle上的安全性造成的

问题


是否有一个变通方法来允许此/避免异常;或者这根本不可能吗?

完整的错误消息如下:

运行时异常(第19行):“System.Management.Automation.Runspaces.RunspaceFactory”的类型初始值设定项引发异常

堆栈跟踪:

[System.Security.SecurityException:请求'System.Security.Permissions.SecurityPermission,mscorlib,版本=4.0.0.0,区域性=neutral,PublicKeyToken=b77a5c561934e089'类型的权限失败。]

[System.TypeInitializationException:“System.Management.Automation.Runspaces.RunspaceFactory”的类型初始值设定项引发异常。] 在Program.RunPSScript(字符串脚本):第19行 在Program.Main()处:第14行

PowerShell必须在用户上下文中运行,并且当在
System.Security.Permissions.SecurityPermission
上引发异常时,很可能意味着当前用户上下文没有运行PowerShell所需的权限或信任,并且没有创建运行空间和进行模拟,我猜想它正在尝试以webservice用户的身份运行,而该用户很可能拥有最低权限


我只是在做假设,您可能需要联系Entech Solutions的优秀人员以获得明确的答案,但希望这有助于回答您的问题。

完整的错误消息如下:

运行时异常(第19行):“System.Management.Automation.Runspaces.RunspaceFactory”的类型初始值设定项引发异常

堆栈跟踪:

[System.Security.SecurityException:请求'System.Security.Permissions.SecurityPermission,mscorlib,版本=4.0.0.0,区域性=neutral,PublicKeyToken=b77a5c561934e089'类型的权限失败。]

[System.TypeInitializationException:“System.Management.Automation.Runspaces.RunspaceFactory”的类型初始值设定项引发异常。] 在Program.RunPSScript(字符串脚本):第19行 在Program.Main()处:第14行

PowerShell必须在用户上下文中运行,并且当在
System.Security.Permissions.SecurityPermission
上引发异常时,很可能意味着当前用户上下文没有运行PowerShell所需的权限或信任,并且没有创建运行空间和进行模拟,我猜想它正在尝试以webservice用户的身份运行,而该用户很可能拥有最低权限


我只是在做假设,您可能需要联系Entech Solutions的好员工以获得明确的答案,但希望这有助于回答您的问题。

同意,这听起来像是一个信任级别的问题,我不指望能够解决它,这听起来像是一个信任级别的问题,在选择了
RunspaceFactory
类型初始值设定项之后,我不能指望能够解决这个问题。我可以说,异常是由于P/Invoke调用而发生的,它在
SecurityPermission(SecurityPermissionFlag.UnmanagedCode)
权限上生成了完整的
Demand
。由于它是非常敏感的权限,因此不太可能授予沙盒,该服务使用沙盒调用您的代码。在选择了
RunspaceFactory
type初始值设定项后,我可以说异常是由于P/Invoke调用而发生的,它在
SecurityPermission(SecurityPermissionFlag.UnmanagedCode)上生成完整的
Demand
权限。因为它是非常敏感的权限,所以不太可能将其授予沙盒,该服务使用它来调用您的代码。