VBScript - Проверка исправлений Windows - Массивы - Если иначе - Для следующего

Мне было поручено получить текущий уровень исправлений для числа компьютеров в домене. В настоящее время с помощью фрагментов кода, используемых из многих источников, мне удалось придумать следующее:

Мой подход состоял в том, чтобы сначала проверить исправления за июль 2017 года, а затем перейти к июню 2017 года, а затем продолжить до марта 2017 года. Я довольно новичок в VBScript и нуждаюсь в совете:| (Только VBScript может использоваться из-за многих ограничений в серверных средах)

' Checks only servers

' Add Respective HotFixID in to respective Array
' Create a new Array if missing in Format "HotFixIDs<YYYY><MM>"
' Add the Array Name as the First in "HotFixIDArray"

'Enter the hotfix number to check for: ONLY the number, no letters here! 

ComputerName = "." 

Dim status
Dim found
Dim HotFixIDArray()

Set fso = CreateObject("Scripting.FileSystemObject" )            
Set file = fso.OpenTextFile("C:\temp\tetran\patchstatus_2017.txt",2,1)

found = 0
HotFixIDArray = Array("HotFixIDs201707","HotFixIDs201706","HotFixIDs201705","HotFixIDs201704","HotFixIDs201703")

For i = 0 To UBound(HotFixIDArray) (

    If found = 0 Then
        'Check July 2017 Patches
        Dim HotFixIDs201707()
        HotFixIDs201707 = Array("4032955","4026061","4026059","4025877","4025872","4025674","4025497","4025409","4025398","4025397","4025343","4025341","4025339","4025337","4025336","4025333","4025331","4025240","4022914","4022748","4022746")

        For j = 0 To UBound(HotFixIDs201707)
            status = CheckParticularHotfix(ComputerName, HotFixIDs201707(j))
            If status = True Then
                file.writeLine ("The hotfix with number " & HotFixIDs201707(j) & " IS installed.")
                found = 1
                Exit For
            Else
                status = False
                found = 0
        Next

        If found = 1 Then
            file.writeLine ("2017 July Patch Installed")
            Exit For
        Else
            'Check June 2017 Patches
            Dim HotFixIDs201706()
            HotFixIDs201706 = Array("3217845","4018106","4019204","4019263","4019264","4021903","4021923","4022008","4022010","4022013","4022715","4022717","4022718","4022719","4022722","4022724","4022726","4022883","4022884","4022887","4024402")

            For j = 0 To UBound(HotFixIDs201706)
                status = CheckParticularHotfix(ComputerName, HotFixIDs201706(j))
                If status = True Then
                    file.writeLine ("The hotfix with number " & HotFixIDs201706(j) & " IS installed.")
                    found = 1
                    Exit For
                Else
                    status = False
                    found = 0
            Next

            If found = 1 Then
                file.writeLine ("2017 June Patch Installed")
                Exit For
            Else
                'Check May 2017 Patches
                Dim HotFixIDs201705()
                HotFixIDs201705 = Array("4018196","4018466","4018556","4018821","4018885","4018927","4019149","4019206","4019213","4019214","4019215","4019216","4019472")

                For j = 0 To UBound(HotFixIDs201705)
                    status = CheckParticularHotfix(ComputerName, HotFixIDs201705(j))
                    If status = True Then
                        file.writeLine ("The hotfix with number " & HotFixIDs201705(j) & " IS installed.")
                        found = 1
                        Exit For
                    Else
                        status = False
                        found = 0
                Next
                    If found = 1 Then
                        file.writeLine ("2017 May Patch Installed")
                        Exit For
                    Else
                        'Check April 2017 Patches
                        Dim HotFixIDs201704()
                        HotFixIDs201704 = Array("3211308","3217841","4014652","4014793","4014794","4015068","4015195","4015217","4015380","4015383","4015546","4015547","4015548","4015549","4015550","4015551","4020535")

                        For j = 0 To UBound(HotFixIDs201704)
                            status = CheckParticularHotfix(ComputerName, HotFixIDs201704(j))
                            If status = True Then
                                file.writeLine ("The hotfix with number " & HotFixIDs201704(j) & " IS installed.")
                                found = 1
                                Exit For
                            Else
                                status = False
                                found = 0
                        Next

                        If found = 1 Then
                            file.writeLine ("2017 April Patch Installed")
                            Exit For
                        Else
                            'Check March 2017 Patches
                            Dim HotFixIDs201703()
                            HotFixIDs201703 = Array("3211306","3214051","3217587","3217882","3218362","4011981","4012021","4012212","4012213","4012214","4012215","4012216","4012217","4012373","4012497","4012583","4012584","4012598","4013429","4017018")

                            For j = 0 To UBound(HotFixIDs201703)
                                status = CheckParticularHotfix(ComputerName, HotFixIDs201703(j))
                                If status = True Then
                                    file.writeLine ("The hotfix with number " & HotFixIDs201703(j) & " IS installed.")
                                    found = 1
                                    Exit For
                                Else
                                    status = False
                                    found = 0
                            Next
                                If found = 1 Then
                                    file.writeLine ("2017 March Patch Installed")
                                    Exit For
                                Else
                                    'Do nothing
                            '201703 End
                        '201704 End 
                    '201705 End     
                '201706 End         
            '201707 End             
        If found = 0 Then
            'Do nothing
        Else
            'Do nothing
    Else
        Exit For
)
Next

File.close

Private Function CheckParticularHotfix(strPuter, strHotfixID)
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  
    ' Version 1.0 
    ' Checks if a particular hotfix is installed or not.  
    ' This function has these 3 return options: 
    ' TRUE, FALSE, <error description>  
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
    On Error Resume Next 
    Set objWMIService = GetObject("winmgmts:" _ 
        & "{impersonationLevel=impersonate}!\\" & strPuter & "\root\cimv2") 
    If err.number <> 0 Then 
        CheckParticularHotfix = "WMI could not connect to computer '" & strPuter & "'" 
        Exit Function 'No reason to continue 
    End If 

    strWMIforesp = "Select * from Win32_QuickFixEngineering where HotFixID = 'Q" & strHotfixID &_  
    "' OR HotFixID = 'KB" & strHotfixID & "'" 
    Set colQuickFixes = objWMIService.ExecQuery (strWMIforesp) 
    If err.number <> 0 Then    'if an error occurs 
        CheckParticularHotfix = "Unable to get WMI hotfix info" 
    Else 'Error number 0 meaning no error occured  
        tal = colQuickFixes.count 
        If tal > 0 Then 
            CheckParticularHotfix = True    'HF installed 
        Else  
            CheckParticularHotfix = False    'HF not installed 
        End If 
    End If 
    Set colQuickFixes = Nothing 

    Err.Clear 
    On Error Goto 0 
End Function

1 ответ

Решение

Наконец-то обошлось и удалось исправить VBScript:)

'Enter the name of the computer to check: (Replace the dot with a computer name, to connect to a remote computer)
ComputerName = "."

'Creates the FileSystemObject and use it to read the file "patch2017.txt"
Set fso = CreateObject("Scripting.FileSystemObject" )            
Set file = fso.OpenTextFile("C:\temp\Patch\patch2017.txt",2,1)

'Variable to identify the installation status
Dim j
j = 0

'July 2017 Patches
'Enter the hotfix number to check for: (ONLY the number, no letters here!)
HotFixID = Array("4032955","4026061","4026059","4025877","4025872","4025674","4025497","4025409","4025398","4025397","4025343","4025341","4025339","4025337","4025336","4025333","4025331","4025240","4022914","4022748","4022746") 

For i = 0 To UBound(HotFixID)
    status = CheckParticularHotfix(ComputerName, HotFixID(i))
    If status = True Then
        j = 1
        File.WriteLine("July 2017")
        Exit For
    End If
Next

'June 2017 Patches
HotFixID = Array("3217845","4018106","4019204","4019263","4019264","4021903","4021923","4022008","4022010","4022013","4022715","4022717","4022718","4022719","4022722","4022724","4022726","4022883","4022884","4022887","4024402")

For i = 0 To UBound(HotFixID)
    status = CheckParticularHotfix(ComputerName, HotFixID(i))
    If status = True Then
        j = 1
        File.WriteLine("June 2017")
        Exit For
    End If
Next

'May 2017 Patches
HotFixID = Array("4018196","4018466","4018556","4018821","4018885","4018927","4019149","4019206","4019213","4019214","4019215","4019216","4019472")

For i = 0 To UBound(HotFixID)
    status = CheckParticularHotfix(ComputerName, HotFixID(i))
    If status = True Then
        j = 1
        File.WriteLine("May 2017")
        Exit For
    End If
Next

'April 2017 Patches
HotFixID = Array("3211308","3217841","4014652","4014793","4014794","4015068","4015195","4015217","4015380","4015383","4015546","4015547","4015548","4015549","4015550","4015551","4020535")

For i = 0 To UBound(HotFixID)
    status = CheckParticularHotfix(ComputerName, HotFixID(i))
    If status = True Then
        j = 1
        File.WriteLine("April 2017")
        Exit For
    End If
Next

'March 2017 Patches
HotFixID = Array("3211306","3214051","3217587","3217882","3218362","4011981","4012021","4012212","4012213","4012214","4012215","4012216","4012217","4012373","4012497","4012583","4012584","4012598","4013429","4017018")

For i = 0 To UBound(HotFixID)
    status = CheckParticularHotfix(ComputerName, HotFixID(i))
    If status = True Then
        j = 1
        file.WriteLine("March 2017")
        Exit For
    End If
Next

file.Close

'**************************************
'*  Read file and produce and output  *
'*  Only if the File is not Empty     *
'*  If File is empty output UNKNOWN   *
'**************************************
If (fso.FileExists("C:\temp\Patch\patch2017.txt")) Then
    If (fso.GetFile("C:\temp\Patch\patch2017.txt").Size <> 0) Then
        Dim firstLine
        Set firstLine = fso.OpenTextFile("C:\temp\Patch\patch2017.txt",1)
        WScript.Echo firstLine.ReadLine
        firstLine.Close
    Else
        WScript.Echo "UNKNOWN"
    End If
End If

'*********************************************************************
'*  This Function checks if a particular hotfix is installed or not  *
'*  This function has these 3 return options:                        *
'*      TRUE, FALSE, <error description>                             *
'*********************************************************************
Private Function CheckParticularHotfix(strPuter, strHotfixID)
    On Error Resume Next
    Set objWMIService = GetObject("winmgmts:" _ 
        & "{impersonationLevel=impersonate}!\\" & strPuter & "\root\cimv2")

    If err.number <> 0 Then
        CheckParticularHotfix = "WMI could not connect to computer '" & strPuter & "'"
        Exit Function
    End If

    strWMIforesp = "Select * from Win32_QuickFixEngineering where HotFixID = 'Q" & strHotfixID &_  
        "' OR HotFixID = 'KB" & strHotfixID & "'"

    Set colQuickFixes = objWMIService.ExecQuery (strWMIforesp)

    If err.number <> 0 Then
        CheckParticularHotfix = "Unable to get WMI hotfix info"
    Else
        tal = colQuickFixes.count
        If tal > 0 Then
            CheckParticularHotfix = True    'HF installed
        Else
            CheckParticularHotfix = False   'HF not installed
        End If

    End If

    Set colQuickFixes = Nothing

    Err.Clear

    On Error Goto 0

End Function
Другие вопросы по тегам