# Copyright 2026 SigmaSRC, Inc. All Rights Reserved.

"""Gather script for Windows hardware info using COM extensions."""


import logging
from win32com.client import GetObject

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

def Gather(collector):
    # write header to output
    table = collector.table("hardware", ["vendor", "model", "laptop"])
    vendor = ""
    model = ""
    laptop = "0"

    try:
        # get the vendor and model
        objItems = GetObject("WinMgMts://./root/cimv2").InstancesOf("Win32_ComputerSystem")
        for item in objItems:
            vendor = item.Properties_("Manufacturer").Value
            if vendor.lower() == "system manufacturer":
                vendor = "Unknown"
            model = item.Properties_("Model").Value
            if model.lower() == "product name":
                model = "Unknown"
    except:
        logger.exception("winhw gather failed for vendor and model")

    try:
        # get the chassis type
        colChassis = GetObject("WinMgMts://./root/cimv2").InstancesOf("Win32_SystemEnclosure")
        for objArray in colChassis:
            for item in objArray.Properties_("ChassisTypes").Value:
                if (item == 8 or item == 9 or item == 10 or item == 11 or item == 14):
                    laptop = "1"
    except:
        logger.exception("winhw gather failed for chassis")

    try:
        if laptop == "0":
            items = GetObject("WinMgMts://./root/cimv2").InstancesOf("Win32_Processor")
            for item in items:
                cpu = item.Properties_("Name").Value
                if cpu.lower().find("mobile") >= 0:
                    laptop = "1"
                    break
    except:
        logger.exception("winhw gather failed for Win32_Processor")

    table.add((vendor, model, laptop))

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

if __name__ == "__main__":
    _test()
