pwnyo
February 5, 2021, 12:05pm
1
I have the following code that generates my location data:
[pre]
$jsonip = Invoke-WebRequest http://ipinfo.io/json
$ipinfo = Convertfrom-JSON $jsonip.Content
$ipinfo.ip
$ipinfo.city
$ipinfo.region
$ipinfo.country
[/pre]
I would like to run this but have the data exported to a txt or csv.
Thankyou!
ferc
February 5, 2021, 12:52pm
2
Assuming the output of $ipinfo is correct and the properties you want to export are there, you can try something like this:
$Location = [pscustomobject]@{'IP' = $ipinfo.ip
'City' = $ipinfo.city
'Region' = $ipinfo.region
'Country' = $ipinfo.country}
$Location | Export-Csv -Path C:\Location.csv -NoTypeInformation
pwnyo
February 5, 2021, 1:15pm
3
thanks, tried that with this code:
[pre]
$jsonip = Invoke-WebRequest http://ipinfo.io/json
$ipinfo = Convertfrom-JSON $jsonip.Content
$Location = [pscustomobject]@{‘IP’ = $ipinfo.ip
‘City’ = $ipinfo.city
‘Region’ = $ipinfo.region
‘Country’ = $ipinfo.country}
$Location | Export-Csv -Path C:\temp\Location.csv -NoTypeInformation
[/pre]
however I get this error:
Cannot convert value to type “System.Management.Automation.LanguagePrimitives+InternalPSCustomObject”. Only core types
are supported in this language mode.
At line:3 char:1
$Location = [pscustomobject]@{‘IP’ = $ipinfo.ip
CategoryInfo : InvalidArgument: ( , RuntimeException
FullyQualifiedErrorId : ConversionSupportedOnlyToCoreTypes
Export-Csv : Cannot bind argument to parameter ‘InputObject’ because it is null.
At line:8 char:13
$Location | Export-Csv -Path C:\temp\Location.csv -NoTypeInformation
CategoryInfo : InvalidData: ( [Export-Csv], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCo
mmand
pwnyo
February 5, 2021, 1:16pm
4
thanks, tried that with this code:
[/pre]
$jsonip = Invoke-WebRequest http://ipinfo.io/json
$ipinfo = Convertfrom-JSON $jsonip.Content
$Location = [pscustomobject]@{‘IP’ = $ipinfo.ip
‘City’ = $ipinfo.city
‘Region’ = $ipinfo.region
‘Country’ = $ipinfo.country}
$Location | Export-Csv -Path C:\temp\Location.csv -NoTypeInformation
[pre]
however I get this error:
Cannot convert value to type “System.Management.Automation.LanguagePrimitives+InternalPSCustomObject”. Only core types
are supported in this language mode.
At line:3 char:1
$Location = [pscustomobject]@{‘IP’ = $ipinfo.ip
CategoryInfo : InvalidArgument: ( , RuntimeException
FullyQualifiedErrorId : ConversionSupportedOnlyToCoreTypes
Export-Csv : Cannot bind argument to parameter ‘InputObject’ because it is null.
At line:8 char:13
$Location | Export-Csv -Path C:\temp\Location.csv -NoTypeInformation
CategoryInfo : InvalidData: ( [Export-Csv], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCo
mmand
ferc
February 5, 2021, 4:16pm
5
It works for me. I am not sure why your code looks formatted like HTML but this is the script I just tested.
$jsonip = Invoke-WebRequest "http://ipinfo.io/json"
$ipinfo = Convertfrom-JSON $jsonip.Content
$Location = [pscustomobject]@{'IP' = $ipinfo.ip
'City' = $ipinfo.city
'Region' = $ipinfo.region
'Country' = $ipinfo.country}
$Location | Export-Csv -Path C:\Location.csv -NoTypeInformation
I got this output in csv format. When opened with Excel it looks like a regular spreadsheet with headers and one row of data.
"IP","City","Region","Country"
"80.79.80.24","Marlow","England","GB"
ferc
February 5, 2021, 4:20pm
6
It works for me. I am not sure why your code looks formatted like HTML but this is the script I just tested.
$jsonip = Invoke-WebRequest "http://ipinfo.io/json"
$ipinfo = Convertfrom-JSON $jsonip.Content
$Location = [pscustomobject]@{'IP' = $ipinfo.ip
'City' = $ipinfo.city
'Region' = $ipinfo.region
'Country' = $ipinfo.country}
$Location | Export-Csv -Path C:\Location.csv -NoTypeInformation
I got this output in csv format. When opened with Excel it looks like a regular spreadsheet with headers and one row of data.
"IP","City","Region","Country"
"80.79.80.24","Marlow","England","GB"
ferc
February 5, 2021, 4:21pm
7
It works for me. I am not sure why your code looks formatted like HTML but this is the script I just tested.
$jsonip = Invoke-WebRequest "http://ipinfo.io/json"
$ipinfo = Convertfrom-JSON $jsonip.Content
$Location = [pscustomobject]@{'IP' = $ipinfo.ip
'City' = $ipinfo.city
'Region' = $ipinfo.region
'Country' = $ipinfo.country}
$Location | Export-Csv -Path C:\Location.csv -NoTypeInformation
I got this output in csv format. When opened with Excel it looks like a regular spreadsheet with headers and one row of data.
"IP","City","Region","Country"
"80.79.80.24","Marlow","England","GB"
ferc
February 5, 2021, 4:22pm
8
It works for me. I am not sure why your code looks formatted like HTML but this is the script I just tested.
$jsonip = Invoke-WebRequest "http://ipinfo.io/json"
$ipinfo = Convertfrom-JSON $jsonip.Content
$Location = [pscustomobject]@{'IP' = $ipinfo.ip
'City' = $ipinfo.city
'Region' = $ipinfo.region
'Country' = $ipinfo.country}
$Location | Export-Csv -Path C:\Location.csv -NoTypeInformation
I got this output in csv format. When opened with Excel it looks like a regular spreadsheet with headers and one row of data.
“IP”,“City”,“Region”,“Country”
“80.79.80.24”,“Marlow”,“England”,“GB”
ferc
February 5, 2021, 4:26pm
9
I can’t post code in my replies to this thread, if I do the reply is either not submitted or not visible, not sure what is wrong. I am posting text formatted as code instead.
Anyway, your code works for me. I am not sure why it looks formatted like HTML but this is the script I just tested.
$jsonip = Invoke-WebRequest “http://ipinfo.io/json ”
$ipinfo = Convertfrom-JSON $jsonip.Content
$Location = [pscustomobject]@{‘IP’ = $ipinfo.ip
‘City’ = $ipinfo.city
‘Region’ = $ipinfo.region
‘Country’ = $ipinfo.country}
$Location | Export-Csv -Path C:\Location.csv -NoTypeInformation
I got this output in csv format. When opened with Excel it looks like a regular spreadsheet with headers and one row of data.
“IP”,“City”,“Region”,“Country”
“80.79.80.24”,“Marlow”,“England”,“GB”