I have looked at similar questions and not finding answers that apply here. I have also been reading and digging around on docs.microsoft.com. Apologies if this is answered before and I could not locate it.
A geolocation API with a CLI is not working when I feed it a variable in Powershell.
The command: (site, API key and IP are changed for this post)
curl.exe 'https://api.xyz.com/ipgeo?apiKey=XXXX&ip=4.3.2.1'
The script as I have it:
[string]$lookup = $Args[0]
echo $lookup
echo "$lookup"
curl.exe 'https://api.xyz.com/ipgeo?apiKey=XXXX&ip="$lookup"'
I run it from powershell with geo 4.3.2.1
At first I did not use the [string] variable type notation but after it was not working and I did some reading on docs.microsoft.com I realized I wanted this treated as a string so I added that.
The echo statements are just me checking how the variable is being handled and will be removed from final script. Both of them display the IPv4 address properly.
I have tried the script originally with
curl.exe 'https://api.xyz.com/ipgeo?apiKey=XXXX&ip=$lookup'
then added the double quotes around variable. Neither work.
If I edit the line in the script, and put an IPv4 address in there and run it, it outputs properly, so I know it is not the API or the command for it, it is how the variable is being passed from powershell.
When the variable is used I get an error from the API.
I have tried this in older powershell, and today installed powershell 7.2 and get same result.
CodePudding user response:
'https://api.xyz.com/ipgeo?apiKey=XXXX&ip=$lookup'
'https://api.xyz.com/ipgeo?apiKey=XXXX&ip="$lookup"'
Looks like you are passing it literally. Try double-quotes outside, single inside.
"https://api.xyz.com/ipgeo?apiKey=XXXX&ip='$lookup'"
Edit: Variable expansion in strings and here-strings
https://devblogs.microsoft.com/powershell/variable-expansion-in-strings-and-here-strings/