簡體   English   中英

Receive-Job返回的類型與沒有Job的情況下調用腳本塊的類型不同

[英]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 ),其中不包含任何有關VMResourceUnitResource

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM