locked
Trying to use PowerShell CIM cmdlets to make Software Center install pushed patches, but getting error: Invoke-CimMethod : Could not infer CimType from the provided .NET object. RRS feed

  • Question

  • The following was done an a Windows 2012 R2 server.::

    I have successfully used Get-wmiobject and Invoke-WmiMethod :::

    $MSpatches =  get-wmiobject -ComputerName $env:COMPUTERNAME -query "SELECT * FROM CCM_SoftwareUpdate" -namespace "ROOT\ccm\ClientSDK

    $MSpatches.count   was returned as 5

    $update_state = Invoke-WmiMethod -Class CCM_SoftwareUpdatesManager -namespace "ROOT\ccm\ClientSDK" -Name "InstallUpdates" -ArgumentList $MSpatches, $null

    The above caused Software Center to start the install of the patches.  YEA!! this worked.

    But, I want to use the CIM cmdlets:::

    I initially used the following to discover the CimClassPropertes, etc.  This class has no methods.

     $class = Get-CimClass -CimSession $CIMSess -ClassName CCM_SoftwareUpdate -namespace "ROOT\ccm\ClientSDK"

    I also used the following to discover the CimClassMethods, etc. for the class below.

    $class = Get-CimClass -CimSession $CIMSess -ClassName CCM_SoftwareUpdatesManager -namespace "ROOT\ccm\ClientSDK"

    I then prepared the following two lines of code to get the patches to install and then to cause them to be installed::

    $ccm_objects = Get-CimInstance -CimSession $CIMSess -ClassName CCM_SoftwareUpdate -Namespace "ROOT\ccm\ClientSDK"

    $ccm_objects.count  is  5 which is good!

    PS ..> $update_state = Invoke-CimMethod -Cimsession $CIMSess -ClassName CCM_SoftwareUpdatesManager -namespace "ROOT\ccm\ClientSDK" -MethodName "InstallUpdates" -Arguments @{ CCMUpdates = $ccm_objects }
    Invoke-CimMethod : Could not infer CimType from the provided .NET object.
    At line:1 char:17
    + $update_state = Invoke-CimMethod -Cimsession $CIMSess -ClassName CCM_SoftwareUpd ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Invoke-CimMethod], ArgumentException
        + FullyQualifiedErrorId : System.ArgumentException,Microsoft.Management.Infrastructure.CimCmdlets.InvokeCimMethodCommand

    QUESTION:   Is this second class not properly CIM enabled or am I doing something wrong?   I have looked and tried other configurations without success.  If CIM enabled what needs to be done to make this work??

    Thank You

    Fletch_bo2

    Friday, July 20, 2018 9:47 PM

Answers

  • This is a cleaner way to write code with  large number of parameters.

    Try{
        $parms1 = @{
            ClassName = 'CCM_SoftwareUpdate'
            Filter = 'ComplianceState = 0'
            NameSpace = 'ROOT\ccm\ClientSDK'
        }
        [ciminstance[]]$ccmupdates = Get-CimInstance @parms1 -ErrorAction Stop
    
        $parms2 = @{
            Namespace = 'ROOT\ccm\ClientSDK' 
            ClassName = 'CCM_SoftwareUpdatesManager'
            MethodName = 'InstallUpdates'
            Arguments = @{CCMUpdates = $ccmupdates}
        }
        Invoke-CimMethod @parms2 -ErrorAction Stop
    }
    Catch{
        Throw $_
    }


    \_(ツ)_/


    • Edited by jrv Friday, July 20, 2018 10:41 PM
    • Marked as answer by Bill_Stewart Tuesday, December 11, 2018 9:17 PM
    Friday, July 20, 2018 10:41 PM

All replies

  • Try it like this first:

    [array]$ccm_objects = Get-CimInstance -CimSession $CIMSess -ClassName CCM_SoftwareUpdate -Namespace ROOT\ccm\ClientSDK
    if($ccm_objects.Count -gt 0){
        $update_state = Invoke-CimMethod -Cimsession $CIMSess -ClassName CCM_SoftwareUpdatesManager -namespace ROOT\ccm\ClientSDK -MethodName InstallUpdates -Arguments @{ CCMUpdates = $ccm_objects }
    }
    


    \_(ツ)_/

    Friday, July 20, 2018 10:21 PM
  • See the following for issues regarding this class with CIM.

    https://powershell.org/forums/topic/converting-a-wmi-script-to-cim-sccm-2012-client/


    \_(ツ)_/

    Friday, July 20, 2018 10:25 PM
  • Ahh! Here:

    [ciminstance[]]$CMMissingUpdates = Get-CimInstance -Query "SELECT * FROM CCM_SoftwareUpdate WHERE ComplianceState = '0'" -Namespace ROOT\ccm\ClientSDK
    Invoke-CimMethod -Namespace ROOT\ccm\ClientSDK -ClassName CCM_SoftwareUpdatesManager -MethodName InstallUpdates -Arguments @{CCMUpdates = $CMMissingUpdates}


    \_(ツ)_/

    Friday, July 20, 2018 10:31 PM
  • This is a cleaner way to write code with  large number of parameters.

    Try{
        $parms1 = @{
            ClassName = 'CCM_SoftwareUpdate'
            Filter = 'ComplianceState = 0'
            NameSpace = 'ROOT\ccm\ClientSDK'
        }
        [ciminstance[]]$ccmupdates = Get-CimInstance @parms1 -ErrorAction Stop
    
        $parms2 = @{
            Namespace = 'ROOT\ccm\ClientSDK' 
            ClassName = 'CCM_SoftwareUpdatesManager'
            MethodName = 'InstallUpdates'
            Arguments = @{CCMUpdates = $ccmupdates}
        }
        Invoke-CimMethod @parms2 -ErrorAction Stop
    }
    Catch{
        Throw $_
    }


    \_(ツ)_/


    • Edited by jrv Friday, July 20, 2018 10:41 PM
    • Marked as answer by Bill_Stewart Tuesday, December 11, 2018 9:17 PM
    Friday, July 20, 2018 10:41 PM
  • JRV,

    THANK YOU!!

    I now understand how to get the cimtype to match.

    I am just doing research to make sure the function desired to be done can be done so I was not taking time to write clean or easier to read code, but THANK YOU!! again for pointing out the need for clean easy to read code. 

    I took your final post and took parts of it to see the results. 

    In whole it works as desired and starts the Install process.

    THANK YOU!!

    Tuesday, July 24, 2018 3:55 PM
  • The method here is not documented for PwoerShell in an obvious way.  You need to search for examples when presented with this ambiguity.  I had to search for about three minutes to understand why it wasn't working.


    \_(ツ)_/

    Tuesday, July 24, 2018 4:00 PM