我正在尝试从CRM系统中导出特定的请求属性,以在excel中构建自定义报告。由于我无法直接访问它的sql服务器,因此我正在使用API。 API响应可以是XML或JSON。
我的第一笔查询基于我在CRM中设置的视图返回大约13个请求及其基本详细信息,然后我需要使用第二个查询来获取每个请求的其他字段。因此,我尝试创建一个数组,在其中可以存储所需的所有字段,然后可以将其导出到单个csv中。
但是,当我尝试使用ForEach-Object向阵列中添加新项目时,它只会一遍又一遍地覆盖同一项目。
也是在我第二次使用-Uri的Invoke-RestMethod时,我应该使用“当前”请求号,但是我不确定如何正确地引用它。
$inputdata = @"
{
"operation": {
"details": {
"from": "0",
"limit": "200",
"filterby": "9304_MyView"
}
}
}
"@
$Params = @{OPERATION_NAME='GET_REQUESTS';INPUT_DATA=$inputdata;TECHNICIAN_KEY='mykey';format='json'}
$Response = Invoke-RestMethod -Uri http://myserver/request -Method Post -Body $Params
$RequestNumber = $Response.operation.details.WORKORDERID
$current = $RequestNumber[0]
$array = @()
ForEach-Object -InputObject $RequestNumber {
$Parameter = @{OPERATION_NAME='GET_REQUEST';TECHNICIAN_KEY='mykey'}
$data = Invoke-RestMethod -Uri http://myserver/request/$current -Method GET -Body $Parameter
$object = New-Object -TypeName PSObject
$object | Add-Member -Name 'RequestNumber' -MemberType NoteProperty -Value $data.API.response.operation.Details.parameter.value[0]
$object | Add-Member -Name 'Subject' -MemberType NoteProperty -Value $data.API.response.operation.Details.parameter.value[12]
$array += @($object)
}
有人可以解释我在做什么错吗? *对不起,英语不是我的母语,但我希望它有一定道理。
基于Moerwald的建议的解决方案
我了解到,使用-InputObject参数将$ RequestNumber视为一个对象,并传递所有“票号”,因此我将数组创建移至$ RequestNumber上方,然后将$ RequestNumber传递给ForEach-Object。 / p>
$inputdata = @"
{
"operation": {
"details": {
"from": "0",
"limit": "200",
"filterby": "9304_MyView"
}
}
}
"@
$Params = @{OPERATION_NAME='GET_REQUESTS';INPUT_DATA=$inputdata;TECHNICIAN_KEY='mykey';format='json'}
$Response = Invoke-RestMethod -Uri http://myserver/request -Method Post -Body $Params
$array = @()
$RequestNumber = $Response.operation.details.WORKORDERID |
ForEach-Object {
$Parameter = @{OPERATION_NAME='GET_REQUEST';TECHNICIAN_KEY='mykey'}
$data = Invoke-RestMethod -Uri http://myserver/request/$_ -Method GET -Body $Parameter
$object = New-Object -TypeName PSObject
$object | Add-Member -Name 'RequestNumber' -MemberType NoteProperty -Value $data.API.response.operation.Details.parameter.value[0]
$object | Add-Member -Name 'Subject' -MemberType NoteProperty -Value $data.API.response.operation.Details.parameter.value[12]
$array += $object
}
答案 0 :(得分:0)
$current
始终指向$RequestNumber[0]
,因此$data
始终包含相同的数据。
$inputdata = @"
{
"operation": {
"details": {
"from": "0",
"limit": "200",
"filterby": "9304_MyView"
}
}
}
"@
$Params = @{OPERATION_NAME='GET_REQUESTS';INPUT_DATA=$inputdata; TECHNICIAN_KEY='mykey';format='json'}
$Response = Invoke-RestMethod -Uri http://myserver/request -Method Post -Body $Params
$RequestNumber = $Response.operation.details.WORKORDERID
$array = @()
ForEach-Object -InputObject $RequestNumber {
$Parameter = @{OPERATION_NAME='GET_REQUEST';TECHNICIAN_KEY='mykey'}
$data = Invoke-RestMethod -Uri http://myserver/request/$_ -Method GET -Body $Parameter
$object = New-Object -TypeName PSObject
$object | Add-Member -Name 'RequestNumber' -MemberType NoteProperty -Value $data.API.response.operation.Details.parameter.value[0]
$object | Add-Member -Name 'Subject' -MemberType NoteProperty -Value $data.API.response.operation.Details.parameter.value[12]
$array += $object
}
试图修复代码,请参见上文。我删除了$current
,并在$_
中使用了管道中的实际对象(= {Invoke-RestMethod
,about_pipelines)。我也将$array += @($object)
更改为$array += $object
,因为您想向该数组添加一个对象,而不是向该数组添加一个新的数组(@()
array sub-expression operator的含义是)。
希望有帮助。
答案 1 :(得分:0)
您每次通过构造一个新数组来重置$ array。为了简单起见,以下代码仅循环显示数字。注意:+ =从头开始构建数组,因此性能可能是个问题(有关原因,请参见FoxDeploy的详细答案)。
$array = @()
0..20 | ForEach-Object {
$object = $_
# Use += for adding to an array
$array += $object
}
Write $array
执行相同操作的更快方法是使用.Net ArrayList。将此答案归功于FoxDeploy。
$array = new-object System.Collections.ArrayList
0..20 | ForEach-Object {
$object = $_
# Call the Method Add here
$array.Add($object)
}
Write $array