Skip to content

Commit 3804ca7

Browse files
authored
Merge pull request #389 from laozc/wmi-process-system-api-service
feat: Use WMI to implement Service related System APIs to reduce PowerShell overhead
2 parents de04a0b + fc1bc4c commit 3804ca7

File tree

3 files changed

+554
-31
lines changed

3 files changed

+554
-31
lines changed

pkg/cim/system.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ import (
1010
"github.com/microsoft/wmi/server2019/root/cimv2"
1111
)
1212

13+
var (
14+
BIOSSelectorList = []string{"SerialNumber"}
15+
ServiceSelectorList = []string{"DisplayName", "State", "StartMode"}
16+
)
17+
18+
type ServiceInterface interface {
19+
GetPropertyName() (string, error)
20+
GetPropertyDisplayName() (string, error)
21+
GetPropertyState() (string, error)
22+
GetPropertyStartMode() (string, error)
23+
GetDependents() ([]ServiceInterface, error)
24+
StartService() (result uint32, err error)
25+
StopService() (result uint32, err error)
26+
Refresh() error
27+
}
28+
1329
// QueryBIOSElement retrieves the BIOS element.
1430
//
1531
// The equivalent WMI query is:
@@ -33,6 +49,11 @@ func QueryBIOSElement(selectorList []string) (*cimv2.CIM_BIOSElement, error) {
3349
return bios, err
3450
}
3551

52+
// GetBIOSSerialNumber returns the BIOS serial number.
53+
func GetBIOSSerialNumber(bios *cimv2.CIM_BIOSElement) (string, error) {
54+
return bios.GetPropertySerialNumber()
55+
}
56+
3657
// QueryServiceByName retrieves a specific service by its name.
3758
//
3859
// The equivalent WMI query is:
@@ -55,3 +76,60 @@ func QueryServiceByName(name string, selectorList []string) (*cimv2.Win32_Servic
5576

5677
return service, err
5778
}
79+
80+
// GetServiceName returns the name of a service.
81+
func GetServiceName(service ServiceInterface) (string, error) {
82+
return service.GetPropertyName()
83+
}
84+
85+
// GetServiceDisplayName returns the display name of a service.
86+
func GetServiceDisplayName(service ServiceInterface) (string, error) {
87+
return service.GetPropertyDisplayName()
88+
}
89+
90+
// GetServiceState returns the state of a service.
91+
func GetServiceState(service ServiceInterface) (string, error) {
92+
return service.GetPropertyState()
93+
}
94+
95+
// GetServiceStartMode returns the start mode of a service.
96+
func GetServiceStartMode(service ServiceInterface) (string, error) {
97+
return service.GetPropertyStartMode()
98+
}
99+
100+
// Win32Service wraps the WMI class Win32_Service (mainly for testing)
101+
type Win32Service struct {
102+
*cimv2.Win32_Service
103+
}
104+
105+
func (s *Win32Service) GetDependents() ([]ServiceInterface, error) {
106+
collection, err := s.GetAssociated("Win32_DependentService", "Win32_Service", "Dependent", "Antecedent")
107+
if err != nil {
108+
return nil, err
109+
}
110+
111+
var result []ServiceInterface
112+
for _, coll := range collection {
113+
service, err := cimv2.NewWin32_ServiceEx1(coll)
114+
if err != nil {
115+
return nil, err
116+
}
117+
118+
result = append(result, &Win32Service{
119+
service,
120+
})
121+
}
122+
return result, nil
123+
}
124+
125+
type Win32ServiceFactory struct {
126+
}
127+
128+
func (impl Win32ServiceFactory) GetService(name string) (ServiceInterface, error) {
129+
service, err := QueryServiceByName(name, ServiceSelectorList)
130+
if err != nil {
131+
return nil, err
132+
}
133+
134+
return &Win32Service{Win32_Service: service}, nil
135+
}

0 commit comments

Comments
 (0)