在 asp.net mvc 中托管像 cli 这样的 powershell 来执行维护

发布于 2024-09-16 00:26:15 字数 178 浏览 5 评论 0原文

我的 asp.net mvc 应用程序需要 CLI 来执行维护任务和一些状态任务。

我认为powershell就是为了提供CLI而设计的。但除了名字之外我一无所知。

如何在 iis 中运行的 asp.net mvc 中托管 powershell 来为自定义任务提供 CLI?

I need CLI for our asp.net mvc application to perform maintenance tasks and some status tasks.

I think powershell is designed to provide CLI. But i do not know anything about it other than name.

How can i host powershell in asp.net mvc running in iis to provide CLI for custom tasks?

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

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

发布评论

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

评论(2

千纸鹤带着心事 2024-09-23 00:26:15

托管和使用 PowerShell 引擎的 .NET 代码非常简单:

private void ExecutePowerShellCommand(string command)
{
    using (var invoker = new RunspaceInvoke())
    {
        Collection<PSObject> results = invoker.Invoke(command);
        foreach (var result in results)
        {
            _listBox.Items.Add(result);
        }                
    }
}

技巧在于配置 PowerShell 运行空间以限制可用命令。您可能不想允许某人从任何旧目录中删除文件、关闭计算机或格式化驱动器(他们可以访问路径中的 EXE)。查看受限运行空间来限制什么可以通过这个机制来执行。您还可以限制可用的语言功能。

The .NET code to host and use the PowerShell engine is very simple:

private void ExecutePowerShellCommand(string command)
{
    using (var invoker = new RunspaceInvoke())
    {
        Collection<PSObject> results = invoker.Invoke(command);
        foreach (var result in results)
        {
            _listBox.Items.Add(result);
        }                
    }
}

The trick is in configuring the PowerShell runspace to limit the available commands. You probably don't want to allow someone to delete files from any old directory, shutdown the computer or format a drive (they would have access to EXEs in the path). Look into Constrained Runspaces to limit what can be executed via this mechanism. You can also limit which language features are available.

孤者何惧 2024-09-23 00:26:15

我以前从未在 PowerShell 中使用过 CLI,但我以前在 .NET 应用程序中执行过一些 PowerShell 的优点。这可能会有所帮助。

本 Channel9 剧集包含在 .NET 应用程序中运行 PowerShell 2.0 命令的相关详细信息(比 1.0 容易得多):http://channel9.msdn.com/posts/bruceky/How-to-Embedding-PowerShell-Within-aC-Application/

(请注意,该过程包括使用System.Diagnostics.Process在操作过程的标准输入和输出流时调用命令。遭遇挫折和失败。)

I've never done CLI within PowerShell before, but I have executed some PowerShell goodness within a .NET application before. Here's something that might help.

This Channel9 episode contains relevant details for running PowerShell 2.0 commands in a .NET application (which is a lot easier than 1.0): http://channel9.msdn.com/posts/bruceky/How-to-Embedding-PowerShell-Within-a-C-Application/

(Note that the process does not include using System.Diagnostics.Process to invoke the commands while manipulating the process' standard input and output stream. Any attempts I made down that road were met with frustration and failure.)

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