Home > OS >  Windows PowerShell command not executed in a batch script
Windows PowerShell command not executed in a batch script

Time:04-21

I have the following commands written on a batch file to obtain my computer's public IP from a remote URL via powershell command:

@echo off
setlocal EnableDelayedExpansion
call :MyIP2
timeout /t 50
EXIT /B 0
:MyIP2
echo Obtaining my IP...
For /f %%A in ('powershell -NonI -NoP -C "(Invoke-Webrequest http://componentsearch.everscrape.com/scraper/ip).content"') Do echo %%A
EXIT /B 0

Now when I try to execute the batch file (even run as administrator), I got the following ouptut instead of the public IP of the computer I'm using.

Obtaining my IP...
Invoke-Webrequest
Internet
At
 
 
 
 
d

I'm currently running Windows Home with all the updates installed. Please any help is greatly appreciated.

CodePudding user response:

try like this:

For /f  "tokens=* delims=" %%A in ('powershell -NonI -NoP -C "(Invoke-Webrequest """http://componentsearch.everscrape.com/scraper/ip""").content"') Do echo %%A

or

echo Obtaining my IP...
for /f  "tokens=* delims=" %%A in (
    'powershell -NonI -NoP -C "(Invoke-Webrequest """http://componentsearch.everscrape.com/scraper/ip""").content"'
) do (
    set "my_ip=%%A"
) 

echo %my_ip%

the URL needs to be in double quotes but in order to escape them when called from batch you need to use triple double quotes.

CodePudding user response:

It seems this issue seem to occur on fresh installed windows. To fix, launch the Internet Explorer and select Recommended Settings.

  • Related