Add-Member - ForEach Item从其他信息中获取价值

时间:2017-02-26 07:44:47

标签: windows powershell web

我有一个PowerShell脚本,其中包含以下方法:
1. Invoke-WebRequest :获取我的json文件 2. Invoke-RestMethod :从(1)获取所有ip信息。

我希望Add-Member$gip.city到每个r.users信息

这是我的错误语法:

$r = Invoke-WebRequest -Uri http://schoolfordesign.net/grabber.json | ConvertFrom-Json
$expip = $r.users  | select -expand ipaddress


ForEach($ips in $expip){

    $infoService = "http://ipinfo.io/$ips/json"
    $gip = Invoke-RestMethod -Method Get -URI $infoService

    $r.users| Add-Member -NotePropertyName city -NotePropertyValue $gip.city -Force
}

#Resilt:
$r.users

当我运行它时,新成员'City'不包含任何值,它显示了这个结果:
pic

2 个答案:

答案 0 :(得分:4)

来自http://schoolfordesign.net/grabber.json的初始JSON包含如下用户记录:

{
    "ipaddress": "66.249.93.218",
    "date": "2017\/02\/23",
    "time": "07:03:34pm",
    "device": "Mozilla\/5.0 (Windows NT 10.0; Win64; x64) ......."
}

最简单的解决方案是整体迭代这些记录,而不仅仅是IP:

$r = Invoke-WebRequest -Uri http://schoolfordesign.net/grabber.json | ConvertFrom-Json

ForEach ($user in $r.users) {
    $info = Invoke-RestMethod -Method Get -URI "http://ipinfo.io/$($user.ipaddress)/json"
    $user | Add-Member city $info.city -force
}

$r.users

答案 1 :(得分:1)

正如我之前所说,您需要在$users集合中查找正确的项目,以便将城市附加到:

$r = Invoke-WebRequest -Uri http://schoolfordesign.net/grabber.json | ConvertFrom-Json
$expip = $r.users  | select -expand ipaddress


ForEach($ips in $expip){

    $infoService = "http://ipinfo.io/$ips/json"
    $gip = Invoke-RestMethod -Method Get -URI $infoService
    $r.users| ?{ $_.ipaddress -eq $ips } | %{ Add-Member -InputObject $_ -NotePropertyName city -NotePropertyValue $gip.city -Force }
}

#Result:
$r.users

或者根据最初的评论中的建议单独迭代用户,并通过@wOxxOm进行很好的扩展

导致,(请注意,某些IP记录不会返回城市信息):

ipaddress : 66.249.93.218
date      : 2017/02/23
time      : 07:03:34pm
device    : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87
city      : 

ipaddress : 66.249.93.219
date      : 2017/02/23
time      : 11:18:38pm
device    : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87
city      : 

ipaddress : 95.84.129.28
date      : 2017/02/24
time      : 08:56:23am
device    : Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.5072
city      : Presnenskiy

ipaddress : 37.204.200.135
date      : 2017/02/24
time      : 12:59:51pm
device    : Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/7.0)
city      : Moscow

ipaddress : 66.249.93.217
date      : 2017/02/24
time      : 03:13:06pm
device    : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87
city      : 

ipaddress : 46.242.121.90
date      : 2017/02/24
time      : 08:27:51pm
device    : Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.450
city      : Moscow

ipaddress : 89.179.106.75
date      : 2017/02/24
time      : 09:15:15pm
device    : Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.3
city      : Rostov-on-Don

ipaddress : 176.193.111.146
date      : 2017/02/25
time      : 11:25:49am
device    : Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)
city      : Moscow

ipaddress : 37.204.142.237
date      : 2017/02/25
time      : 05:49:15pm
device    : Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.5072
city      : Moscow

ipaddress : 5.228.95.51
date      : 2017/02/25
time      : 07:47:42pm
device    : Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/7.0)
city      : Moscow

ipaddress : 109.63.236.189
date      : 2017/02/25
time      : 11:40:26pm
device    : Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.3
city      : Moscow

ipaddress : 176.195.116.125
date      : 2017/02/26
time      : 12:12:28am
device    : Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.450
city      : Andreyevka

ipaddress : 66.249.93.218
date      : 2017/02/26
time      : 04:05:01am
device    : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87
city      :