go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/detector/windows/wmi_windows.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 //go:build windows 5 // +build windows 6 7 package windows 8 9 import ( 10 "errors" 11 "runtime" 12 "strconv" 13 14 wmi "github.com/StackExchange/wmi" 15 "go.mondoo.com/cnquery/providers/os/connection" 16 "go.mondoo.com/cnquery/providers/os/connection/shared" 17 ) 18 19 const wmiOSQuery = "SELECT Name, Caption, Manufacturer, OSArchitecture, Version, BuildNumber, Description, OSType, ProductType, SerialNumber FROM Win32_OperatingSystem" 20 21 func GetWmiInformation(conn shared.Connection) (*WmicOSInformation, error) { 22 // if we are running locally on windows, we want to avoid using powershell to be faster 23 if conn.Type() == connection.Local && runtime.GOOS == "windows" { 24 25 // we always get a list or entries 26 type win32_OperatingSystem struct { 27 Name *string 28 Caption *string 29 Manufacturer *string 30 OSArchitecture *string 31 Version *string 32 BuildNumber *string 33 Description *string 34 OSType *int 35 ProductType *int 36 } 37 38 // query wmi to retrieve information 39 var entries []win32_OperatingSystem 40 if err := wmi.Query(wmiOSQuery, &entries); err != nil { 41 return nil, err 42 } 43 44 if len(entries) != 1 || entries[0].Version == nil { 45 return nil, errors.New("could not query machine id on windows") 46 } 47 48 entry := entries[0] 49 return &WmicOSInformation{ 50 Name: toString(entry.Name), 51 Caption: toString(entry.Caption), 52 Manufacturer: toString(entry.Manufacturer), 53 OSArchitecture: toString(entry.OSArchitecture), 54 Version: toString(entry.Version), 55 BuildNumber: toString(entry.BuildNumber), 56 Description: toString(entry.Description), 57 OSType: intToString(entry.OSType), 58 ProductType: intToString(entry.ProductType), 59 }, nil 60 } 61 62 return powershellGetWmiInformation(conn) 63 } 64 65 func toString(s *string) string { 66 if s == nil { 67 return "" 68 } 69 return *s 70 } 71 72 func intToString(i *int) string { 73 if i == nil { 74 return "" 75 } 76 return strconv.Itoa(*i) 77 }