Search code examples
powershellcim

Get-CIMInstance | Unable to include "-Query" parameter in splatted parameters


I have a script pulling some CIM information, running the command directly from the terminal (on the local machine, not a remote CIM session) is successful:

Get-CimInstance -Namespace root\sms\site_SITECODE -Query "SELECT SMS_Collection.* FROM SMS_FullCollectionMembership, SMS_Collection where Name = '$($hostName)' and SMS_FullCollectionMembership.CollectionID = SMS_Collection.CollectionID"

returns the correct info, everything working fine.

If I attempt to splatt the parameters like the following:

$curParams = @{
    Namespace = "root\sms\site_SITECODE"
    ClassName = "SMS_FullCollectionMembership"
    Query     = "SELECT SMS_Collection.* FROM SMS_FullCollectionMembership, SMS_Collection where Name = '$($hostname)' and SMS_FullCollectionMembership.CollectionID = SMS_Collection.CollectionID"

}

Get-CimInstance @curParams

The command fails with the error:

Get-CimInstance : Cannot bind parameter 'Query' to the target. Exception setting "Query": "Unable to resolve the parameter set name."
At C:\Program Files\WindowsPowerShell\Modules\ABC-MECM\ABC-MECM.psm1:321 char:21
+     Get-CimInstance @curParams
+                     ~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Get-CimInstance], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand

Through some brief testing it looks like the 'Query' parameter somehow does not exist when using splatted parameters?

In the documentation here:

https://learn.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-7.2

There are multiple 'syntax' options, only some of which contain the "Query" parameter.

Can anyone help explain why this error is occurring and in what cases a query is not accepted?

EDIT:

Of course I (at least partially) resolved this immediately after posting.

For some reason, including the 'classname' parameter while splatting caused the query to fail.

Removing that parameter resolved the issue.

Looks like just inconsistent behavior as the exact same command with all parameters completes from the terminal without splatting.

Maybe someone can explain why this behavior is different when executing the command with named parameters or splatting?


Solution

  • For some reason, including the 'classname' parameter while splatting caused the query to fail.

    Removing that parameter resolved the issue.

    Looks like just inconsistent behavior as the exact same command with all parameters completes from the terminal without splatting.