# Copyright 2026 SigmaSRC, Inc. All Rights Reserved.

"""Gather script for Windows service pack info using COM extension."""


import logging
from win32com.client import GetObject

logger = logging.getLogger("gather.gather_winsp")

def Gather(collector):
    # write header to output
    table = collector.table("winsp", ["spname", "spmajor", "spminor"])
    try:
        objItems = GetObject("WinMgMts://./root/cimv2").InstancesOf("Win32_OperatingSystem")
        for item in objItems:
            ver = item.Properties_("CSDVersion").Value
            spmajorver = item.Properties_("ServicePackMajorVersion").Value
            spminorver = item.Properties_("ServicePackMinorVersion").Value
            if not ver:
                ver = spmajorver = spminorver = ""
            table.add([ver, spmajorver, spminorver])

    except:
        logger.exception("winsp gather failed")

def _test():
    from sigma.gather import gathering
    gathering.test(Gather)

if __name__ == "__main__":
    _test()
