Powershell ConsoleShell.Start 出现问题

发布于 2024-09-12 11:31:00 字数 4472 浏览 2 评论 0原文

运行以下代码会导致 HostException;

  Public Sub RunPowershellInConsole(ByVal scriptText As String)
    Dim config = RunspaceConfiguration.Create
    Dim args() As String = New String() {scriptText}

    ConsoleShell.Start(config, "Windows PowerShell", "", args)
  End Sub

System.Management.Automation.Host.HostException was unhandled
  Message=The Win32 internal error "The handle is invalid" 0x6 occurred when retrieving handle for active console output buffer. Contact Microsoft Support Services.
  Source=Microsoft.PowerShell.ConsoleHost
  WasThrownFromThrowStatement=False
  StackTrace:
       at Microsoft.PowerShell.ConsoleControl.GetActiveScreenBufferHandle()
       at Microsoft.PowerShell.ConsoleHostRawUserInterface.GetBufferInfo(CONSOLE_SCREEN_BUFFER_INFO& bufferInfo)
       at Microsoft.PowerShell.ConsoleHostRawUserInterface.get_ForegroundColor()
       at Microsoft.PowerShell.ConsoleHostRawUserInterface..ctor(ConsoleHostUserInterface mshConsole)
       at Microsoft.PowerShell.ConsoleHostUserInterface..ctor(ConsoleHost parent)
       at Microsoft.PowerShell.ConsoleHost..ctor(RunspaceConfiguration configuration)
       at Microsoft.PowerShell.ConsoleHost.CreateSingletonInstance(RunspaceConfiguration configuration)
       at Microsoft.PowerShell.ConsoleHost.Start(RunspaceConfiguration configuration, String bannerText, String helpText, String preStartWarning, String[] args)
       at Microsoft.PowerShell.ConsoleShell.Start(RunspaceConfiguration configuration, String bannerText, String helpText, String preStartWarning, String[] args)
       at Microsoft.PowerShell.ConsoleShell.Start(RunspaceConfiguration configuration, String bannerText, String helpText, String[] args)
       at MyApp.Barry.Bosely.RunPowershell.RunPowershellInConsole(String scriptText) in C:\Dev\MiscProjects\GordonB\Barry\Barry.Bosely\RunPowershell.vb:line 87
       at MyApp.Barry.Bosely.frmMain.TEstToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Dev\MiscProjects\GordonB\Barry\Barry.Bosely\frmMain.vb:line 119
       at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
       at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
       at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ToolStrip.WndProc(Message& m)
       at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at MyApp.Barry.Bosely.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.ComponentModel.Win32Exception
       ErrorCode=-2147467259
       Message=The handle is invalid
       NativeErrorCode=6
       InnerException: 

对任何人都有帮助吗?

Running the following code resulting in an HostException;

  Public Sub RunPowershellInConsole(ByVal scriptText As String)
    Dim config = RunspaceConfiguration.Create
    Dim args() As String = New String() {scriptText}

    ConsoleShell.Start(config, "Windows PowerShell", "", args)
  End Sub

.

System.Management.Automation.Host.HostException was unhandled
  Message=The Win32 internal error "The handle is invalid" 0x6 occurred when retrieving handle for active console output buffer. Contact Microsoft Support Services.
  Source=Microsoft.PowerShell.ConsoleHost
  WasThrownFromThrowStatement=False
  StackTrace:
       at Microsoft.PowerShell.ConsoleControl.GetActiveScreenBufferHandle()
       at Microsoft.PowerShell.ConsoleHostRawUserInterface.GetBufferInfo(CONSOLE_SCREEN_BUFFER_INFO& bufferInfo)
       at Microsoft.PowerShell.ConsoleHostRawUserInterface.get_ForegroundColor()
       at Microsoft.PowerShell.ConsoleHostRawUserInterface..ctor(ConsoleHostUserInterface mshConsole)
       at Microsoft.PowerShell.ConsoleHostUserInterface..ctor(ConsoleHost parent)
       at Microsoft.PowerShell.ConsoleHost..ctor(RunspaceConfiguration configuration)
       at Microsoft.PowerShell.ConsoleHost.CreateSingletonInstance(RunspaceConfiguration configuration)
       at Microsoft.PowerShell.ConsoleHost.Start(RunspaceConfiguration configuration, String bannerText, String helpText, String preStartWarning, String[] args)
       at Microsoft.PowerShell.ConsoleShell.Start(RunspaceConfiguration configuration, String bannerText, String helpText, String preStartWarning, String[] args)
       at Microsoft.PowerShell.ConsoleShell.Start(RunspaceConfiguration configuration, String bannerText, String helpText, String[] args)
       at MyApp.Barry.Bosely.RunPowershell.RunPowershellInConsole(String scriptText) in C:\Dev\MiscProjects\GordonB\Barry\Barry.Bosely\RunPowershell.vb:line 87
       at MyApp.Barry.Bosely.frmMain.TEstToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Dev\MiscProjects\GordonB\Barry\Barry.Bosely\frmMain.vb:line 119
       at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
       at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
       at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ToolStrip.WndProc(Message& m)
       at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at MyApp.Barry.Bosely.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.ComponentModel.Win32Exception
       ErrorCode=-2147467259
       Message=The handle is invalid
       NativeErrorCode=6
       InnerException: 

Little help anyone?

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

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

发布评论

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

评论(1

仲春光 2024-09-19 11:31:00

我刚刚在我的控制台应用程序中尝试了这段代码,它起作用了:

using System.Management.Automation.Runspaces;
using Microsoft.PowerShell;

namespace TryConsoleShell
{
    class Program
    {
        static void Main(string[] args)
        {
            var config = RunspaceConfiguration.Create();
            ConsoleShell.Start(config, "Windows PowerShell", "help text", new string[] { "-noexit", "ls" });
        }
    }
}

我猜您的应用程序是Windows应用程序,并且这种情况要么根本不支持,要么您仍然必须有一个可用的控制台窗口(我还没有尝试过这种方式)。

编辑:

它也适用于 WinForm 应用程序。但控制台主机仍然需要控制台窗口。要使控制台窗口可用,只需将项目输出类型更改为控制台应用程序

using System;
using System.Management.Automation.Runspaces;
using System.Windows.Forms;
using Microsoft.PowerShell;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var config = RunspaceConfiguration.Create();
            ConsoleShell.Start(config, "Windows PowerShell", "help text", new string[] { "-noexit", "ls" });
        }
    }
}

PS 我从未在实践中见过这种场景的使用,甚至不知道它实际上有效。它有一些实际有用的应用吗?

I have just tried this code in my Console Application and it worked:

using System.Management.Automation.Runspaces;
using Microsoft.PowerShell;

namespace TryConsoleShell
{
    class Program
    {
        static void Main(string[] args)
        {
            var config = RunspaceConfiguration.Create();
            ConsoleShell.Start(config, "Windows PowerShell", "help text", new string[] { "-noexit", "ls" });
        }
    }
}

I guess your application is Windows Application and this scenario is either not supported at all or you still have to have a console window available (I have not tried this way yet).

EDIT:

It worked in WinForm Application as well. But the console window is still needed for the console host. To make the console window available just change the project output type to Console Application.

using System;
using System.Management.Automation.Runspaces;
using System.Windows.Forms;
using Microsoft.PowerShell;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var config = RunspaceConfiguration.Create();
            ConsoleShell.Start(config, "Windows PowerShell", "help text", new string[] { "-noexit", "ls" });
        }
    }
}

P.S. I have never seen use of this scenario in practice and did not even know that it actually works. Are there some practically useful applications of it?

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