PowerShell – SCCM Cmdlet Library “Get-CMDeviceCollection : Specified cast is not valid.”

UPDATE: This issue has been resolved in the latest version here https://www.microsoft.com/en-us/download/details.aspx?id=46681

While doing some SCCM automation we bumped into an issue with the SCCM Cmdlet Library 5.0.8249.1128.

When you try to execute a workflow using PowerShell Remoting in SMA like this…

workflow test {

InlineScript{ $VerbosePreference = "Continue"

$ModuleName = (get-item $env:SMS_ADMIN_UI_PATH).parent.FullName + "\ConfigurationManager.psd1"

Import-Module $ModuleName

cd P01:

$DeviceCollection = Get-CMDeviceCollection -CollectionId "P010000C"

Return $DeviceCollection

} -PSComputerName "SERVERFQDN"

Get-CMDeviceCollection : Specified cast is not valid.
At test:3 char:3
+ CategoryInfo : NotSpecified: (:) [Get-CMDeviceCollection], InvalidCastException
+ FullyQualifiedErrorId : System.InvalidCastException,Microsoft.ConfigurationManagement.Cmdlets.Collections.Commands.GetDeviceCollectionCommand
+ PSComputerName : [SERVERFQDN]

After some investigation we could not determine the cause of it, so the last option was to rollback to Cmdlet Library Version and then everything worked fine. The problem exists if we provide a named parameter like -CollectionId or –Name the problem also exists in other Cmdlets and also in the latest version of SCCM 2016 (vNext). Microsoft has confirmed / fixed this issue and it will be available in the next version. I have filed this bug on connect.

I hope this helps!