从代码运行Windows Update时获取KB号 [英] Getting KB numbers when running Windows Update from code

查看:79
本文介绍了从代码运行Windows Update时获取KB号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果运行此代码,它将检查Windows更新.

我从投诉列表中提取清单,从投诉列表中提取清单,在投诉列表中,还列出了一些所需的更新.

我需要能够在同一窗口中显示kb文章编号和MSRC编号是用于需要更新的计算机的.

谁能告诉我我在做什么?
我使用Visual Basic2008.

If you run this code it checks for windows updates.

I pulls a list of in complaince and out of complaince comuters, in the out of complaince it also pulls a number of needed updates.

I need to be able to display in that same window, what the kb article number is and the MSRC number is for the computers that need the updates.

Can anyone tell me what I am doing worng?
I use Visual Basic 2008.

Copyright (c) Microsoft Corporation. All rights reserved. 
Imports Microsoft.UpdateServices.Administration
Imports System.Text

Module Module1

      Sub Main()
            Try
                  Dim iUpdateServer As IUpdateServer
                  Dim AdminProxy As AdminProxy
                  Dim Body As New StringBuilder
                  Dim InComplianceComputers As New StringBuilder
                  Dim OutOfComplianceComputers As New StringBuilder
                  Dim Computers As ComputerTargetCollection
                  Dim NeededUpdatesCount As Integer
                  Dim InstallInfoCollection As UpdateInstallationInfoCollection
                  Dim OutOfComplianceCount As Integer = 0
                  Dim InComplianceCount As Integer = 0
                  Dim Index As Integer = 0
                  Dim OutOfCompliance As Boolean = False
                  Dim File As System.IO.StreamWriter
                  Dim SecurityBulletins As System.Collections.Specialized.StringCollection
                  ' Dim KBArticles As System.Collections.Specialized.StringCollection
                  Dim UpdateCollection As UpdateCollection

                  'connect to the local server
                  AdminProxy = New AdminProxy
                  iUpdateServer = AdminProxy.GetUpdateServer

                  'get the computers
                  Computers = iUpdateServer.GetComputerTargets()

                  'get the list of updates
                  UpdateCollection = iUpdateServer.GetUpdates(ApprovedStates.Any, Date.MinValue, Date.MaxValue, Nothing, Nothing)

                  'get the list of approved updates
                  UpdateCollection = iUpdateServer.GetUpdates(ApprovedStates.HasStaleUpdateApprovals + ApprovedStates.LatestRevisionApproved, Date.MinValue, Date.MaxValue, Nothing, Nothing)

                  Console.WriteLine("Getting computer status...")

                  'loop through all computers and get the status of updates..
                  For Each Computer As IComputerTarget In Computers

                        'clear the flag that indicates this computer is out of compliance
                        OutOfCompliance = False
                        'get the status of all the updates on the computer
                        InstallInfoCollection = Computer.GetUpdateInstallationInfoPerUpdate()

                        'clear the collection we use to hold the needed & failed updates
                        NeededUpdatesCount = 0


                        For Each InstallInfo As IUpdateInstallationInfo In InstallInfoCollection
                              'is this update in a needed or failed state?
                              If (InstallInfo.UpdateInstallationState = UpdateInstallationState.Failed) Or _
                                 (InstallInfo.UpdateInstallationState = UpdateInstallationState.Downloaded) Or _
                                 (InstallInfo.UpdateInstallationState = UpdateInstallationState.NotInstalled) _
                              Then
                                    'if it is, set the flag to true to indicate that this box is out of compliance
                                    OutOfCompliance = True
                                    NeededUpdatesCount = NeededUpdatesCount + 1
                              End If

                        Next

                        'write a line for the computer
                        If OutOfCompliance = True Then
                              OutOfComplianceComputers.AppendFormat("<tr><td >{0}</td><td >{1}</td><td>{2}</td></tr>", "computerName=""" + Computer.FullDomainName + """", NeededUpdatesCount.ToString, Computer.LastSyncTime)
                        Else
                              InComplianceComputers.AppendFormat("<tr><td>{0}</td><td >{1}</td><td>{2}</td></tr>", "computername=""" + Computer.FullDomainName + """", NeededUpdatesCount.ToString, Computer.LastSyncTime)
                        End If

                        'Console.WriteLine("pause")
                        'If OutOfCompliance = True Then
                        '      SecurityBulletins.ToString()

                        For Each update As IUpdate In UpdateCollection

                              SecurityBulletins = update.SecurityBulletins
                              'if there are any list them
                              For Each SecurityBulletin As String In SecurityBulletins
                                    String.Format(Text.UTF8Encoding.UTF8.ToString)
                                    If OutOfCompliance = True Then
                                          Body.AppendFormat("<tr><td>{0}</td><td >{1}</td><td>{2}</td></tr>", SecurityBulletins.ToString)
                                    Else
                                          'do nothing
                                    End If
                              Next



                              'If OutOfCompliance = True Then
                              '      OutOfComplianceComputers.AppendFormat("<tr><td >{0}</td><td >{1}</td><td>{2}</td></tr>", "Security Bulletin", SecurityBulletins.ToString)
                              'End If
                              'up the index count

                              Index = Index + 1

                              If OutOfCompliance = True Then
                                    OutOfComplianceCount = OutOfComplianceCount + 1
                              Else
                                    InComplianceCount = InComplianceCount + 1
                              End If
                        Next
                  Next


                  ' create the HTML body
                  Body.Append("<html><head><meta http-equiv='Content-Language' content='en-us'><meta http-equiv='Content-Type' content='text/html; charset=windows-1252'><style><!--.SectionHead { font-family: Tahoma; font-size: 8pt; color: #FFFFFF; font-weight: bold; margin: 4 5; background-color: #ABA483 }.SubTable      { font-family: Tahoma; font-size: 8pt; margin-left: 10; margin-right: 10 }.MainTable   { font-family: Tahoma; font-size: 8pt; border: 2px solid #ABA483 }--></style></head><body>")
                  Body.AppendFormat("<table border='1' width='98%' id='MainTable' class='MainTable' style='border-collapse: collapse; border-width: 1px'> <tr><td class='SectionHead' nowrap align='left' valign='top'>Computer Compliance Report</td></tr><tr><td style='border-bottom-style: none; border-bottom-width: medium'>There are {0} out of {1} computers that need updates as of {2}.<br><br></td></tr>", OutOfComplianceCount, Computers.Count, DateTime.Now.ToString)

                  'fill in the details for the out of compliance computers
                  Body.AppendFormat("<tr><td class='SectionHead' nowrap align='left' valign='top'>Computers out of compliance: {0}</td></tr><tr><td>", OutOfComplianceCount.ToString)
                  Body.Append("<table border='0' width='90%' id='NeedingUpdates' class='SubTable' style='border-collapse: collapse'>")
                  Body.AppendFormat("<tr><td><b>Computer Name</b></td><td><b>Needed Updates</b></td><td ><b>Last Contact</b></td></tr>")
                  Body.Append(OutOfComplianceComputers)
                  Body.Append("</table>")

                  'fill in the details for the in compliance computers
                  Body.AppendFormat("<tr><td class='SectionHead' nowrap align='left' valign='top'>Computers in compliance: {0}</td></tr><tr><td>", InComplianceCount.ToString)
                  Body.Append("<table border='0' width='90%' id='NotNeedingUpdates' class='SubTable' style='border-collapse: collapse'>")
                  Body.AppendFormat("<tr><td ><b>Computer Name</b></td><td ><b>Needed Updates</b></td><td ><b>Last Contact</b></td></tr>")
                  Body.Append(InComplianceComputers)
                  Body.Append("</table>")
                  Body.Append("</table></body></html>")

                  ''fill in the details for the in computers MSRC
                  'Body.AppendFormat("<tr><td class='SectionHead' nowrap align='left' valign='top'>Computers MSRC: {0}</td></tr><tr><td>", OutOfComplianceCount.ToString)
                  'Body.Append("<table border='0' width='90%' id='NeedingUpdates' class='SubTable' style='border-collapse: collapse'>")
                  'Body.AppendFormat("<tr><td ><b>Computer Name</b></td><td ><b>Needed Updates</b></td><td ><b>Last Contact</b></td></tr>")
                  'Body.Append(SecurityBulletins)
                  'Body.Append("</table>")
                  'Body.Append("</table></body></html>")

                  Console.WriteLine("pause")

                  'write the results to the file. 
                  Console.WriteLine("Creating HTML File...")
                  File = New System.IO.StreamWriter(Environment.CurrentDirectory & "\Compliance report- " & DateTime.Now.ToLongDateString & ".html")

                  Console.WriteLine("Writing results to HTML File...")
                  File.Write(Body.ToString)
                  Console.WriteLine("Saving HTML File...")
                  File.Close()

                  Console.WriteLine("Complete. Results are saved to the {0}\Compliance report- {1}.html file", Environment.CurrentDirectory, DateTime.Now.ToLongDateString)

            Catch ex As Exception
                  Console.WriteLine("An error occurred: " & ex.ToString)
            End Try


      End Sub


End Module

推荐答案

通过UpdateCollection时,与SecurityBulletins相同,可以通过update.KnowledgebaseArticles获得适用于该更新的KB文章,也可以通过update.MsrcSeverity获得MSRC.
As you go through the UpdateCollection the KB articles that apply to the update are available through update.KnowledgebaseArticles same as the SecurityBulletins, also the MSRC is available through update.MsrcSeverity.


这篇关于从代码运行Windows Update时获取KB号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆