我正在尝试将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
但这不起作用。我怎样才能实现目标?
答案 0 :(得分:1)
使用for /f
。这样的事情。
for /f "usebackq delims=" %%a in (`PowerShell -Command "(new-object net.webclient).DownloadString('https://some-url/id')"`) do set initial=%%a
答案 1 :(得分:1)
这个示例向您展示如何获取公共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
::*****************************************************************