我正在使用从https://curl.haxx.se/下载的外部curl命令
目前,我正在尝试获取curl命令,以便将一些信息写入在我的计算机上本地运行的elasticsearch中。
在将数据传递给外部curl命令之前,我正在通过转换器-json运行该信息,以确保数据格式正确。
我遇到的问题是,任何有空格的字段都会立即被卷曲抛出,出现意外的输入结束错误,删除字段数据中的空间可以让它很好地运行。
$curlExe = "h:\powershell\esb\elastic\curl\curl.exe"
function global:elasticcall ([string] $elasticdata)
{
$elasticoutput = "h:\powershell\esb\elastic\elastic.txt"
$elastichost="http://localhost:9200/newtest4/filecopy/?pretty"
$elasticheader="content-type: application/json"
$elamethod="POST"
$jsonelasticdata = $elasticdata | ConvertTo-Json -Compress
$curlargs = $elastichost,
'-X',$elamethod,
'-d',$jsonelasticdata,
'-H',$elasticheader
write-host "Curl arguments in the entire string : " $curlargs
& $curlexe @curlargs
$elasticdata | Out-File $elasticoutput -Append
}
$timereceived="randomness"
$timesentconv="randomness2"
$name="testingspaces.txt"
$curlstatus=0
$elasticbody = '{"timereceived":"' + $timereceived + '","timesent":"' + $timesentconv + '","Filename":"' + $name + '","Status":"' + $curlstatus + '"}'
elasticcall $elasticbody
将变量$name更改为“测试spaces.txt”将生成错误。
发布于 2018-04-10 16:58:40
您正在使用当前的字符串$elasticbody
,您已经手动尝试将其格式化为json。
$timereceived = "randomness"
$timesentconv = "randomness2"
$name = "testing spaces.txt"
$curlstatus = 0
$elasticbody = '{"timereceived":"' + $timereceived + '","timesent":"' + $timesentconv + '","Filename":"' + $name + '","Status":"' + $curlstatus + '"}'
$elasticbody | ConvertTo-Json -Compress
这将返回如下内容,您可以看到文件名周围没有引号,并且字符串本身被包装在引号中。
结果:"\"timereceived\":\"randomness\",\"timesent\":\"randomness2\",\"Filename\":\"testing spaces.txt\",\"Status\":\"0\"}"
您应该尝试做一些列出以下内容的事情:
$GLOBAL:curlExe = "h:\powershell\esb\elastic\curl\curl.exe"
function global:elasticcall {
param (
$timereceived,
$timesentconv,
$name,
$curlstatus,
$elasticoutput = "h:\powershell\esb\elastic\elastic.txt",
$elastichost = "http://localhost:9200/newtest4/filecopy/?pretty",
$elasticheader = "content-type: application/json",
$elamethod = "POST"
)
$elasticdata = @{
timereceived = $timereceived
timesent = $timesentconv
Filename = $name
Status = $curlstatus
}
$jsonelasticdata = $elasticdata | ConvertTo-Json -Compress
$curlargs = $elastichost,
'-X', $elamethod,
'-d', $jsonelasticdata,
'-H', $elasticheader
write-host "Curl arguments in the entire string : " $curlargs
& $curlexe @curlargs
$elasticdata | Out-File $elasticoutput -Append
}
global:elasticcall -timereceived 'randomness' -timesentconv 'randomness2' -name 'testing spaces.txt' -curlstatus 0
您接受变量作为参数,并使用函数中的ConvertTo-Json
对它们进行格式化。它应该正确地处理字符串中的空格。
https://stackoverflow.com/questions/49758040
复制相似问题