检查.NET 3.5并启用(如果禁用)

时间:2016-04-11 13:52:03

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

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

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

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

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

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

1 个答案:

答案 0 :(得分:1)

使用点网代码可以轻松完成。如,

  1. 使用适当的参数启动Dism进程。
  2. 不要使用ShellExecute。
  3. 重定向标准输出。
  4. 阅读标准输出。
  5. 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.