2010-07-28 68 views
1

運行以下代碼導致HostException;Powershell ConsoleShell.Start出現問題

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: 

對別人沒有幫助嗎?

回答

1

我剛纔想這個代碼在我控制檯應用程序和它的工作:

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

P.S.我從來沒有在實踐中看到過使用這個場景,甚至都不知道它實際上有效。它有一些實際有用的應用程序嗎?

+0

我希望如此,但我需要看看shell窗口如何處理像set-executionPolicy這樣的腳本不受限制 – GordonB 2010-07-28 16:02:13

+1

如果只關注'Set-ExecutionPolicy',那麼您可以避免使用'-Force'開關進行交互: Set-ExecutionPolicy RemoteSigned -Force'。 – 2010-07-28 16:17:35

+0

這不是全部......我們有其他腳本,用戶希望看到輸出......歡呼遠程簽名的力量。 +1;) – GordonB 2010-07-29 09:16:26