Powershell ConsoleShell.Start 出现问题
运行以下代码会导致 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚在我的控制台应用程序中尝试了这段代码,它起作用了:
我猜您的应用程序是Windows应用程序,并且这种情况要么根本不支持,要么您仍然必须有一个可用的控制台窗口(我还没有尝试过这种方式)。
编辑:
它也适用于 WinForm 应用程序。但控制台主机仍然需要控制台窗口。要使控制台窗口可用,只需将项目输出类型更改为控制台应用程序。
PS 我从未在实践中见过这种场景的使用,甚至不知道它实际上有效。它有一些实际有用的应用吗?
I have just tried this code in my Console Application and it worked:
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.
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?