簡體   English   中英

如何在 python 中獲得待處理的 windows 更新?

[英]How do I get pending windows updates in python?

I am trying to get pending windows updates on python but no module returns me the pending windows updates, only windows update history, I don't need especifiation about the update I just need to know if there are pending updates or not, I'm嘗試使用此代碼:

from windows_tools.updates import get_windows_updates
import os

for update in get_windows_updates(filter_duplicates=True, include_all_states=False):
    print(update)

它返回:

    {'kb': None, 'date': '2022-01-14 20:18:21', 'title': '9PLFNLNT3G5G-AppUp.IntelGraphicsExperience', 'description': '9PLFNLNT3G5G-1152921505694231446', 'supporturl': '', 'operation': 'installation', 'result': 'succeeded'}
{'kb': None, 'date': '2022-01-14 20:18:21', 'title': '9NBLGGH3FRZM-Microsoft.VCLibs.140.00', 'description': '9NBLGGH3FRZM-1152921505694106457', 'supporturl': '', 'operation': 'installation', 'result': 'succeeded'}
{'kb': None, 'date': '2022-01-14 20:18:21', 'title': '9MW2LKJ0TPJF-Microsoft.NET.Native.Framework.2.2', 'description': '9MW2LKJ0TPJF-1152921505692414645', 'supporturl': '', 'operation': 'installation', 'result': 'succeeded'}
{'kb': None, 'date': '2022-01-14 20:18:21', 'title': '9PLL735RFDSM-Microsoft.NET.Native.Runtime.2.2', 'description': '9PLL735RFDSM-1152921505689378154', 'supporturl': '', 'operation': 'installation', 'result': 'succeeded'}
{'kb': None, 'date': '2022-01-14 20:18:15', 'title': 'HP Inc. - HIDClass - 2.1.16.30156', 'description': 'HP Inc. HIDClass  driver update released in  November 2021', 'supporturl': 'http://support.microsoft.com/select/?target=hub', 'operation': 'installation', 'result': 'succeeded'}
{'kb': None, 'date': '2022-01-14 20:18:03', 'title': 'Intel Corporation - Bluetooth - 20.100.7.1', 'description': 'Intel Corporation Bluetooth  driver update released in  July 2020', 'supporturl': 'http://support.microsoft.com/select/?target=hub', 'operation': 'installation', 'result': 'succeeded'}
{'kb': None, 'date': '2022-01-14 20:18:01', 'title': 'Intel Corporation - Extension - 12/16/2018 12:00:00 AM - 20.110.1.1', 'description': 'Intel Corporation Extension  driver update released in  December 2018', 'supporturl': 'http://support.microsoft.com/select/?target=hub', 'operation': 'installation', 'result': 'succeeded'}
{'kb': None, 'date': '2022-01-14 20:17:50', 'title': 'Intel Corporation - Display - 27.20.100.8681', 'description': 'Intel Corporation Display  driver update released in  September 2020', 'supporturl': 'http://support.microsoft.com/select/?target=hub', 'operation': 'installation', 'result': 'succeeded'}
{'kb': None, 'date': '2022-01-14 20:15:12', 'title': 'Realtek Semiconductor Corp. - MEDIA - 6.0.8940.1', 'description': 'Realtek Semiconductor Corp. MEDIA  driver update released in  April 2020', 'supporturl': 'http://support.microsoft.com/select/?target=hub', 'operation': 'installation', 'result': 'succeeded'}
{'kb': 'KB4591272', 'date': '2022-01-14 20:13:19', 'title': '2021-11 Atualização do Windows 10 Version 21H2 para sistemas baseados em x64 (KB4591272)', 'description': 'Instale esta atualização para resolver problemas no Windows. Para obter a lista completa dos problemas incluídos nesta atualização, consulte o artigo da Base de Dados de Conhecimento Microsoft associado. Talvez seja necessário reiniciar o computador após instalar este item.', 'supporturl': 'http://support.microsoft.com', 'operation': 'installation', 'result': 'succeeded'}
{'kb': 'KB5003791', 'date': '2021-10-06 00:00:00', 'title': None, 'description': 'Update', 'supporturl': 'https://support.microsoft.com/help/5003791', 'operation': None, 'result': None}
{'kb': 'KB5009636', 'date': '2022-01-20 00:00:00', 'title': None, 'description': 'Update', 'supporturl': None, 'operation': None, 'result': None}
{'kb': 'KB5005699', 'date': '2021-10-06 00:00:00', 'title': None, 'description': 'Security Update', 'supporturl': None, 'operation': None, 'result': None}

我得到了所有已安裝的更新,而不是待處理的更新,如何以編程方式找到待處理的更新。

我正在使用 python 3.10

python 中沒有解決方案,所以我做了一個 vbs 腳本並從我的 function 內部調用。 vbs 腳本是

Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()        
Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")


If searchResult.Updates.Count <> 0 Then 

For i = 0 To searchResult.Updates.Count - 1
    Set update = searchResult.Updates.Item(i)
    
Next
End If

Main

Sub Main()
    Dim result, fso, fs
    result = 1 / Cos(25)
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fs  = fso.CreateTextFile("output.txt", True)
    fs.Write searchResult.Updates.Count
    fs.Close
End Sub

它獲取待處理更新的數量,然后我像這樣在 function 內部調用

import subprocess, time, os


class update_monitor():
    def __init__(self):
        self.output='output.txt'
    
    def updates_restando(self):
        os.system(r'script.vbs')

        time.sleep(10)

        with open(self.output,'r') as file:
            for i in file:
                if i == '0':
                    print('Não há atualizações disponiveis')
                    return 'Não há atualizações disponiveis'
                else:
                    print('Existem atualizações pendentes')
                    return 'Existem atualizações pendentes'


a = update_monitor()
a.updates_restando()

這個解決方案工作得很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM