循环CSV并通过REST API发布数据

时间:2017-11-06 13:02:56

标签: api powershell loops csv foreach

我有简单的CSV文件,如:

device,template
dlinkrouter,DLink DGS Switch
cisco, DLink DGS Switch

简单的脚本如下:

$csv = Import-Csv "C:\Users\m.zurek\Desktop\devices.csv"

$csv.device | ForEach-Object {
    Write-Host $_
    $action = "http://localhost:8090/api/json/ncmsettings/addDevice"
    $hash = @{}
    $hash.Add("apiKey", "cb464d185c0cdf785295a6a0a1d227a2")
    $hash.Add("IPADDRESS", $($_))
    $hash.Add("DEVICE_BEHAVIOUR", "DLink DGS Switch")
    $adddev = Invoke-WebRequest -Uri $action -UseBasicParsing -Body $hash -Method Post
    return $adddev
}

问题是该脚本仅添加CSV文件中的第一个设备。其余的我有API关于错误的回复。不确定这是API还是我的脚本的问题。

StatusCode        : 200
StatusDescription : 
Content           : {"Message":"Error in adding device. Zip the logs folder and send it to support.","isSuccess":false,"statusMsg":"Failure","bulkResourceIds":[]}

RawContent        : HTTP/1.1 200 
                Access-Control-Allow-Origin: *
                Access-Control-Allow-Methods: GET,POST
                Access-Control-Max-Age: 5000
                Transfer-Encoding: chunked
                Vary: Accept-Encoding
                Content-Type: application/json;c...
Forms             : 
Headers           : {[Access-Control-Allow-Origin, *], [Access-Control-Allow-Methods, GET,POST], [Access-Control-Max-Age, 5000], [Transfer-Encoding, chunked]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : 
RawContentLength  : 144

Write-Host正常工作并从CSV中写下所有设备的名称。

0 个答案:

没有答案