通过Powershell通过Web服务使用复杂对象吗? [英] Using complex objects via a Web Service from Powershell?
问题描述
我一直在尝试通过Powershell(我运行的是4.0)利用供应商提供的Web服务系统。
以下是我用来设置代理以使用该服务的代码:
I've been trying to make use of a vendor supplied Web Service system via Powershell (I'm running 4.0). The following is the code I've used to set up the proxy to use the service :
$uri = http://somehost.employer.net:9999/AdministrationService?wsdl
$webSvc = New-WebServiceProxy -Uri $uri -namespace WebServiceProxy -Credential $TestCreds_NonPriv
当前,我对服务公开的两个方法 SaveAttributes和 GetAllAttributes感兴趣,
wsdl定义如下:
Currently, I'm interested in two methods that the service exposes, "SaveAttributes" and "GetAllAttributes", wsdl definitions as here:
<xsd:element name="SaveAttributes">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="Id" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="attributes" nillable="true" type="q5:ArrayOfKeyValueOfstringstring"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="GetAllAttributes">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="Id" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="GetAllAttributesResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="GetAllAttributesResult" nillable="true" type="q2:ArrayOfKeyValueOfstringstring"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
我已经通过以下方式成功使用了GetAllAttributes函数:
I've successfully used the GetAllAttributes function via :
$websvc.GetAllAttributes('someid')
这会返回一个对象,该对象是这样的键值对数组:
and this returns me an object which is an array of key value pairs like this :
Key Value
--- -----
ID 01
PERSONA godeater@stackoverflow.com
NAME GodEater's real name
如果要在Web Service上创建新对象,则必须使用
两个参数调用 SaveAttributes函数,这是一个新的 ID作为字符串和一个数组其余属性(
)的键/值对的集合,这就是我的困扰。我无法创建将成功将
序列化到服务的Powershell对象。我试过的是这样:
If I wish to create a new object on the Web Service, I have to call the "SaveAttributes" function with two parameters, a new "ID" as a string and an Array of Key/Value pairs for the rest of the attributes, and this is where I'm stumped. I'm unable to create a powershell object which gets successfully serialized to the service. What I tried was this:
[WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring[]]$newentity = @(
@{Key='PERSONA';Value='someone_else@stackoverflow.com'},
@{Key='NAME';Value='Someone Elses Real Name'}
)
$WebSvc.SaveAttributes('02',$newentity)
但是我得到的是这个相当困惑的错误:
But what I get this is this rather perplexing error:
Cannot convert argument "attributes", with value: "WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring[]", for "SaveAttributes" to type
"WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring[]": "Cannot convert the "WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring" value of type
"WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring" to type "WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring"."
At line:1 char:1
+ $WebSvc.SaveAttributes('02',$newentity)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
现在,查看GetAllAttributes返回给我的东西,它应该是同一类对象当我
尝试输入SaveAttributes方法的'attributes'参数时,似乎对
的序列化与我自己构造的对象略有不同。
Now, looking at what the GetAllAttributes returned to me, which should be the same sort of object as I try to feed into the 'attributes' argument of the SaveAttributes method, it seems to serialize slightly differently to the object I've constructed myself.
GetAllAttributes在序列化时的结果:
Result from GetAllAttributes when serialized:
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring[]</T>
<T>System.Array</T>
<T>System.Object</T>
</TN>
<LST>
<Obj RefId="1">
<TN RefId="1">
<T>WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring</T>
<T>System.Object</T>
</TN>
<ToString>WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring</ToString>
<Props>
<S N="Key">ID</S>
<S N="Value">01</S>
</Props>
</Obj>
<Obj RefId="2">
<TNRef RefId="1" />
<ToString>WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring</ToString>
<Props>
<S N="Key">PERSONA</S>
<S N="Value">godeater@stackoverflow.com</S>
</Props>
</Obj>
<Obj RefId="3">
<TNRef RefId="1" />
<ToString>WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring</ToString>
<Props>
<S N="Key">NAME</S>
<S N="Value">GodEater's real name</S>
</Props>
</Obj>
</LST>
</Obj>
</Objs>
虽然我创建的对象序列化为:
Whilst the object I've created serializes to this:
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>System.Collections.Hashtable</T>
<T>System.Object</T>
</TN>
<DCT>
<En>
<S N="Key">PERSONA</S>
<S N="Value"></S>
</En>
</DCT>
</Obj>
</Objs>
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>System.Collections.Hashtable</T>
<T>System.Object</T>
</TN>
<DCT>
<En>
<S N="Key">NAME</S>
<S N="Value">Someone Elses Real Name</S>
</En>
</DCT>
</Obj>
</Objs>
其中似乎缺乏环绕数组成员的包装,而似乎缺少
Which seems to lack the wrapping around the members of the array, and seems instead to have each item in the array as a standalone object.
任何人有任何想法吗?
推荐答案
尝试将键/值对强制为 WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring
类型:
Try forcing Key/Value pairs to be of the WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring
type:
$Properties = @(
@{Key='PERSONA';Value='someone_else@stackoverflow.com'},
@{Key='NAME';Value='Someone Elses Real Name'}
)
[WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring[]]$newentity = $Properties | ForEach-Object {
New-Object -TypeName 'WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring' -Property $_
}
$WebSvc.SaveAttributes('02',$newentity)
这篇关于通过Powershell通过Web服务使用复杂对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!