ASP.NET如何启动PowerShell脚本在IIS 7.5上运行?

3

我的开发机器上一切正常,但是如果部署到IIS上,进程就无法启动。我正在通过以下方式启动powershell脚本:

    private void RunScript()
    {
        Process process = null;
        try
        {
            int timeout = 1800000;
            var startInfo = new ProcessStartInfo
            {
                FileName = @"powershell.exe",    
                Arguments = string.Format("{0} {1}", "\path\toscript", "myParam"),
                UseShellExecute = false,
                CreateNoWindow = true
            };                   

            process = Process.Start(startInfo);
            process.WaitForExit(timeout);
        }
        finally
        {
            if (!process.HasExited)
            {
                if (process.Responding)
                    process.CloseMainWindow();
                else
                    process.Kill();
            }
            if (process != null)
            {
                process.Close();
                process.Dispose();
            }
        }  
    }

以下是此应用程序池的配置信息:

进程模型
->身份验证 = 域用户,该用户是域管理员。
->加载用户配置文件 = True

Web 应用程序
身份验证为 Windows

还需要配置什么才能运行该进程?

3个回答

3
正如Start-Automating建议的那样,最终我做了这个:

            using (Runspace runSpace = RunspaceFactory.CreateRunspace())
        {
            try
            {
                runSpace.Open();
                RunspaceInvoke scriptInvoker = new RunspaceInvoke(runSpace);
                scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted"); 
                using (Pipeline pipeLine = runSpace.CreatePipeline())
                {                        
                    var myCommand = new Command(scriptPath);                           
                    var myParam1 = new CommandParameter("-paramName", "someValue"); 
                    myCommand.Parameters.Add(myParam1);
                    pipeLine.Commands.Add(myCommand);       
                    pipeLine.Commands.Add("Out-String");
                    Collection<PSObject> returnObjects = pipeLine.Invoke();
                    runSpace.Close();

                    return returnObjects;                                              
                }
            }
            finally 
            {
                runSpace.Close();
            }
        }

在IIS服务器上,我执行了以下PowerShell命令“Set-ExecutionPolicy RemoteSigned”。

1

将PowerShell APIs嵌入调用.exe要好得多。

这是一个旧链接,可以为每个用户在ASP.NET中嵌入一个PowerShell运行空间:

http://powershellpipeworks.com/


0
检查存储 powershell.exe 的文件系统的权限。
此外,检查事件查看器中的Security Log以查找身份验证错误和访问违规。

正在使用的应用程序池域帐户也是计算机上管理员组的成员。 因此,在C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe上,管理员组具有读取,读取和执行权限。 这足够的权限吗? - dm80

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接