从 .NET Windows 应用程序中运行 powershell 脚本 [英] Running powershell scripts from within a .NET windows app

查看:65
本文介绍了从 .NET Windows 应用程序中运行 powershell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 vb.net Windows 应用程序中运行脚本.

我的脚本在后台运行良好;

 使用 MyRunSpace 作为 Runspace = RunspaceFactory.CreateRunspace()MyRunSpace.Open()使用 MyPipeline 作为管道 = MyRunSpace.CreatePipeline()MyPipeline.Commands.AddScript("import-module -name " & moduleName &vbCrLf &"(get-module -name " & moduleName & ").version")Dim 结果 = MyPipeline.Invoke()'对结果做点什么结束使用MyRunSpace.Close()结束使用

但是,我现在需要能够运行 powershell(不在后台),例如.出现提示时;

<块引用>

Set-ExecutionPolicy 不受限制

我目前正在查看 Microsoft.PowerShell.ConsoleHost 命名空间,看看我是否可以使用类似的东西;

Dim config = RunspaceConfiguration.CreateConsoleShell.Start(config, "Windows PowerShell", "", New String() {""})

谁能给我建议???

我对此做了一些捏造;

 公共函数 RunPowershellViaShell(ByVal scriptText As String) As IntegerDim execProcess 作为新 System.Diagnostics.ProcessDim psScriptTextArg = "-NoExit -Command ""&get-module -list"""'Dim psScriptTextArg = "-NoExit -Command ""&set-executionPolicy unrestricted"""'Dim psScriptTextArg = ""-NoExit -Command """ & scriptText & """"execProcess.StartInfo.WorkingDirectory = Environment.SystemDirectory &"\WindowsPowershell\v1.0\"execProcess.StartInfo.FileName = "powershell.exe"execProcess.StartInfo.Arguments = psScriptTextArgexecProcess.StartInfo.UseShellExecute = True返回 execProcess.Start结束函数

但一定有更好的方法吗??

解决方案

PowerShell 引擎与其主机之间存在区别.您想要的是在您的应用程序中运行引擎,然后启动一个单独的主机(它也托管 PowerShell 引擎)来处理提示.您可能需要考虑修改您的应用程序以充当主机本身.然后,您可以对提示(读取主机)和弹出对话框或其他任何内容做出反应.看看这个相关的PowerShell命名空间.另请查看此 有关创建简单 PSHost 的博文.

I'm needing to run scripts from within a vb.net windows app.

I've got the scripts running in the background fine;

 Using MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()
      MyRunSpace.Open()
      Using MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
        MyPipeline.Commands.AddScript("import-module -name " & moduleName &
                              vbCrLf &
                              "(get-module -name " & moduleName & ").version")


        Dim results = MyPipeline.Invoke()
        'Do something with the results
      End Using

      MyRunSpace.Close()
    End Using

However, i now need to be able to have the powershell run (not in the background) eg. When prompts occur;

Set-ExecutionPolicy unrestricted

I'm currently looking into the Microsoft.PowerShell.ConsoleHost namespace to see if i can use something like;

Dim config = RunspaceConfiguration.Create
ConsoleShell.Start(config, "Windows PowerShell", "", New String() {""})

Can anyone advise me please???

EDIT: I've fudged it a bit with this;

  Public Function RunPowershellViaShell(ByVal scriptText As String) As Integer
    Dim execProcess As New System.Diagnostics.Process
    Dim psScriptTextArg = "-NoExit -Command ""& get-module -list"""
    'Dim psScriptTextArg = "-NoExit -Command ""& set-executionPolicy unrestricted"""
    'Dim psScriptTextArg = ""-NoExit -Command """ & scriptText & """"

    execProcess.StartInfo.WorkingDirectory = Environment.SystemDirectory & "\WindowsPowershell\v1.0\"
    execProcess.StartInfo.FileName = "powershell.exe"
    execProcess.StartInfo.Arguments = psScriptTextArg
    execProcess.StartInfo.UseShellExecute = True
    Return execProcess.Start
  End Function

But there's gotta be a better way??

解决方案

There is a distinction between the PowerShell engine and its host. What you're wanting is to run the engine within your application but then fire up a separate host (which also is hosting the PowerShell engine) to handle prompts. You might want to look into modifying your application to act as a host itself. You could then react to prompts (read-host) and pop dialog boxes or whatever. Take a look at this relevant PowerShell namespace. Also check out this blog post on creating a simple PSHost.

这篇关于从 .NET Windows 应用程序中运行 powershell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆