英文:
Select-Object -ExpandProperty multiple in powershell
问题
从REST API调用中,我试图使用以下代码将响应存储为CSV:
$response = Invoke-RestMethod 'http://api.ipstack.com/54.251.51.24?access_key=mykey&security=1&hostname=1' -Method 'GET' -Headers $headers |
Select-Object ip,hostname -ExpandProperty time_zone |
Select-Object ip,hostname,@{N='time_zone_code';E={$_.code}} -ExpandProperty location |
Select-Object ip,hostname,time_zone_code,@{N='location_geoname_id';E={$_.geoname_id}} -ExpandProperty languages |
Select-Object ip,hostname,location_geoname_id,time_zone_code,@{N='languages_native';E={$_.native}} |
Export-Csv C:\Users\Lenovo\Desktop\Danial\response.csv -NoTypeInformation -Append
它给我以下错误:
Select-Object : 无法找到属性 "location"。
在行:3字符:9
+ Select-Object ip,hostname,@{N='time_zone_code';E={$_.code}} - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (@{id=Australia/...e=1.132.104.84}:PSObject)
[Select-Object], PSArgumentException
+ FullyQualifiedErrorId :
ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
我尝试了很多方法,但不知道如何在Select-Object中展开多个属性。REST响应如下:
{
"ip": "54.251.51.24",
"hostname": "54.251.51.24",
"type": "ipv4",
"continent_code": "OC",
"continent_name": "Oceania",
"country_code": "AU",
"country_name": "Australia",
"region_code": "QLD",
"region_name": "Queensland",
"city": "Brisbane",
"zip": "4000",
"latitude": -27.467580795288086,
"longitude": 153.02789306640625,
"location": {
"geoname_id": 2174003,
"capital": "Canberra",
"languages": [
{
"code": "en",
"name": "English",
"native": "English"
}
],
"country_flag": "https://assets.ipstack.com/flags/au.svg",
"country_flag_emoji": "🇦🇺",
"country_flag_emoji_unicode": "U+1F1E6 U+1F1FA",
"calling_code": "61",
"is_eu": false
},
"time_zone": {
"id": "Australia/Brisbane",
"current_time": "2023-03-07T18:54:34+10:00",
"gmt_offset": 36000,
"code": "AEST",
"is_daylight_saving": false
}
}
我想将location和time_zone的所有列存储在CSV中,以便可读而不是System.Object。
英文:
From REST API call, I try to store the response in csv using below code
$response = Invoke-RestMethod 'http://api.ipstack.com/54.251.51.24?access_key=mykey&security=1&hostname=1' -Method 'GET' -Headers $headers |
Select-Object ip,hostname -ExpandProperty time_zone |
Select-Object ip,hostname,@{N='time_zone_code';E={$_.code}} -ExpandProperty location |
Select-Object ip,hostname,time_zone_code,@{N='location_geoname_id';E={$_.geoname_id}} -ExpandProperty languages |
Select-Object ip,hostname,location_geoname_id,time_zone_code,@{N='languages_native';E={$_.native}} |
Export-Csv C:\Users\Lenovo\Desktop\Danial\response.csv -NoTypeInformation -Append
it gives me below error
Select-Object : Property "location" cannot be found.
At line:3 char:9
+ Select-Object ip,hostname,@{N='time_zone_code';E={$_.code}} - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (@{id=Australia/...e=1.132.104.84}:PSObject)
[Select-Object], PSArgumentException
+ FullyQualifiedErrorId :
ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
I tried many things but don't know how to expand multiple property in Select-Object. Rest response is
{
"ip": "54.251.51.24",
"hostname": "54.251.51.24",
"type": "ipv4",
"continent_code": "OC",
"continent_name": "Oceania",
"country_code": "AU",
"country_name": "Australia",
"region_code": "QLD",
"region_name": "Queensland",
"city": "Brisbane",
"zip": "4000",
"latitude": -27.467580795288086,
"longitude": 153.02789306640625,
"location": {
"geoname_id": 2174003,
"capital": "Canberra",
"languages": [
{
"code": "en",
"name": "English",
"native": "English"
}
],
"country_flag": "https://assets.ipstack.com/flags/au.svg",
"country_flag_emoji": "🇦🇺",
"country_flag_emoji_unicode": "U+1F1E6 U+1F1FA",
"calling_code": "61",
"is_eu": false
},
"time_zone": {
"id": "Australia/Brisbane",
"current_time": "2023-03-07T18:54:34+10:00",
"gmt_offset": 36000,
"code": "AEST",
"is_daylight_saving": false
}
}
I want to store all columns of location and time_zone in csv as readable not System.Object
答案1
得分: 1
以下是您要翻译的部分:
- 设置一些测试数据
$json = @"
{
"ip": "54.251.51.24",
"hostname": "54.251.51.24",
"type": "ipv4",
"continent_code": "OC",
"continent_name": "Oceania",
"country_code": "AU",
"country_name": "Australia",
"region_code": "QLD",
"region_name": "Queensland",
"city": "Brisbane",
"zip": "4000",
"latitude": -27.467580795288086,
"longitude": 153.02789306640625,
"location": {
"geoname_id": 2174003,
"capital": "Canberra",
"languages": [
{
"code": "en",
"name": "English",
"native": "English"
}
],
"country_flag": "https://assets.ipstack.com/flags/au.svg",
"country_flag_emoji": "🇦🇺",
"country_flag_emoji_unicode": "U+1F1E6 U+1F1FA",
"calling_code": "61",
"is_eu": false
},
"time_zone": {
"id": "Australia/Brisbane",
"current_time": "2023-03-07T18:54:34+10:00",
"gmt_offset": 36000,
"code": "AEST",
"is_daylight_saving": false
}
}
"@
$data = $json | ConvertFrom-Json;
- 将数据格式化为适合CSV的格式
$formatted = $data | foreach-object {
[pscustomobject] [ordered] @{
"HostName" = $_.hostname
"Capital" = $_.location.capital
"Timezone" = $_.time_zone.code
}
}
# 用于参考的数据:
$formatted | format-table
# HostName Capital Timezone
# -------- ------- --------
# 54.251.51.24 Canberra AEST
- 转换格式化后的数据(如果要保存到磁盘,请使用
Export-Csv
代替):
$formatted | ConvertTo-Csv
# "HostName","Capital","Timezone"
# "54.251.51.24","Canberra","AEST"
英文:
It might be easier to simply create new objects from the data rows and export those rather than use Select-Object
:
- Set up some test data
$json = @"
{
"ip": "54.251.51.24",
"hostname": "54.251.51.24",
"type": "ipv4",
"continent_code": "OC",
"continent_name": "Oceania",
"country_code": "AU",
"country_name": "Australia",
"region_code": "QLD",
"region_name": "Queensland",
"city": "Brisbane",
"zip": "4000",
"latitude": -27.467580795288086,
"longitude": 153.02789306640625,
"location": {
"geoname_id": 2174003,
"capital": "Canberra",
"languages": [
{
"code": "en",
"name": "English",
"native": "English"
}
],
"country_flag": "https://assets.ipstack.com/flags/au.svg",
"country_flag_emoji": "🇦🇺",
"country_flag_emoji_unicode": "U+1F1E6 U+1F1FA",
"calling_code": "61",
"is_eu": false
},
"time_zone": {
"id": "Australia/Brisbane",
"current_time": "2023-03-07T18:54:34+10:00",
"gmt_offset": 36000,
"code": "AEST",
"is_daylight_saving": false
}
}
"@
$data = $json | ConvertFrom-Json;
- Format the data into a csv-friendly format
$formatted = $data | foreach-object {
[pscustomobject] [ordered] @{
"HostName" = $_.hostname
"Capital" = $_.location.capital
"Timezone" = $_.time_zone.code
}
}
# show the data for reference:
$formatted | format-table
# HostName Capital Timezone
# -------- ------- --------
# 54.251.51.24 Canberra AEST
- Convert the formatted data (use
Export-Csv
instead if you're saving to disk):
$formatted | ConvertTo-Csv
# "HostName","Capital","Timezone"
# "54.251.51.24","Canberra","AEST"
答案2
得分: 0
访问location
时出现错误,因为您在这里已经删除了该属性:
Select-Object ip,hostname -ExpandProperty time_zone |
将要在管道中使用的属性添加到所有前面的select-object
中。
英文:
You get an error on accessing location
because you already got rid of this property here:
Select-Object ip,hostname -ExpandProperty time_zone |
Add properties that you'll be using down the pipeline to all preceding select-object
答案3
得分: 0
我的问题已通过以下代码解决:
$properties=@('ip','hostname'
@{Name='time_zone_code';Expression={$_.time_zone.code}}
@{Name='location_geoname_id';Expression={$_.location.geoname_id}}
@{Name='location_languages_native';Expression={$_.location.languages.native}}
)
$response = Invoke-RestMethod 'http://api.ipstack.com/54.251.51.24?access_key=mykey&security=1&hostname=1' -Method 'GET' -Headers $headers |
Select-Object -Property $properties |
Export-Csv C:\Users\Lenovo\Desktop\Danial\response.csv -NoTypeInformation -Append
英文:
My issue resolved using below code
$properties=@('ip','hostname'
@{Name='time_zone_code';Expression={$_.time_zone.code}}
@{Name='location_geoname_id';Expression={$_.location.geoname_id}}
@{Name='location_languages_native';Expression={$_.location.languages.native}}
)
$response = Invoke-RestMethod 'http://api.ipstack.com/54.251.51.24?access_key=mykey&security=1&hostname=1' -Method 'GET' -Headers $headers |
Select-Object -Property $properties |
Export-Csv C:\Users\Lenovo\Desktop\Danial\response.csv -NoTypeInformation -Append
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论