wmi

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Rendered for windows/amd64

Index

Constants

View Source
const (
	MSFTDiskClass           = "MSFT_Disk"
	MSFTStorageSettingClass = "MSFT_StorageSetting"

	// PartitionStyleUnknown indicates an unknown partition table format
	PartitionStyleUnknown = 0
	// PartitionStyleMBR indicates the disk uses Master Boot Record (MBR) format
	PartitionStyleMBR = 1
	// PartitionStyleGPT indicates the disk uses GUID Partition Table (GPT) format
	PartitionStyleGPT = 2

	// GPTPartitionTypeBasicData is the GUID for basic data partitions in GPT
	// Used for general purpose storage partitions
	GPTPartitionTypeBasicData = "{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}"
	// GPTPartitionTypeMicrosoftReserved is the GUID for Microsoft Reserved Partition (MSR)
	// Reserved by Windows for system use
	GPTPartitionTypeMicrosoftReserved = "{e3c9e316-0b5c-4db8-817d-f92df00215ae}"

	// ErrorCodeCreatePartitionAccessPathAlreadyInUse is the error code (42002) returned when the driver letter failed to assign after partition created
	ErrorCodeCreatePartitionAccessPathAlreadyInUse = 42002
)
View Source
const (
	MSFTiSCSITargetPortalClass = "MSFT_iSCSITargetPortal"
	MSFTiSCSITargetClass       = "MSFT_iSCSITarget"
	MSFTiSCSISessionClass      = "MSFT_iSCSISession"
)
View Source
const (
	SmbMappingStatusOK uint32 = iota
	SmbMappingStatusPaused
	SmbMappingStatusDisconnected
	SmbMappingStatusNetworkError
	SmbMappingStatusConnecting
	SmbMappingStatusReconnecting
	SmbMappingStatusUnavailable
)

Refer to https://learn.microsoft.com/en-us/previous-versions/windows/desktop/smb/msft-smbmapping

View Source
const (
	MSFTVolumeClass    = "MSFT_Volume"
	MSFTPartitionClass = "MSFT_Partition"

	FileSystemUnknown = 0
)
View Source
const (
	WMINamespaceCimV2   = "Root\\CimV2"
	WMINamespaceStorage = "Root\\Microsoft\\Windows\\Storage"
	WMINamespaceSmb     = "Root\\Microsoft\\Windows\\Smb"

	WBEM_S_FALSE = 0x00000001
)
View Source
const (
	MSFTSmbGlobalMappingClass = "MSFT_SmbGlobalMapping"
)
View Source
const (
	Win32ServiceClass = "Win32_Service"
)

Variables

View Source
var (
	DiskSelectorListForDiskNumberAndLocation = []string{"Number", "Location"}
	DiskSelectorListForPartitionStyle        = []string{"PartitionStyle"}
	DiskSelectorListForPathAndSerialNumber   = []string{"Path", "SerialNumber"}
	DiskSelectorListForIsOffline             = []string{"IsOffline"}
	DiskSelectorListForSize                  = []string{"Size"}
)
View Source
var (
	BIOSSelectorList    = []string{"SerialNumber"}
	ServiceSelectorList = []string{"DisplayName", "State", "StartMode"}
)
View Source
var (
	VolumeSelectorListForFileSystemType = []string{"FileSystemType"}
	VolumeSelectorListForStats          = []string{"UniqueId", "SizeRemaining", "Size"}
	VolumeSelectorListUniqueID          = []string{"UniqueId"}

	PartitionSelectorListObjectID = []string{"ObjectId"}
)
View Source
var (
	ErrNotFound      = errors.New("not found")
	ErrStopIteration = errors.New("stop iteration")

	DiscardOutputParameter = func(_ string, _ *ole.VARIANT) (interface{}, error) {
		return nil, nil
	}
	DiscardResult = func(_ *ole.VARIANT) error {
		return nil
	}
)
View Source
var (
	ISCSITargetPortalDefaultSelectorList = []string{"TargetPortalAddress", "TargetPortalPortNumber"}
)

Functions

func Associators

func Associators(obj *ole.IDispatch, assocClass, resultClass, role, resultRole string, fn func(*ole.IDispatch) error) error

Associators gets the associators of the given object.

func CallMethodOnWMIClass

func CallMethodOnWMIClass(namespace, class, methodName string, input map[string]interface{}, handler func(string, *ole.VARIANT) (interface{}, error)) (uint32, map[string]interface{}, error)

CallMethodOnWMIClass calls a method on a WMI class and returns the return value.

func ConnectISCSITarget

func ConnectISCSITarget(portalAddress string, portalPortNumber uint16, nodeAddress string, authType string, chapUsername *string, chapSecret *string) error

ConnectISCSITarget establishes a connection to an iSCSI target with optional CHAP authentication credential.

Refer https://learn.microsoft.com/en-us/previous-versions/windows/desktop/iscsidisc/msft-iscsitarget-connect for the WMI method definition.

func CreatePartition

func CreatePartition(disk *COMDispatchObject, params ...interface{}) error

CreatePartition creates a partition on a disk.

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/createpartition-msft-disk for the WMI method definition.

func DisconnectISCSITarget

func DisconnectISCSITarget(target *COMDispatchObject, sessionIdentifier string) error

DisconnectISCSITarget disconnects the specified session between an iSCSI initiator and an iSCSI target.

Refer https://learn.microsoft.com/en-us/previous-versions/windows/desktop/iscsidisc/msft-iscsitarget-disconnect for the WMI method definition.

func Enumerate

func Enumerate(obj *ole.IDispatch, fn func(item *ole.VARIANT) error) error

Enumerate enumerates the items of the given object.

Doc: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/automat/dispid-constants

func FlushVolume

func FlushVolume(volume *COMDispatchObject) error

FlushVolume flushes the cached data in the volume's file system to disk.

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-volume-flush for the WMI method definition.

func ForEach

func ForEach(items []*COMDispatchObject, fn func(*COMDispatchObject) error) error

func FormatVolume

func FormatVolume(volume *COMDispatchObject, params ...interface{}) error

FormatVolume formats the specified volume.

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/format-msft-volume for the WMI method definition.

func GetBIOSSerialNumber

func GetBIOSSerialNumber(bios *COMDispatchObject) (string, error)

GetBIOSSerialNumber returns the BIOS serial number.

func GetDiskLocation

func GetDiskLocation(disk *COMDispatchObject) (string, error)

GetDiskLocation returns the location of a disk.

func GetDiskNumber

func GetDiskNumber(disk *COMDispatchObject) (uint32, error)

GetDiskNumber returns the number of a disk.

func GetDiskPartitionStyle

func GetDiskPartitionStyle(disk *COMDispatchObject) (uint16, error)

GetDiskPartitionStyle returns the partition style of a disk.

func GetDiskPath

func GetDiskPath(disk *COMDispatchObject) (string, error)

GetDiskPath returns the path of a disk.

func GetDiskSerialNumber

func GetDiskSerialNumber(disk *COMDispatchObject) (string, error)

GetDiskSerialNumber returns the serial number of a disk.

func GetDiskSize

func GetDiskSize(disk *COMDispatchObject) (uint64, error)

GetDiskSize returns the size of a disk.

func GetISCSISessionIdentifier

func GetISCSISessionIdentifier(session *COMDispatchObject) (string, error)

GetISCSISessionIdentifier returns the identifier of an iSCSI session.

func GetISCSITargetNodeAddress

func GetISCSITargetNodeAddress(target *COMDispatchObject) (string, error)

GetISCSITargetNodeAddress returns the node address of an iSCSI target.

func GetPartitionDiskNumber

func GetPartitionDiskNumber(part *COMDispatchObject) (uint32, error)

GetPartitionDiskNumber retrieves the disk number associated with a given partition.

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partition for the WMI class definitions.

func GetPartitionOffset

func GetPartitionOffset(part *COMDispatchObject) (uint64, error)

GetPartitionOffset returns the starting offset of a partition on its disk, in bytes.

func GetPartitionSize

func GetPartitionSize(part *COMDispatchObject) (uint64, error)

GetPartitionSize returns the size of a partition.

func GetPartitionSupportedSize

func GetPartitionSupportedSize(part *COMDispatchObject) (sizeMin, sizeMax uint64, status string, err error)

GetPartitionSupportedSize retrieves the minimum and maximum sizes that the partition can be resized to using the ResizePartition method.

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partition-getsupportedsizes for the WMI method definition.

func GetServiceDisplayName

func GetServiceDisplayName(service ServiceInterface) (string, error)

GetServiceDisplayName returns the display name of a service.

func GetServiceName

func GetServiceName(service ServiceInterface) (string, error)

GetServiceName returns the name of a service.

func GetServiceStartMode

func GetServiceStartMode(service ServiceInterface) (string, error)

GetServiceStartMode returns the start mode of a service.

func GetServiceState

func GetServiceState(service ServiceInterface) (string, error)

GetServiceState returns the state of a service.

func GetSmbGlobalMappingStatus

func GetSmbGlobalMappingStatus(smb *COMDispatchObject) (uint32, error)

GetSmbGlobalMappingStatus returns the status of an SMB global mapping.

func GetVolumeFileSystemType

func GetVolumeFileSystemType(volume *COMDispatchObject) (uint16, error)

GetVolumeFileSystemType returns the file system type of a volume.

func GetVolumeSize

func GetVolumeSize(volume *COMDispatchObject) (uint64, error)

GetVolumeSize returns the size of a volume.

func GetVolumeSizeRemaining

func GetVolumeSizeRemaining(volume *COMDispatchObject) (uint64, error)

GetVolumeSizeRemaining returns the remaining size of a volume.

func GetVolumeUniqueID

func GetVolumeUniqueID(volume *COMDispatchObject) (string, error)

GetVolumeUniqueID returns the unique ID (object ID) of a volume.

func IgnoreNotFound

func IgnoreNotFound(err error) error

IgnoreNotFound returns nil if the error is nil or a "not found" error, otherwise returns the original error.

func InitializeCOMSecurity

func InitializeCOMSecurity()

InitializeCOMSecurity registers process-wide COM security for WMI/DCOM.

It is enabled by default and runs at most once per process (including from init). Set WMI_COM_SECURITY to false/0/no/off to skip. Errors (including missing permissions or an already initialized process) are logged and ignored; this function never panics.

func InitializeDisk

func InitializeDisk(disk *COMDispatchObject, partitionStyle int) error

InitializeDisk initializes a RAW disk with a particular partition style.

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/initialize-msft-disk for the WMI method definition.

func IsDiskOffline

func IsDiskOffline(disk *COMDispatchObject) (bool, error)

IsDiskOffline returns whether a disk is offline.

func IsEndOfEnum

func IsEndOfEnum(err error) bool

func IsISCSISessionPersistent

func IsISCSISessionPersistent(session *COMDispatchObject) (bool, error)

IsISCSISessionPersistent returns whether an iSCSI session is persistent.

func IsISCSITargetConnected

func IsISCSITargetConnected(target *COMDispatchObject) (bool, error)

IsISCSITargetConnected returns whether the iSCSI target is connected.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound returns true if it's a "not found" error.

func NewISCSITargetPortal

func NewISCSITargetPortal(targetPortalAddress string, targetPortalPortNumber uint16, initiatorInstanceName *string, initiatorPortalAddress *string, isHeaderDigest *bool, isDataDigest *bool) error

NewISCSITargetPortal creates a new iSCSI target portal.

Refer to https://learn.microsoft.com/en-us/previous-versions/windows/desktop/iscsidisc/msft-iscsitargetportal-new for the WMI method definition.

func NewSmbGlobalMapping

func NewSmbGlobalMapping(remotePath, username, password string, requirePrivacy bool) error

NewSmbGlobalMapping creates a new SMB global mapping to the remote path.

Refer to https://pkg.go.dev/github.com/microsoft/wmi/server2019/root/microsoft/windows/smb#MSFT_SmbGlobalMapping for the WMI class definition.

func ParseISCSITargetPortal

func ParseISCSITargetPortal(instance *COMDispatchObject) (string, uint16, error)

ParseISCSITargetPortal retrieves the portal address and port number of an iSCSI target portal.

func Query

func Query(namespace, query string, fn func(item *ole.IDispatch) error) error

Query queries the WMI objects with the given namespace and query. Callers must ensure COM is initialized on the current OS thread (e.g., via WithCOMThread). The callback fn receives an *ole.IDispatch that is only valid for the duration of the callback; callers must NOT store or use it after fn returns.

func QueryWithBuilder

func QueryWithBuilder(builder *QueryBuilder, fn func(item *ole.IDispatch) error) error

func RefreshDisk

func RefreshDisk(disk *COMDispatchObject) (string, error)

RefreshDisk Refreshes the cached disk layout information.

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-disk-refresh for the WMI method definition.

func RemoveISCSITargetPortal

func RemoveISCSITargetPortal(instance *COMDispatchObject) error

RemoveISCSITargetPortal removes an iSCSI target portal.

Refer to https://learn.microsoft.com/en-us/previous-versions/windows/desktop/iscsidisc/msft-iscsitargetportal-remove for the WMI method definition.

func RemoveSmbGlobalMappingByRemotePath

func RemoveSmbGlobalMappingByRemotePath(scope *Scope, remotePath string) error

RemoveSmbGlobalMappingByRemotePath removes an SMB global mapping matching to the remote path.

Refer to https://pkg.go.dev/github.com/microsoft/wmi/server2019/root/microsoft/windows/smb#MSFT_SmbGlobalMapping for the WMI class definition.

func RescanDisks

func RescanDisks() error

RescanDisks rescans all changes by updating the internal cache of software objects (that is, Disks, Partitions, Volumes) for the storage setting.

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-storagesetting-updatehoststoragecache for the WMI method definition.

func ResizePartition

func ResizePartition(part *COMDispatchObject, size uint64) (string, error)

ResizePartition resizes a partition.

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partition-resize for the WMI method definition.

func SetISCSISessionChapSecret

func SetISCSISessionChapSecret(mutualChapSecret string) error

SetISCSISessionChapSecret sets a CHAP secret key for use with iSCSI initiator connections.

Refer https://learn.microsoft.com/en-us/previous-versions/windows/desktop/iscsidisc/msft-iscsitarget-disconnect for the WMI method definition.

func SetPartitionState

func SetPartitionState(part *COMDispatchObject, online bool) (string, error)

SetPartitionState takes a partition online or offline.

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partition-online and https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partition-offline for the WMI method definition.

func UnregisterISCSISession

func UnregisterISCSISession(session *COMDispatchObject) error

UnregisterISCSISession unregisters the iSCSI session so that it is no longer persistent.

Refer https://learn.microsoft.com/en-us/previous-versions/windows/desktop/iscsidisc/msft-iscsisession-unregister for the WMI method definition.

func WithCOMThread

func WithCOMThread(fn func() error) error

WithCOMThread runs the given function `fn` on a locked OS thread with COM initialized using COINIT_MULTITHREADED.

This is necessary for using COM/OLE APIs directly (e.g., via go-ole), because COM requires that initialization and usage occur on the same thread.

It performs the following steps:

  • Locks the current goroutine to its OS thread
  • Calls ole.CoInitializeEx with COINIT_MULTITHREADED
  • Executes the user-provided function
  • Uninitializes COM
  • Unlocks the thread

Process-wide COM security is initialized once at package init (best-effort).

If COM initialization fails, or if the user's function returns an error, that error is returned by WithCOMThread.

func WithScope

func WithScope(fn func(*Scope) error) error

WithScope runs the given function with a new scope. It returns the error from the function.

func WithWMIService

func WithWMIService(namespace string, fn func(*ole.IDispatch) error) error

WithWMIService runs the given function with a WMI service.

It creates a new WMI service and calls the given function with it. The callback fn receives an *ole.IDispatch that is only valid for the duration of the callback; callers must NOT store or use it after fn returns.

Types

type COMDispatchObject

type COMDispatchObject struct {
	// contains filtered or unexported fields
}

COMDispatchObject is a wrapper around an ole.IDispatch object.

func FindPartitionsByVolume

func FindPartitionsByVolume(scope *Scope, volumes []*COMDispatchObject) ([]*COMDispatchObject, error)

FindPartitionsByVolume finds all partitions associated with the given volumes using MSFT_PartitionToVolume association.

WMI association MSFT_PartitionToVolume:

Partition                                                               | Volume
---------                                                               | ------
MSFT_Partition (ObjectId = "{1}\\WIN-8E2EVAQ9QSB\ROOT/Microsoft/Win...) | MSFT_Volume (ObjectId = "{1}\\WIN-8E2EVAQ9QS...

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partitiontovolume for the WMI class definition.

func FindVolumesByPartition

func FindVolumesByPartition(scope *Scope, partitions []*COMDispatchObject) ([]*COMDispatchObject, error)

FindVolumesByPartition finds all volumes associated with the given partitions using MSFT_PartitionToVolume association.

WMI association MSFT_PartitionToVolume:

Partition                                                               | Volume
---------                                                               | ------
MSFT_Partition (ObjectId = "{1}\\WIN-8E2EVAQ9QSB\ROOT/Microsoft/Win...) | MSFT_Volume (ObjectId = "{1}\\WIN-8E2EVAQ9QS...

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partitiontovolume for the WMI class definition.

func GetPartitionByVolumeUniqueID

func GetPartitionByVolumeUniqueID(scope *Scope, volumeID string) (*COMDispatchObject, error)

GetPartitionByVolumeUniqueID retrieves a specific partition from a volume identified by its unique ID.

func GetVolumeByDriveLetter

func GetVolumeByDriveLetter(scope *Scope, driveLetter string, partitionSelectorList []string) (*COMDispatchObject, error)

GetVolumeByDriveLetter retrieves a volume associated with a specific drive letter.

func ListDisks

func ListDisks(scope *Scope, selectorList []string) ([]*COMDispatchObject, error)

ListDisks retrieves information about all available disks.

The equivalent WMI query is:

SELECT [selectors] FROM MSFT_Disk

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-disk for the WMI class definition.

func ListDisksByTarget

func ListDisksByTarget(scope *Scope, target *COMDispatchObject) ([]*COMDispatchObject, error)

ListDisksByTarget find all disks associated with an iSCSITarget. It finds out the iSCSIConnections from MSFT_iSCSITargetToiSCSIConnection association, then locate MSFT_Disk objects from MSFT_iSCSIConnectionToDisk association.

WMI association MSFT_iSCSITargetToiSCSIConnection:

iSCSIConnection                                                     | iSCSITarget
---------------                                                     | -----------
MSFT_iSCSIConnection (ConnectionIdentifier = "ffffac0cacbff010-15") | MSFT_iSCSITarget (NodeAddress = "iqn.1991-05.com...

WMI association MSFT_iSCSIConnectionToDisk:

Disk                                                               | iSCSIConnection
----                                                               | ---------------
MSFT_Disk (ObjectId = "{1}\\WIN-8E2EVAQ9QSB\root/Microsoft/Win...) | MSFT_iSCSIConnection (ConnectionIdentifier = "fff...

Refer to https://learn.microsoft.com/en-us/previous-versions/windows/desktop/iscsidisc/msft-iscsiconnection for the WMI class definition.

func ListISCSITargetPortals

func ListISCSITargetPortals(scope *Scope, selectorList []string) ([]*COMDispatchObject, error)

ListISCSITargetPortals retrieves a list of iSCSI target portals.

The equivalent WMI query is:

SELECT [selectors] FROM MSFT_IscsiTargetPortal

Refer to https://learn.microsoft.com/en-us/previous-versions/windows/desktop/iscsidisc/msft-iscsitargetportal for the WMI class definition.

func ListISCSITargetsByTargetPortal

func ListISCSITargetsByTargetPortal(scope *Scope, portals []*COMDispatchObject) ([]*COMDispatchObject, error)

ListISCSITargetsByTargetPortal retrieves all iSCSI targets from the specified iSCSI target portal using MSFT_iSCSITargetToiSCSITargetPortal association.

WMI association MSFT_iSCSITargetToiSCSITargetPortal:

iSCSITarget                                                                  | iSCSITargetPortal
-----------                                                                  | -----------------
MSFT_iSCSITarget (NodeAddress = "iqn.1991-05.com.microsoft:win-8e2evaq9q...) | MSFT_iSCSITargetPortal (TargetPortalAdd...

Refer to https://learn.microsoft.com/en-us/previous-versions/windows/desktop/iscsidisc/msft-iscsitarget for the WMI class definition.

func ListISCSITargetsByTargetPortalAddressAndPort

func ListISCSITargetsByTargetPortalAddressAndPort(scope *Scope, address string, port uint16, selectorList []string) ([]*COMDispatchObject, error)

ListISCSITargetsByTargetPortalAddressAndPort retrieves ISCSI targets by address and port of an iSCSI target portal.

func ListPartitionsOnDisk

func ListPartitionsOnDisk(scope *Scope, diskNumber, partitionNumber uint32, selectorList []string) ([]*COMDispatchObject, error)

ListPartitionsOnDisk retrieves all partitions or a partition with the specified number on a disk.

The equivalent WMI query is:

SELECT [selectors] FROM MSFT_Partition
  WHERE DiskNumber = '<diskNumber>'
    AND PartitionNumber = '<partitionNumber>'

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partition for the WMI class definition.

func ListPartitionsWithFilters

func ListPartitionsWithFilters(scope *Scope, selectorList []string, filters ...Condition) ([]*COMDispatchObject, error)

ListPartitionsWithFilters retrieves all partitions matching with the conditions specified by query filters.

The equivalent WMI query is:

SELECT [selectors] FROM MSFT_Partition
  WHERE ...

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partition for the WMI class definition.

func ListVolumes

func ListVolumes(scope *Scope, selectorList []string) ([]*COMDispatchObject, error)

ListVolumes retrieves all available volumes on the system.

The equivalent WMI query is:

SELECT [selectors] FROM MSFT_Volume

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-volume for the WMI class definition.

func NewCOMDispatchObject

func NewCOMDispatchObject(obj *ole.IDispatch) *COMDispatchObject

NewCOMDispatchObject clones an ole.IDispatch object and returns a COMDispatchObject.

Ownership of the object is not transferred to the COMDispatchObject. Instead, it holds a new reference to the object.

func QueryBIOSElement

func QueryBIOSElement(scope *Scope, selectorList []string) (*COMDispatchObject, error)

QueryBIOSElement retrieves the BIOS element.

The equivalent WMI query is:

SELECT [selectors] FROM CIM_BIOSElement

Refer to https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/cim-bioselement for the WMI class definition.

func QueryDiskByNumber

func QueryDiskByNumber(scope *Scope, diskNumber uint32, selectorList []string) (*COMDispatchObject, error)

QueryDiskByNumber retrieves disk information for a specific disk identified by its number.

The equivalent WMI query is:

SELECT [selectors] FROM MSFT_Disk
  WHERE Number = '<diskNumber>'

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-disk for the WMI class definition.

func QueryFirstObjectWithBuilder

func QueryFirstObjectWithBuilder(scope *Scope, q *QueryBuilder) (*COMDispatchObject, error)

QueryFirstObjectWithBuilder queries the first object with the given query builder.

It returns the object and the error.

If the object is not found, it returns ErrNotFound.

If the query fails, it returns the error.

If the query is successful, it returns the object.

func QueryISCSISessionByTarget

func QueryISCSISessionByTarget(scope *Scope, target *COMDispatchObject) (*COMDispatchObject, error)

QueryISCSISessionByTarget retrieves the iSCSI session from the specified iSCSI target using MSFT_iSCSITargetToiSCSISession association.

WMI association MSFT_iSCSITargetToiSCSISession:

iSCSISession                                                                | iSCSITarget
------------                                                                | -----------
MSFT_iSCSISession (SessionIdentifier = "ffffac0cacbff010-4000013700000016") | MSFT_iSCSITarget (NodeAddress = "iqn.199...

Refer to https://learn.microsoft.com/en-us/previous-versions/windows/desktop/iscsidisc/msft-iscsisession for the WMI class definition.

func QueryISCSITarget

func QueryISCSITarget(scope *Scope, address string, port uint16, nodeAddress string) (*COMDispatchObject, error)

QueryISCSITarget retrieves the iSCSI target from the specified portal address, portal and node address.

func QueryISCSITargetPortal

func QueryISCSITargetPortal(scope *Scope, address string, port uint16, selectorList []string) (*COMDispatchObject, error)

QueryISCSITargetPortal retrieves information about a specific iSCSI target portal identified by its network address and port number.

The equivalent WMI query is:

SELECT [selectors] FROM MSFT_IscsiTargetPortal
  WHERE TargetPortalAddress = '<address>'
    AND TargetPortalPortNumber = '<port>'

Refer to https://learn.microsoft.com/en-us/previous-versions/windows/desktop/iscsidisc/msft-iscsitargetportal for the WMI class definition.

func QueryObjectsWithBuilder

func QueryObjectsWithBuilder(scope *Scope, q *QueryBuilder) ([]*COMDispatchObject, error)

QueryObjectsWithBuilder queries WMI objects with the given query builder.

func QueryServiceByName

func QueryServiceByName(scope *Scope, name string, selectorList []string) (*COMDispatchObject, error)

QueryServiceByName retrieves a specific service by its name.

The equivalent WMI query is:

SELECT [selectors] FROM Win32_Service

Refer to https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-service for the WMI class definition.

func QuerySmbGlobalMappingByRemotePath

func QuerySmbGlobalMappingByRemotePath(scope *Scope, remotePath string) (*COMDispatchObject, error)

QuerySmbGlobalMappingByRemotePath retrieves the SMB global mapping from its remote path.

The equivalent WMI query is:

SELECT [selectors] FROM MSFT_SmbGlobalMapping

Refer to https://pkg.go.dev/github.com/microsoft/wmi/server2019/root/microsoft/windows/smb#MSFT_SmbGlobalMapping for the WMI class definition.

func QueryVolumeByUniqueID

func QueryVolumeByUniqueID(scope *Scope, volumeID string, selectorList []string) (*COMDispatchObject, error)

QueryVolumeByUniqueID retrieves a specific volume by its unique identifier.

The equivalent WMI query is:

SELECT [selectors] FROM MSFT_Volume WHERE UniqueId = "<volumeID>"

Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-volume for the WMI class definition.

func (*COMDispatchObject) CallMethod

func (c *COMDispatchObject) CallMethod(name string, fn func(*ole.VARIANT) error, params ...interface{}) error

func (*COMDispatchObject) CallUint32

func (c *COMDispatchObject) CallUint32(name string, params ...interface{}) (uint32, error)

func (*COMDispatchObject) CallVoid

func (c *COMDispatchObject) CallVoid(name string, params ...interface{}) error

func (*COMDispatchObject) Clone

func (*COMDispatchObject) Dispatch

func (c *COMDispatchObject) Dispatch() *ole.IDispatch

func (*COMDispatchObject) GetAssociated

func (c *COMDispatchObject) GetAssociated(scope *Scope, assocClass, resultClass, role, resultRole string) ([]*COMDispatchObject, error)

func (*COMDispatchObject) GetBoolProperty

func (c *COMDispatchObject) GetBoolProperty(name string) (bool, error)

func (*COMDispatchObject) GetPropertyWithHandler

func (c *COMDispatchObject) GetPropertyWithHandler(name string, fn func(*ole.VARIANT) error) error

func (*COMDispatchObject) GetStringProperty

func (c *COMDispatchObject) GetStringProperty(name string) (string, error)

func (*COMDispatchObject) GetStringPropertyAsUint64

func (c *COMDispatchObject) GetStringPropertyAsUint64(name string) (uint64, error)

func (*COMDispatchObject) GetUint16Property

func (c *COMDispatchObject) GetUint16Property(name string) (uint16, error)

func (*COMDispatchObject) GetUint32Property

func (c *COMDispatchObject) GetUint32Property(name string) (uint32, error)

type Condition

type Condition struct {
	Field    string
	Operator string
	Value    any
}

Condition is a condition for a WMI query.

func FilterForPartitionOnDisk

func FilterForPartitionOnDisk(diskNumber uint32) Condition

FilterForPartitionOnDisk creates a WMI query filter to query a disk by its number.

func FilterForPartitionsOfTypeNormal

func FilterForPartitionsOfTypeNormal() Condition

FilterForPartitionsOfTypeNormal creates a WMI query filter for all non-reserved partitions.

func WithCondition

func WithCondition(field string, operator string, value any) Condition

WithCondition creates a new condition for a WMI query.

func (Condition) String

func (c Condition) String() string

type QueryBuilder

type QueryBuilder struct {
	Class      string
	Namespace  string
	Selectors  []string
	Conditions []Condition
}

QueryBuilder is a builder for WMI queries.

func NewQuery

func NewQuery(class string) *QueryBuilder

NewQuery creates a new QueryBuilder for a WMI class.

func (*QueryBuilder) Build

func (q *QueryBuilder) Build() string

func (*QueryBuilder) Select

func (q *QueryBuilder) Select(selectors ...string) *QueryBuilder

func (*QueryBuilder) WithCondition

func (q *QueryBuilder) WithCondition(field string, operator string, value any) *QueryBuilder

func (*QueryBuilder) WithConditions

func (q *QueryBuilder) WithConditions(conditions ...Condition) *QueryBuilder

func (*QueryBuilder) WithNamespace

func (q *QueryBuilder) WithNamespace(ns string) *QueryBuilder

type SafeVariant

type SafeVariant struct {
	// contains filtered or unexported fields
}

SafeVariant is a wrapper around an ole.VARIANT object.

func NewSafeVariant

func NewSafeVariant(v *ole.VARIANT) SafeVariant

func (SafeVariant) Bool

func (s SafeVariant) Bool() bool

func (SafeVariant) Clear

func (s SafeVariant) Clear() error

func (SafeVariant) Int

func (s SafeVariant) Int() int

func (SafeVariant) Int32

func (s SafeVariant) Int32() int32

func (SafeVariant) Int64

func (s SafeVariant) Int64() int64

func (SafeVariant) Raw

func (s SafeVariant) Raw() (interface{}, bool)

func (SafeVariant) String

func (s SafeVariant) String() string

func (SafeVariant) Uint16

func (s SafeVariant) Uint16() uint16

func (SafeVariant) Uint32

func (s SafeVariant) Uint32() uint32

func (SafeVariant) Uint64

func (s SafeVariant) Uint64() uint64

func (SafeVariant) Value

func (s SafeVariant) Value() (interface{}, error)

type Scope

type Scope struct {
	// contains filtered or unexported fields
}

Scope is a scope for a WMI operation to track the objects created during the operation.

It is used to ensure that the objects are released when the scope is closed.

func (*Scope) Close

func (s *Scope) Close()

Close closes the scope and releases the objects.

func (*Scope) Track

func (s *Scope) Track(obj *COMDispatchObject) *COMDispatchObject

Track tracks the given object in the scope.

It returns the object itself.

type ServiceInterface

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

type WMIError

type WMIError struct {
	Method  string
	Class   string
	Target  string
	Code    uint32
	Details string
}

WMIError is an error for a WMI operation.

func NewWMIError

func NewWMIError(class, method string, target *ole.IDispatch, code uint32) *WMIError

NewWMIError creates a new WMIError.

func (*WMIError) Error

func (e *WMIError) Error() string

type Win32Service

type Win32Service struct {
	*COMDispatchObject
}

Win32Service wraps the WMI class Win32_Service (mainly for testing)

func (*Win32Service) GetDependents

func (s *Win32Service) GetDependents(scope *Scope) ([]ServiceInterface, error)

func (*Win32Service) GetPropertyDisplayName

func (s *Win32Service) GetPropertyDisplayName() (string, error)

func (*Win32Service) GetPropertyName

func (s *Win32Service) GetPropertyName() (string, error)

func (*Win32Service) GetPropertyStartMode

func (s *Win32Service) GetPropertyStartMode() (string, error)

func (*Win32Service) GetPropertyState

func (s *Win32Service) GetPropertyState() (string, error)

func (*Win32Service) Refresh

func (s *Win32Service) Refresh(scope *Scope) error

func (*Win32Service) StartService

func (s *Win32Service) StartService() (uint32, error)

func (*Win32Service) StopService

func (s *Win32Service) StopService() (uint32, error)

type Win32ServiceFactory

type Win32ServiceFactory struct {
}

func (Win32ServiceFactory) GetService

func (impl Win32ServiceFactory) GetService(scope *Scope, name string) (ServiceInterface, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL