Invoke-RestMethod传递带有哈希表值的参数

时间:2019-02-28 16:00:32

标签: rest powershell

我一直在寻找高低之间,尝试各种变化,但我似乎无法正确理解。

我使用的是RESTful API,参数是“ log_data”,值是哈希表@ {}。我不断收到来自API的错误消息,内容为“必须使用body参数填充请求正文”。

这就是我要尝试的:

$JSONHashTable = 
@{
"auto_discovered"= "false"
"coalesce_events"= "true"
"credibility"= 5
"description"= "Type @ SERVERNAME"
"enabled"= "true"
"gateway"= "false"
"group_ids"= @(11111)
"internal"= "true"
"language_id"= 1
"log_extension_id"= 153
"name"= "Type @ SERVERNAME"
"protocol_parameters"= @(
@{
 id= '41040'
 name= "RootDirectory"
 value= "C:\Temp\"
}
@{
 id= '41060'
 name= "MonitoringAlgorithm"
 value= "Continuous Monitoring"
}
@{
 id= '41080'
 name= "OnlyMonitorFilesCreatedToday"
 value= "false"
}
@{
 id= '41100'
 name= "FileReaderPolicy"
 value= "UnicodeLogFile"
}
@{
 id= '41000'
 name= "identifier"
 value= "192.168.8.6"
}
@{
 id= '41005'
 name= "Local_System"
 value= "true"
}
@{
 id= '41050'
 name= "FilenamePattern"
 value= "locallog.*\.txt"
}
@{
 id= '41090'
 name= "FileMonitorPolicy"
 value= "FileMonitorNoFSRedirect"
}
@{
 id= '41120'
 name= "ThrottleTimeout"
 value= "Wi @ SERVERNAME"
}
@{
 id= '41110'
 name= "WinInstanceName"
 value= "5000"
}
)
"protocol_type_id"= 41
"requires_deploy"= "true"
"store_event_payload"= "true"
"target_collector_id"= 235
"type_id"= 39
"win_internal_destination_id"= 14
}

$body = @{"log_data"=$JSONHashTable} | ConvertTo-Json 
$LogRequest = Invoke-RestMethod -Method POST -body $body -Uri $LogURI -Header @{"SEC"= $apiKey }

1 个答案:

答案 0 :(得分:2)

您要提供给ConvertTo-Json的哈希表的深度为3层。默认情况下,ConvertTo-Json仅转换前2层,因此您的身体没有完全转换。您可以手动更改所需的深度:

$body = @{"log_data"=$JSONHashTable} | ConvertTo-Json -Depth 3