ASP.NET 进程启动 PowerShell 脚本 IIS 7.5

发布于 2024-12-11 04:27:38 字数 1161 浏览 0 评论 0原文

在我的开发计算机上一切正常,但如果部署到 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 App
身份验证是 Windows

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

All works fine on my dev machine but if deployed to IIS the process doesn't get started. I am starting a powershell script by

    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();
            }
        }  
    }

Here's what's configured for the app pool this is running under.

Process Model

->Identity = domain user who is a Domain Admin.

->Load User Profile = True

Web App

Authentication is Windows

What else do I need to configure to so that I can run the Process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

jJeQQOZ5 2024-12-18 04:27:38

正如 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”

As Start-Automating suggested I eventually ended up doing this:

            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();
            }
        }

On the IIS server I executed the following powershell command "Set-ExecutionPolicy RemoteSigned"

若相惜即相离 2024-12-18 04:27:38

最好将 PowerShell API 嵌入调用 .exe

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

http://powershellpipeworks.com/

It's much better to embed the PowerShell APIs the call the .exe

Here's an old link that will get you a PowerShell runspace embedded in ASP.NET per user:

http://powershellpipeworks.com/

优雅的叶子 2024-12-18 04:27:38

检查powershell.exe所在文件系统的权限。

另外,请检查事件查看器中的安全日志是否存在身份验证错误和访问违规。

Check the permissions of the file system where powershell.exe lives.

Also, check the Security Log in the Event Viewer for authentication errors and access violations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文