Edit

Share via


Invoke-CMWmiQuery

Run a WMI query.

Syntax

ByWql (Default)

Invoke-CMWmiQuery
    [-Query] <String>
    [-Context <Hashtable>]
    [-Option <QueryOptions>]
    [-DisableWildcardHandling]
    [-ForceWildcardHandling]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

BySearch

Invoke-CMWmiQuery
    -ClassName <String>
    -Search <SmsProviderSearch>
    [-Context <Hashtable>]
    [-Option <QueryOptions>]
    [-DisableWildcardHandling]
    [-ForceWildcardHandling]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

Description

The Invoke-CMWmiQuery cmdlet runs a Windows Management Instrumentation (WMI) query. Unlike other query cmdlets or tools, with this cmdlet the connection and namespace is already set up for you.

You can also use this cmdlet to create a query with WMI Query Language (WQL). Configuration Manager uses WQL for queries in collections. WQL is similar to SQL, but still uses the SMS Provider, thus abides by role-based access controls.

Note

Run Configuration Manager cmdlets from the Configuration Manager site drive, for example PS XYZ:\>. For more information, see getting started.

Examples

Example 1: Run a WQL query

The first command creates a WQL query and stores it in the $WQL variable. The second command runs the query stored in the variable.

$WQL = @"
SELECT app.* FROM SMS_ApplicationLatest AS app
INNER JOIN SMS_CIContentPackage AS con ON app.CI_ID=con.CI_ID
INNER JOIN SMS_DistributionPoint AS srv ON con.PackageID=srv.PackageID
WHERE app.IsHidden = 0
"@

Invoke-CMWmiQuery -Query $WQL -Option Lazy

Example 2: Run a WMI query for device collections

The first command creates a search object and stores the object in the $Search variable.

The second command specifies that the search order is ascending by CollectionID.

The third command adds search parameters to the $Search object. In this case, the query searches for device collections.

The last command runs the query stored in $Search. It specifies SMS_Collection as the class that contains the CollectionID property.

$Search = [Microsoft.ConfigurationManagement.PowerShell.Provider.SmsProviderSearch]::new()
$Search.AddOrder("CollectionID", [Microsoft.ConfigurationManagement.PowerShell.Provider.SearchParameterOrderBy]::Asc)
$Search.Add("Name","DeviceCol*", $True)

Invoke-CMWmiQuery -Search $Search -ClassName "SMS_Collection" -Option Lazy

Example 3: Run a WMI query for sites by status

The first command clears the search parameters from any existing search object.

The second command adds search parameters to the $Search object. In this case, the query searches for sites.

The last command runs the query stored in $Search. It specifies SMS_Site as the class that contains the site Status property.

$Search.Clear()
$Search.Add("Status", $True)

Invoke-CMWmiQuery -ClassName "SMS_Site" -Search $Search

Example 4: Run a WMI query for sites by name

The first command clears the search parameters from any existing search object.

The second command adds search parameters to the $Search object. In this case, the query searches for sites.

The last command runs the query stored in $Search. It specifies SMS_Site as the class that contains the SiteName property.

$Search.Clear()
$Search.Add("SiteName", $null, [Microsoft.ConfigurationManagement.PowerShell.Provider.SearchParameterOperator]::NotEquals)

Invoke-CMWmiQuery -ClassName "SMS_Site" -Search $Search

Example 5: Run a WMI query for applications

The first command clears the search parameters from any existing search object.

The second command adds search parameters to the $Search object. In this case, the query searches for applications.

The last command runs the query stored in $Search. It specifies SMS_Application as the class that contains the site CI_ID property.

$Search.Clear()
$Search.AddAdhoc("CI_ID > 0")

Invoke-CMWmiQuery -ClassName "SMS_Application" -Search $Search -Option Lazy

Parameters

-ClassName

Specifies the Configuration Manager WMI class that you want to query.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

BySearch
Position:Named
Mandatory:True
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-Confirm

Prompts you for confirmation before running the cmdlet.

Parameter properties

Type:SwitchParameter
Default value:False
Supports wildcards:False
DontShow:False
Aliases:cf

Parameter sets

(All)
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-Context

Specify the WMI context as a hash table. It's a list of name/value pairs that are passed to a WMI provider that supports context information for a customized operation.

Parameter properties

Type:Hashtable
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

(All)
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-DisableWildcardHandling

This parameter treats wildcard characters as literal character values. You can't combine it with ForceWildcardHandling.

Parameter properties

Type:SwitchParameter
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

(All)
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-ForceWildcardHandling

This parameter processes wildcard characters and may lead to unexpected behavior (not recommended). You can't combine it with DisableWildcardHandling.

Parameter properties

Type:SwitchParameter
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

(All)
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-Option

The most common option is Fast.

Specify a query option:

  • None: Default
  • Lazy: By default, the cmdlet loads lazy properties.
  • Fast: Use this option to not load lazy properties. This option can return results more quickly for some classes.
  • ExpectResults & ExpectResultsThrowException: If the query returns no results, throw an exception. This exception typically ends a script.
  • FastExpectResults & LazyExpectResults: These options combine Fast and Lazy with ExpectResults.
  • ExpectResultsSoftFail: If the query returns no results, output an error, but don't end the script.

For more information about lazy properties, see Configuration Manager lazy properties.

The following values are for internal use only:

  • Clone
  • NoMask
  • NoRefresh
  • IgnoreNoResults

Parameter properties

Type:QueryOptions
Default value:None
Accepted values:None, NoRefresh, Lazy, Fast, ExpectResults, FastExpectResults, LazyExpectResults, Clone, ExpectResultsSoftFail, ExpectResultsThrowException, NoMask, IgnoreNoResults
Supports wildcards:False
DontShow:False
Aliases:Options

Parameter sets

(All)
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-Query

Specifies a WMI Query Language (WQL) statement.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

ByWql
Position:0
Mandatory:True
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

Specifies an SMSProviderSearch object.

Parameter properties

Type:SmsProviderSearch
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

BySearch
Position:Named
Mandatory:True
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet doesn't run.

Parameter properties

Type:SwitchParameter
Default value:False
Supports wildcards:False
DontShow:False
Aliases:wi

Parameter sets

(All)
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

Inputs

None

Outputs

IResultObject

IResultObject