【问题标题】:C# - Checking For Device Problems and System ProblemsC# - 检查设备问题和系统问题
【发布时间】:2010-12-06 13:12:14
【问题描述】:

在 C# 中,我该如何检查设备和系统错误?使用 PowerShell Scipts 会很简单,还是会增加复杂性和难度?

【问题讨论】:

  • 你能解释一下“设备和系统错误”是什么意思吗?哪些设备和系统?
  • 没有具体错误,只是一般错误。也许检测设备可能出现故障的可能原因。我可以手动执行此操作,但它是针对其他用户的,所以为了让生活更轻松,也许会检测一些可能的原因并将其显示给用户。

标签: c# powershell system device


【解决方案1】:

对于 Windows 7 客户端,请查看 Windows Troubleshooting Platform。这是一个download,上面有更多细节。它使用 PowerShell 脚本来准确地执行您所说的操作。这个blog post 展示了如何编写故障排除包——这很容易。

我认为 WTP 不适用于低级平台。在这种情况下,我只需编写一些 PowerShell 脚本来检测和修复根本原因。如果您想将其包装在一个漂亮的 UI 中,请查看 PowerBoots - 一种在脚本之上创建 WPF GUI 的简单方法。如果您想在基于 C# 的 GUI 中托管 PowerShell,这非常简单。这是来自表单应用程序的代码 sn-p:

    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;

【讨论】:

  • 我很想在我的 GUI 中加入一些 Powershell。我可以在哪里与您联系?
猜你喜欢
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 2021-01-13
  • 2012-12-13
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
相关资源
最近更新 更多