[英]Receive-Job not returning same type as calling the script block without a Job
我正在加载一个在Powershell脚本中定义某些类型的F#dll,以便创建一个带有主体的Web请求,以将其发送到F#中制成的Web服务。 这些类型之一是以下类型:
type Resource =
| VM of VMResource
| Unit of UnitResource
同
type VMResource = {
ComputerName: string
Ip: string
Attributes: string[]
}
type UnitResource = {
UnitName: string
Ip: string
Username: string
Password: string
Attributes: string[]
}
当我运行以下小型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
不幸的是,当我尝试在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
在这种情况下, $obj
是一个字符串数组,一个条目是ResourceTypes+Resource+VM
,另一个条目是ResourceTypes+Resource+Unit
。
我有什么办法可以从Job取回GetResourcesResponse
对象,而不是从包含string
数组的PSObject
对象?
根据评论:
我认为问题在于,乔布斯的输出在返回时会被序列化。 您可以使用运行空间而不是作业来运行并行进程并保留本机对象类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.