[英]Receive-Job not returning same type as calling the script block without a Job
I am loading an F# dll that defines certain types in a powershell script, in order to create a web request with a body to send it to a web service made in F#. 我正在加载一个在Powershell脚本中定义某些类型的F#dll,以便创建一个带有主体的Web请求,以将其发送到F#中制成的Web服务。 One of those types is the following: 这些类型之一是以下类型:
type Resource =
| VM of VMResource
| Unit of UnitResource
With 同
type VMResource = {
ComputerName: string
Ip: string
Attributes: string[]
}
type UnitResource = {
UnitName: string
Ip: string
Username: string
Password: string
Attributes: string[]
}
When I run the following small powershell snippet, the request's response is actually of type GetResourcesResponse
(which is a record type containing an Array of Resource
), which is what I want: 当我运行以下小型powershell代码片段时,请求的响应实际上是GetResourcesResponse
类型(这是包含Resource
Array的记录类型),这是我想要的:
Add-Type -Path "pathtomydll.dll"
$fullRequestUrl = "http://localhost:2121/Resources/Get"
$body = "{`"Id`":`"Test`",`"RequestedResources`":[{`"ResourceType`":{`"Case`":`"VM`"},`"Attributes`":[`"A1`",`"A2`"]},{`"ResourceType`":{`"Case`":`"Unit`"},`"Attributes`":[]}]}"
$resp = Invoke-WebRequest $fullRequestUrl -Method Post -Body $body -ContentType "application/json"
$obj = [ServerProtocolTypes+GetResourcesResponse]::FromJson($resp)
$obj.GetType() # GetResourcesResponse
Unfortunately, when I try to run the same code in a Job, I get a PSObject type with a property which is an Array of string
representation of my Resource
type (for example: ResourceTypes+Resource+VM
), which doesn't contain any information about the VMResource
or UnitResource
: 不幸的是,当我尝试在Job中运行相同的代码时,我得到了一个PSObject类型,其属性是我的Resource
类型的string
表示形式的数组(例如: ResourceTypes+Resource+VM
),其中不包含任何有关VMResource
或UnitResource
:
Add-Type -Path "pathtomydll.dll"
$fullRequestUrl = "http://localhost:2121/Resources/Get"
$body = "{`"Id`":`"Test`",`"RequestedResources`":[{`"ResourceType`":{`"Case`":`"VM`"},`"Attributes`":[`"A1`",`"A2`"]},{`"ResourceType`":{`"Case`":`"Unit`"},`"Attributes`":[]}]}"
$job = Start-Job -ScriptBlock { param($url, $reqBody) Add-Type -Path "pathtomydll.dll"; $resp = Invoke-WebRequest $url -Method Post -Body $reqBody -ContentType "application/json"; return [ServerProtocolTypes+GetResourcesResponse]::FromJson($resp) } -ArgumentList ($fullRequestUrl, $body)
Wait-Job $job
$obj = Receive-Job $job
$obj.GetType() # PSObject
In this case, $obj
is an array of string, with one entry being ResourceTypes+Resource+VM
and the other being ResourceTypes+Resource+Unit
. 在这种情况下, $obj
是一个字符串数组,一个条目是ResourceTypes+Resource+VM
,另一个条目是ResourceTypes+Resource+Unit
。
Is there any way for me to get back my GetResourcesResponse
object back from the Job instead of a PSObject
that contains an array of string
? 我有什么办法可以从Job取回GetResourcesResponse
对象,而不是从包含string
数组的PSObject
对象?
Per the comments: 根据评论:
I think the problem is that output from Jobs is serialized when it's returned. 我认为问题在于,乔布斯的输出在返回时会被序列化。 You can run parallel processes and retain the native object types by using runspaces instead of jobs. 您可以使用运行空间而不是作业来运行并行进程并保留本机对象类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.