通过WSManConnectionInfo WinRM';执行托管Powershell所需的IIS AppPool用户最低权限是多少;环回';C#中的会话?

通过WSManConnectionInfo WinRM';执行托管Powershell所需的IIS AppPool用户最低权限是多少;环回';C#中的会话?,c#,powershell,C#,Powershell,我有一个运行托管Powershell的web服务。前端有身份验证,它将凭证传递给C#中的某个模拟 应用程序以默认IIS AppPool用户身份运行。几乎所有的东西都是模仿出来的。 这非常有效-尽管我不得不调整.NET“alwaysFlowImpersonationPolicy”设置,正如我在这里发现的: 在回答上述问题时,也有人建议我可以使用WinRM作为执行Powershell的替代方法,而我当时没有选择这样做 出于一系列不同的原因,我想尝试取消C#模拟,并将所有权限提升移到WinRM。我相

我有一个运行托管Powershell的web服务。前端有身份验证,它将凭证传递给C#中的某个模拟

应用程序以默认IIS AppPool用户身份运行。几乎所有的东西都是模仿出来的。 这非常有效-尽管我不得不调整.NET“alwaysFlowImpersonationPolicy”设置,正如我在这里发现的:

在回答上述问题时,也有人建议我可以使用WinRM作为执行Powershell的替代方法,而我当时没有选择这样做

出于一系列不同的原因,我想尝试取消C#模拟,并将所有权限提升移到WinRM。我相信这是可能的,尽管我很高兴地承认这不是

如果我这样做:

        PSCredential psc = new PSCredential(user, password);
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://ahostname:5986/wsman"), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", psc);
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
        connectionInfo.SkipCNCheck = true;
        connectionInfo.SkipCACheck = true;
        using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
        {
            connectionInfo.EnableNetworkAccess = true;
            using (PowerShell powershell = PowerShell.Create())
            {

                string existingScript = @"C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1";
                string scriptFileName = Path.GetFileName(existingScript);
                string scriptText = ". " + existingScript + ";  " + command;

                if (command_logging == true)
                {
                    string logMessage = "Running command via PSH wrapper: " + scriptText;
                    Tools.logToFile(logMessage);
                }

                powershell.AddScript(scriptText);
                Collection<PSObject> results = new Collection<PSObject>();
                try
                {
                   results = powershell.Invoke();
                }
                catch (Exception ex)
                {

                    results.Add(new PSObject((object)ex.Message));

                }

                StringBuilder stringBuilder = new StringBuilder();
                foreach (PSObject obj in results)
                {
                   stringBuilder.AppendLine(obj.ToString());
                }
                string rawReturnValue = stringBuilder.ToString();
                string returnValue = rawReturnValue.Replace(System.Environment.NewLine, "");
                returnValue.Trim();
                powershell.Dispose();
                return returnValue;
            }
PSCredential psc=新的PSCredential(用户、密码);
WSManConnectionInfo connectionInfo=新的WSManConnectionInfo(新的Uri(“https://ahostname:5986/wsman"), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell“,psc);
connectionInfo.AuthenticationMechanism=AuthenticationMechanism.Kerberos;
connectionInfo.skipncheck=true;
connectionInfo.SkipCACheck=true;
使用(Runspace Runspace=RunspaceFactory.CreateRunspace(connectionInfo))
{
connectionInfo.EnableNetworkAccess=true;
使用(PowerShell PowerShell=PowerShell.Create())
{
字符串existingScript=@“C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell\u profile.ps1”;
string scriptFileName=Path.GetFileName(existingScript);
字符串scriptText=“.”+现有脚本+”;“+命令;
if(命令_日志==true)
{
string logMessage=“通过PSH包装器运行命令:”+scriptText;
工具日志文件(logMessage);
}
powershell.AddScript(脚本文本);
收集结果=新收集();
尝试
{
结果=powershell.Invoke();
}
捕获(例外情况除外)
{
添加(新的PSObject((object)ex.Message));
}
StringBuilder StringBuilder=新的StringBuilder();
foreach(结果中的PSObject对象)
{
stringBuilder.AppendLine(obj.ToString());
}
string rawReturnValue=stringBuilder.ToString();
字符串returnValue=rawReturnValue.Replace(System.Environment.NewLine,“”);
returnValue.Trim();
powershell.Dispose();
返回值;
}
我有一套奇怪的行为:

如果我的AppPool在默认凭据(ApplicationPoolIdentity)下运行,99%的Powershell函数将不会返回任何有意义的内容

上面代码段中的PSObject“results”对象返回0。完全没有错误,几乎就像它没有执行一样

例如,某些函数“Get Process”将返回良好结果

我想也许我的方法学不会转移到使用WSManConnectionInfo(即dot寻源配置文件等),但是如果我强制应用程序在管理员帐户下运行,一切都会完美运行(如此一来,我就可以无缝地交换新的和旧的Powershell“包装器”方法,运行一系列其他测试…)

我测试的两个“默认”cmdlet:

“获取模块”。它位于Microsoft.PowerShell.Core中,在管理员帐户下工作,但在ApplicationPoolIdentity下不工作

“获取进程”。Microsoft.PowerShell.Management可在ApplicationPoolIdentity和管理员帐户下工作

然后,我将“Get Process”包装在一个测试函数中,将其放入我的$profile中,并尝试调用它。当AppPool在管理员帐户下运行,而不是在ApplicationPoolIdentity下运行时,这会起作用

几乎可以肯定是权限问题?有人能告诉我正确的方向吗