【问题标题】:Check for .NET 3.5 and enable if disabled检查 .NET 3.5 并在禁用时启用
【发布时间】:2016-04-11 13:52:02
【问题描述】:

我有一个运行以下行的批处理文件:

dism /online /get-features /format:table | find "NetFx3"

如果输出返回 NetFx3 已“禁用”,我想使用以下命令启用它:

dism.exe /online /enable-feature /featurename:NetFX3

我不确定如何实际查询输出并检查启用/禁用。

【问题讨论】:

  • 如果不检查直接运行dism.exe /online /enable-feature /featurename:NetFX3会怎样?这会破坏脚本吗?
  • 奇怪的是,如果“启用”命令已经启用,我并没有考虑运行它。我会在一些设备上测试一下,看看输出是什么。

标签: windows batch-file windows-server-2012


【解决方案1】:

使用 dot-net 代码可以轻松完成。作为,

  1. 使用适当的参数启动 Dism 进程。
  2. 不要使用 ShellExecute。
  3. 重定向标准输出。
  4. 读取标准输出。

Vb.net 代码:

Public Function GetDismOutput() As String
        Dim Output As String = Nothing
        Dim SxsPath As String = "D:\Sources\sxs"
        Dim CmdParm As String = "/online /enable-feature /featurename:NetFX3 /All /Source:"""
        Dim Pinf As New ProcessStartInfo With {.FileName = SxsPath, .Arguments = CmdParm + SxsPath + """ /LimitAccess"}
        Dim Prc As Process
        Try
            Pinf.UseShellExecute = False
            Pinf.RedirectStandardOutput = True
            Prc = Process.Start(Pinf)
            Prc.WaitForExit(20 * 60 * 1000) '' Wait for 20 Minutes
            CmdParm = Prc.StandardOutput.ReadToEnd()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return Output
    End Function

C# 代码:

  public string GetDismOutput()
  {
        string Output = null;
        string SxsPath = "D:\\Sources\\sxs";
        string CmdParm = "/online /enable-feature /featurename:NetFX3 /All /Source:\"";
        ProcessStartInfo Pinf = new ProcessStartInfo {FileName = SxsPath, Arguments = CmdParm + SxsPath + "\" /LimitAccess"};
        Process Prc = null;
        try
        {
            Pinf.UseShellExecute = false;
            Pinf.RedirectStandardOutput = true;
            Prc = Process.Start(Pinf);
            Prc.WaitForExit(20 * 60 * 1000); //' Wait for 20 Minutes
            CmdParm = Prc.StandardOutput.ReadToEnd();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return Output;
    }

其中 D:\Sources\sxs 是 sxs 文件夹的路径。 输出字符串如下:

Deployment Image Servicing and Management tool
Version: 6.3.9600.17031
Image Version: 6.3.9600.17031
Enabling feature(s)
The operation completed successfully.

【讨论】:

    猜你喜欢
    • 2021-01-04
    • 2013-03-27
    • 2012-03-11
    • 2010-11-13
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多