如何在可变批处理脚本中存储 url 的响应

How to store response of url in a variable batch scripting

我正在尝试将 URL 的响应存储到批处理脚本中的变量中

示例:

set initial=PowerShell -Command "(new-object net.webclient).DownloadString('https://some-url/id')"

我正在尝试存储以下回复:

PowerShell -Command "(new-object net.webclient).DownloadString('https://some-url/id')"

进入变量:

initial

但这不起作用。我怎样才能实现我的目标?

使用for /f。像这样。

for /f "usebackq delims=" %%a in (`PowerShell -Command "(new-object net.webclient).DownloadString('https://some-url/id')"`) do set initial=%%a

此示例向您展示如何获取您的 public IP 地址:

@echo off
set "URL=http://myexternalip.com/raw"
Call :GetResponse %URL%
echo %initial% 
Pause>nul & exit
::*****************************************************************
:GetResponse <URL>
for /f "usebackq delims=" %%a in (`PowerShell -Command "(new-object net.webclient).DownloadString('%1')"`) do set "initial=%%a"
Exit /b
::*****************************************************************