2009-10-14 31 views
0

在C#中,我該如何檢查設備和系統錯誤?使用PowerShell Scipts會很簡單,還是會增加複雜性和難度?C# - 檢查設備問題和系統問題

+0

您能說清楚「設備和系統錯誤」是什麼意思嗎?哪些設備和系統? – bobbymcr 2009-10-14 03:28:25

+0

沒有特定的錯誤,只是一般的錯誤。也許可以檢測到設備可能失敗的可能原因。我可以手動執行此操作,但是它適用於其他用戶,所以爲了讓生活變得更輕鬆,可能會檢測一些可能的原因並將其顯示給用戶。 – Michael 2009-10-14 03:40:54

回答

2

對於Windows 7客戶請查看Windows Troubleshooting Platform。這裏有一個download上面有更多的細節。它使用PowerShell腳本來完成你正在談論的內容。這blog post顯示如何撰寫故障排除包 - 這很容易。

我不認爲WTP適用於下層平臺。在這種情況下,我只需編寫一些PowerShell腳本來檢測並修復根本原因。如果你想把它包裝在一個漂亮的用戶界面中,請查看PowerBoots - 在腳本之上創建一個WPF GUI的簡單方法。如果你想在你的基於C#的GUI上託管PowerShell,那很簡單。以下是Forms應用程序的代碼片段:

private void button1_Click(object sender, EventArgs e) 
    { 
     string cmd = @"Get-ChildItem $home\Documents -recurse | " + 
         "Where {!$_.PSIsContainer -and " + 
         "($_.LastWriteTime -gt (Get-Date).AddDays(-7))} | " + 
         "Sort Fullname | Foreach {$_.Fullname}"; 

     using (Runspace runspace = RunspaceFactory.CreateRunspace()) 
     { 
      runspace.Open(); 
      using (Pipeline pipeline = runspace.CreatePipeline(cmd)) 
      { 
       this.Cursor = Cursors.WaitCursor; 

       pipeline.Commands.AddScript(cmd); 
       Collection<PSObject> results = pipeline.Invoke(); 
       foreach (PSObject obj in results) 
       { 
        listBox1.Items.Add(obj); 
       } 

       this.Cursor = Cursors.Default; 
      } 
     } 
    } 

您需要添加對System.Management.Automation程序集的引用。如果您已安裝應在ProgramFiles \ ReferenceAssemblies \ Microsoft \ WindowsPowerShell \ v1.0中的Windows/.NET SDK。您還需要使用狀態圖:

using System.Collections.ObjectModel; 
using System.Management.Automation; 
using System.Management.Automation.Runspaces; 
+0

我很想在我的GUI中獲得一些Powershell。我可以在哪裏與您聯繫? – Michael 2009-10-14 15:57:27

+0

你可以通過我的博客http://KeithHill.spaces.live.com與我聯繫 – 2009-10-14 17:52:12