none
Extract characters from txt RRS feed

  • Question

  • Hello, I am new to powershell and I need to grab a certain part of a text from .txt file as:

    ip         location                                                                                                                       
    --         --------                                                                                                                       
    75.82.30.5 @{country=RO; region=Bucuresti; city=Bucharest; lat=44.4354; lng=26.1033; postalCode=051527; timezone=+03:00; geonameId=683506}

    I need to grab RO part and make it a variable in order to used further 

    I have this as a code:

    New-Item -ItemType directory -Path C:\temp\
    cd C:\temp\
    Invoke-RestMethod -Method Get -Uri "https://geo.ipify.org/api/v1?apiKey=at_iNhGwrCTwXtgrIVk7wborCRQMpH5Q&ipAddress" > C:\temp\country.txt
    $Text = Get-Content -Path C:\temp\country.txt

    The project is to rename computer based on IP location + Serial number 

    Thank you

    Wednesday, August 21, 2019 1:31 PM

Answers

  • How about this?

    $Country = (Invoke-RestMethod -Method Get -Uri "https://geo.ipify.org/api/v1?apiKey=at_iNhGwrCTwXtgrIVk7wborCRQMpH5Q&ipAddress").location.country
    No need to save the output  of the Invoke-RestMethod to a file unless you need it for something else.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, August 21, 2019 2:46 PM