简体   繁体   中英

Windows PowerShell command not executed in a batch script

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.

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.

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

Posting an answer to make sure there is clarity on this.

  1. You specify in your question that all other devices work with your current script, besides this one device.
  2. You posted an error after attempting the ode provided by Npocmaka which states:
 Invoke-Webrequest : The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again. At line:1 char:2 + (Invoke-Webrequest "http://componentsearch.everscrape.com/scraper/ip" ... +

This error has a clear message, well two in fact. It shows you the dependancy on IE as a parser on windows and more importantly it shows you the fix in this line:

Specify the UseBasicParsing parameter and try again

Simply adding the -UseBasicParsing parameter will therefore directly solve the issue without needing to run IE initially.

This is also clearly stipulated in this documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM