DataType confusion with New-WebServiceProxy

Hey guys. This is a long one, but stick with me - I know I’m missing something silly. I am working with a SOAP HTTP API and I am getting this generic looking error when trying to update an object that has a member of type UserDefinedField[].

Here is an attempt to use (create and apply to my object “entity”) the datatype “UserDefinedField”, exposed in the API:

PS C:> $myService.GetType()

IsPublic IsSerial Name BaseType


True False ATWS System.Web.Services.Protocols.SoapHttpClientProtocol

PS C:> $type = $myService.GetType().Namespace
PS C:> $udf = New-Object -TypeName “$type`.UserDefinedField”
PS C:> $udf.Name = ‘Item’
PS C:> $udf.Value = ‘1’
PS C:> $udf

Name Value


Item1 1

PS C:> $entity.UserDefinedFields = $udf

The error that follows:

Exception setting “UserDefinedFields”: “Cannot convert the “Microsoft.PowerShell.Commands.NewWebserviceProxy.Autogenera
tedTypes.WebServiceProxy1k_net_atservices_1_5_atws_wsdl.UserDefinedField” value of type “Microsoft.PowerShell.Commands.
NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1k_net_atservices_1_5_atws_wsdl.UserDefinedField” to type “Microso
ft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1k_net_atservices_1_5_atws_wsdl.UserDefined
Field[]”.”

So, I get this kind-of. The API wants UserDefinedField[] and I’m only providing UserDefinedField.

So let’s try to pass $udf as an array:

PS C:> $udflist = @()
PS C:> $udflist += $udf
PS C:> $udflist

Name Value


Item1 1

PS C:> $entity.UserDefinedFields = $udflist

A new error:

Exception setting “UserDefinedFields”: “Cannot convert the “Microsoft.PowerShell.Commands.NewWebserviceProxy.Autogenera
tedTypes.WebServiceProxy1k_net_atservices_1_5_atws_wsdl.UserDefinedField” value of type “Microsoft.PowerShell.Commands.
NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1k_net_atservices_1_5_atws_wsdl.UserDefinedField” to type “Microso
ft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1k_net_atservices_1_5_atws_wsdl.UserDefined
Field”.”

So, this I do not understand at all. The API wants UserDefinedField and I’m providing UserDefinedField. Weird?

I dug a little deeper:

1) Existing datatype of the property, pulled from the API

PS C:> $entity.UserDefinedFields.gettype()

IsPublic IsSerial Name BaseType


True True UserDefinedField[] System.Array

2) udf datatype

PS C:> $udf.gettype()

IsPublic IsSerial Name BaseType


True True UserDefinedField System.Object

3) udf array datatype

PS C:> $udflist.gettype()

IsPublic IsSerial Name BaseType


True True Object[] System.Array

So basically, my $udf is a single “UserDefinedField” and not an array, and my attempt to make $udf an array turned it into an object. The API didn’t like either of these options; it expects “UserDefinedField[]”.

It was here that I actually saw my problem (and solved it - kind of). I found that I could bypass my problem by referencing a different object with the correct “UserDefinedField” already set and set my target entity.UserDefinedField equal to the reference.UserDefinedField. The API handled this with no errors and I saw the changes reflected in the cloud service.

BUT, this is hackey and I know there’s a better way to do this. What are some techniques/suggestions I can try to turn my $udf into an object that matches the datatype of $entity.UserDefinedFields?

Hmm, PowerShell’s usually good about automatically casting single objects to an array of their type, but maybe that’s not working with the SOAP adapter for some reason. Try this:

$type = $myService.GetType().Namespace
$udf = New-Object -TypeName "$type`.UserDefinedField"
$udf.Name = 'Item'
$udf.Value = '1'

$entity.UserDefinedFields = $udf -as $udf.GetType().MakeArrayType()

Hi Dave, .MakeArrayType() turned my $udf object into the correct “looking” datatype:


PS C:> $udf = $udf -as $udf.gettype().makearraytype()
PS C:> $udf.gettype()

IsPublic IsSerial Name BaseType


True True UserDefinedField[] System.Array

PS C:> $entity.UserDefinedFields = $udf

But, I’m getting the same error that I saw earlier:

Exception setting "UserDefinedFields": "Cannot convert the "Microsoft.PowerShell.Commands.NewWebserviceProxy.Autogenera tedTypes.WebServiceProxy1k_net_atservices_1_5_atws_wsdl.UserDefinedField" value of type "Microsoft.PowerShell.Commands. NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1k_net_atservices_1_5_atws_wsdl.UserDefinedField" to type "Microso ft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1k_net_atservices_1_5_atws_wsdl.UserDefined Field"." At line:1 char:1 + $entity.UserDefinedFields = $udf + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException + FullyQualifiedErrorId : ExceptionWhenSetting

Weird?

Hmm… I’m not sure what’s going on there. If I this API is something I can access myself, I can fiddle with it and see if I can find a way to get this working, but I don’t think I’ll be able to offer much help otherwise, at this point.

No worries - thanks for the input Dave.