Skip to content

Unable to mock SCCM/ConfigMgr IResultObjects #1467

Open
@g1bs0nsg

Description

@g1bs0nsg

1. General summary of the issue

Trying to write a test for a function which calls SCCM cmdlets, specifically Set-CMTSStepApplyOperatingSystem that has a strong typed parameter -ImagePackage which is expected be of type Microsoft.ConfigurationManagement.ManagementProvider.IResultObject. I've tried using PS Custom Objects, New-MockObject, etc and I always get the following error:

Cannot process argument transformation on parameter 'ImagePackage'.  Cannot convert the value of type "System.Management.Automation.PSCustomObject" to type "Microsoft.ConfigurationManagement.ManagementProvider.IResultObject"

If I run the command in a live environment and use Get-CMOperatingSystemImage to get the object that I pass to Set-CMTSStepApplyOperatingSystem and do $obj.gettype().FullName it comes back with Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine.WqlResultObject

If I use New-MockObject -Type ([Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine.WqlResultObject]) I get a similar argument transformation error as the above. I'm also unable to add any properties to this object, if after creating the Mock Object I do a gettype it comes back with the following error:

[3:58 PM] C:\Temp\Scripts > $obj = New-MockObject -Type ([Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine.WqlResultObject])
[3:58 PM] C:\Temp\Scripts > $obj.gettype().FullName
The adapter cannot get property "GetType" for "Properties are being masked for this object".
At line:1 char:1
+ $obj.gettype().FUllName
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
    + FullyQualifiedErrorId : PSPropertyAdapter.GetProperty

If I try to use Microsoft.ConfigurationManagement.ManagementProvider.IResultObject as the type for New-MockObject like the error message suggests, I get the following:

[3:58 PM] C:\Temp\Scripts > $obj = New-MockObject -Type ([Microsoft.ConfigurationManagement.ManagementProvider.IResultObject])
Exception calling "GetUninitializedObject" with "1" argument(s): "Cannot create an abstract class."
At C:\Program Files\WindowsPowerShell\Modules\Pester\4.10.1\Functions\New-MockObject.ps1:28 char:5
+     [System.Runtime.Serialization.Formatterservices]::GetUninitialize ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : MemberAccessException

2. Describe Your Environment

Pester version : 4.10.1 C:\Program Files\WindowsPowerShell\Modules\Pester\4.10.1\Pester.psd1
PowerShell version : 5.1.17763.1007
OS version : Microsoft Windows NT 10.0.17763.0

3. Expected Behavior

I'm not sure how to proceed to get a test that properly passes under these circumstances.... is there a way to have the mocked command ignore a parameter? This is what is being called from within the function:

Set-CMTaskSequenceStepApplyOperatingSystem -TaskSequenceName $TS.Name -StepName $step -ImagePackage $WIM -ImagePackageIndex 1

$WIM is the result of using Get-CMOperatingSystemImage to return all Operating System Image files from SCCM and then filtering based on some criteria to narrow down to one to use with Set-CMTaskSequenceStepApplyOperatingSystem to update the step in an SCCM task sequence.

If I could just make it ignore -ImagePackage in some way then that would work.

There is also the ConvertTo-CMIResultObject cmdlet that takes an inputobject and converts to an IResult object, but everything I tried to feed it via PS Custom Object or Mock Objects failed. Seems to only convert WMIObjects... not entirely sure how I could mock one of those to feed it either.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions