Skip to content

feat: Use WMI to implement Service related System APIs to reduce PowerShell overhead #389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions pkg/cim/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ import (
"github.com/microsoft/wmi/server2019/root/cimv2"
)

var (
BIOSSelectorList = []string{"SerialNumber"}
ServiceSelectorList = []string{"DisplayName", "State", "StartMode"}
)

type ServiceInterface interface {
GetPropertyName() (string, error)
GetPropertyDisplayName() (string, error)
GetPropertyState() (string, error)
GetPropertyStartMode() (string, error)
GetDependents() ([]ServiceInterface, error)
StartService() (result uint32, err error)
StopService() (result uint32, err error)
Refresh() error
}

// QueryBIOSElement retrieves the BIOS element.
//
// The equivalent WMI query is:
Expand All @@ -33,6 +49,11 @@ func QueryBIOSElement(selectorList []string) (*cimv2.CIM_BIOSElement, error) {
return bios, err
}

// GetBIOSSerialNumber returns the BIOS serial number.
func GetBIOSSerialNumber(bios *cimv2.CIM_BIOSElement) (string, error) {
return bios.GetPropertySerialNumber()
}

// QueryServiceByName retrieves a specific service by its name.
//
// The equivalent WMI query is:
Expand All @@ -55,3 +76,60 @@ func QueryServiceByName(name string, selectorList []string) (*cimv2.Win32_Servic

return service, err
}

// GetServiceName returns the name of a service.
func GetServiceName(service ServiceInterface) (string, error) {
return service.GetPropertyName()
}

// GetServiceDisplayName returns the display name of a service.
func GetServiceDisplayName(service ServiceInterface) (string, error) {
return service.GetPropertyDisplayName()
}

// GetServiceState returns the state of a service.
func GetServiceState(service ServiceInterface) (string, error) {
return service.GetPropertyState()
}

// GetServiceStartMode returns the start mode of a service.
func GetServiceStartMode(service ServiceInterface) (string, error) {
return service.GetPropertyStartMode()
}

// Win32Service wraps the WMI class Win32_Service (mainly for testing)
type Win32Service struct {
*cimv2.Win32_Service
}

func (s *Win32Service) GetDependents() ([]ServiceInterface, error) {
collection, err := s.GetAssociated("Win32_DependentService", "Win32_Service", "Dependent", "Antecedent")
if err != nil {
return nil, err
}

var result []ServiceInterface
for _, coll := range collection {
service, err := cimv2.NewWin32_ServiceEx1(coll)
if err != nil {
return nil, err
}

result = append(result, &Win32Service{
service,
})
}
return result, nil
}

type Win32ServiceFactory struct {
}

func (impl Win32ServiceFactory) GetService(name string) (ServiceInterface, error) {
service, err := QueryServiceByName(name, ServiceSelectorList)
if err != nil {
return nil, err
}

return &Win32Service{Win32_Service: service}, nil
}
Loading