C# - Checking For Device Problems and System Problems
In C#, How Could I go ab开发者_运维百科out checking for device and systems errors? Would it be simple to use PowerShell Scipts, or would that add to the complexity and difficulty?
For Windows 7 clients check out the Windows Troubleshooting Platform. Here is a download on it with more details. It uses PowerShell scripts to do exacty what you're talking about. This blog post shows how to author a troubleshooting pack - it's pretty easy.
I don't think WTP works on downlevel platforms. In this case, I would just write some PowerShell scripts to detect and fix root causes. If you want to wrap that up in a nice UI, check out PowerBoots - an easy way to create a WPF GUI on top of your script. If you want to host PowerShell in your on C#-based GUI it is very simple. Here's a code snippet from a Forms app:
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;
}
}
}
You need to add a reference to the System.Management.Automation assembly. If you have installed the Windows/.NET SDK that should be in ProgramFiles\ReferenceAssemblies\Microsoft\WindowsPowerShell\v1.0. You will also need a couple of using statememets:
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
精彩评论