none
vbs到vb2005的转移报错 RRS feed

  • 问题

  • 在微软找到了一个vbs脚本,是用来手工更新windows补丁的:
    Set updateSession = CreateObject("Microsoft.Update.Session")
    Set updateSearcher = updateSession.CreateupdateSearcher()
    
    WScript.Echo "Searching for updates..." & vbCRLF
    
    Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")
    
    
    WScript.Echo "List of applicable items on the machine:"
    
    For I = 0 To searchResult.Updates.Count-1
        Set update = searchResult.Updates.Item(I)
        WScript.Echo I + 1 & "> " & update.Title
    Next
    
    If searchResult.Updates.Count = 0 Then
    	WScript.Echo "There are no applicable updates."
    	WScript.Quit
    End If
    
    WScript.Echo vbCRLF & "Creating collection of updates to download:"
    
    Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
    
    For I = 0 to searchResult.Updates.Count-1
        Set update = searchResult.Updates.Item(I)
        WScript.Echo I + 1 & "> adding: " & update.Title 
        updatesToDownload.Add(update)
    Next
    
    WScript.Echo vbCRLF & "Downloading updates..."
    
    Set downloader = updateSession.CreateUpdateDownloader() 
    downloader.Updates = updatesToDownload
    downloader.Download()
    
    WScript.Echo  vbCRLF & "List of downloaded updates:"
    
    For I = 0 To searchResult.Updates.Count-1
        Set update = searchResult.Updates.Item(I)
        If update.IsDownloaded Then
           WScript.Echo I + 1 & "> " & update.Title 
        End If
    Next
    
    Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
    
    WScript.Echo  vbCRLF & "Creating collection of downloaded updates to install:" 
    
    For I = 0 To searchResult.Updates.Count-1
        set update = searchResult.Updates.Item(I)
        If update.IsDownloaded = true Then
           WScript.Echo I + 1 & "> adding:  " & update.Title 
           updatesToInstall.Add(update)	
        End If
    Next
    
    WScript.Echo  vbCRLF & "Would you like to install updates now? (Y/N)"
    strInput = WScript.StdIn.Readline
    WScript.Echo 
    
    If (strInput = "N" or strInput = "n") Then 
    	WScript.Quit
    ElseIf (strInput = "Y" or strInput = "y") Then
    	WScript.Echo "Installing updates..."
    	Set installer = updateSession.CreateUpdateInstaller()
    	installer.Updates = updatesToInstall
    	Set installationResult = installer.Install()
    	
    	'Output results of install
    	WScript.Echo "Installation Result: " & 	installationResult.ResultCode 
    	WScript.Echo "Reboot Required: " & 	installationResult.RebootRequired & vbCRLF 
    	WScript.Echo "Listing of updates installed " & 	 "and individual installation results:" 
    	
    	For I = 0 to updatesToInstall.Count - 1
    		WScript.Echo I + 1 & "> " & updatesToInstall.Item(i).Title & ": " & installationResult.GetUpdateResult(i).ResultCode 		
    	Next
    End If
    

    现在想用vb2005来实现,稍微修改了下代码:
    Module Module1
    
        Dim a As New MyLogWriter
    
        Sub Main()
            Dim updateSession, updateSearcher, searchResult, update, updatesToDownload, downloader, updatesToInstall
            Dim installer, installationResult
    
            updateSession = CreateObject("Microsoft.Update.Session")
            updateSearcher = updateSession.CreateupdateSearcher()
    
            Console.WriteLine("Searching for updates..." & vbCrLf)
    
            searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")
    
    
            Console.WriteLine("List of applicable items on the machine:")
    
            For I As Integer = 0 To searchResult.Updates.Count - 1
                update = searchResult.Updates.Item(I)
                Console.WriteLine(I + 1 & "> " & update.Title)
            Next
    
            If searchResult.Updates.Count = 0 Then
                Console.WriteLine("There are no applicable updates.")
                Exit Sub
            End If
    
            Console.WriteLine(vbCrLf & "Creating collection of updates to download:")
    
            updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
    
            For I As Integer = 0 To searchResult.Updates.Count - 1
                update = searchResult.Updates.Item(I)
                Console.WriteLine(I + 1 & "> adding: " & update.Title)
                updatesToDownload.Add(update)
            Next
    
            Console.WriteLine(vbCrLf & "Downloading updates...")
    
                 downloader = updateSession.CreateUpdateDownloader()
    
                downloader.updates = updatesToDownload
    
                downloader.Download()
    
    
    
            Console.WriteLine(vbCrLf & "List of downloaded updates:")
    
            For I As Integer = 0 To searchResult.Updates.Count - 1
                update = searchResult.Updates.Item(I)
                If update.IsDownloaded Then
                    Console.WriteLine(I + 1 & "> " & update.Title)
                End If
            Next
    
            updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
    
            Console.WriteLine(vbCrLf & "Creating collection of downloaded updates to install:")
    
            For I As Integer = 0 To searchResult.Updates.Count - 1
                update = searchResult.Updates.Item(I)
                If update.IsDownloaded = True Then
                    Console.WriteLine(I + 1 & "> adding:  " & update.Title)
                    updatesToInstall.Add(update)
                End If
            Next
    
            Console.WriteLine(vbCrLf & "Would you like to install updates now? (Y/N)")
            Dim strInput As String = Console.ReadLine
    
    
            If (strInput = "N" Or strInput = "n") Then
                Exit Sub
            ElseIf (strInput = "Y" Or strInput = "y") Then
                Console.WriteLine("Installing updates...")
                installer = updateSession.CreateUpdateInstaller()
                installer.Updates = updatesToInstall
                installationResult = installer.Install()
    
                'Output results of install
                Console.WriteLine("Installation Result: " & installationResult.ResultCode)
                Console.WriteLine("Reboot Required: " & installationResult.RebootRequired & vbCrLf)
                Console.WriteLine("Listing of updates installed " & "and individual installation results:")
    
                For I As Integer = 0 To updatesToInstall.Count - 1
                    Console.WriteLine(I + 1 & "> " & updatesToInstall.Item(I).Title & ": " & installationResult.GetUpdateResult(I).ResultCode)
                Next
            End If
    
        End Sub
    
    End Module
    可运行到downloader.updates = updatesToDownload的时候就报错,提示:System.Runtime.InteropServices.COMException (0x80020003): 找不到成员。 (异常来自 HRESULT:0x80020003 (DISP_E_MEMBERNOTFOUND)) 网上找了很多文章也看不出个所以然来,不知道错在哪里呢?
    2009年11月11日 6:51

答案