Windows PowerShell命令未在批处理脚本中执行

发布于 2025-01-22 17:10:16 字数 529 浏览 0 评论 0原文

我在批处理文件上写了以下命令,以通过PowerShell命令从远程URL获取我的计算机的公共IP:

@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

现在,当我尝试执行批处理文件(即使作为管理员运行)时,我得到了以下OUPTUT而不是公共IP我正在使用的计算机。

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

我目前正在运行Windows Home,并安装了所有更新。请非常感谢任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小猫一只 2025-01-29 17:10:16

发布答案以确保对此有清晰度。

  1. 您在问题中指定所有其他设备都可以与当前脚本一起使用,除了该设备。
  2. 您在尝试NPOCMAKA提供的ODE后发布了一个错误,该错误指出:
 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" ... +

此错误具有明确的消息,实际上是两个。它向您显示了对IE的依赖性作为Windows上的解析器,更重要的是,它显示了此行中的修复程序:

Specify the UseBasicParsing parameter and try again

仅添加-UseBasicParsing参数将直接解决该问题,而无需最初运行IE即可。

文档

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

平生欢 2025-01-29 17:10:16

尝试这样的尝试:

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

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%

URL需要以双引号为单位,但要在批处理时逃脱它们,您需要使用三重双引号。

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.

无所谓啦 2025-01-29 17:10:16

似乎这个问题似乎发生在新鲜的窗户上。要解决,请启动Internet Explorer,然后选择推荐的设置。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文