Newbie : Export webrequest data to txt

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!

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

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: (:slight_smile: [], 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: (:slight_smile: [Export-Csv], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCo
    mmand

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: (:slight_smile: [], 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: (:slight_smile: [Export-Csv], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCo
    mmand

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"

 

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"

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"

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”

 

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”

thanks, that worked!