在 C# 中,我该如何检查设备和系统错误?使用 PowerShell Scipts 会很简单,还是会增加复杂性和难度?
问问题
245 次
1 回答
2
对于 Windows 7 客户端,请查看Windows 故障排除平台。这是有关它的下载,其中包含更多详细信息。它使用 PowerShell 脚本来准确地执行您所说的操作。这篇博文展示了如何编写故障排除包 - 这非常简单。
我认为 WTP 不适用于低级平台。在这种情况下,我只需编写一些 PowerShell 脚本来检测和修复根本原因。如果您想将其包装在一个漂亮的 UI 中,请查看PowerBoots - 一种在脚本之上创建 WPF GUI 的简单方法。如果您想在基于 C# 的 GUI 中托管 PowerShell,这非常简单。这是来自表单应用程序的代码片段:
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。您还需要几个使用 statememets:
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
于 2009-10-14T14:17:25.100 回答