我在批處理檔案上撰寫了以下命令,以通過 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
現在,當我嘗試執行批處理檔案(甚至以管理員身份運行)時,我得到了以下輸出,而不是我正在使用的計算機的公共 IP。
Obtaining my IP...
Invoke-Webrequest
Internet
At
d
我目前正在運行安裝了所有更新的 Windows Home。非常感謝任何幫助。
uj5u.com熱心網友回復:
發布答案以確保對此有明確性。
- 您在問題中指定所有其他設備都可以使用您當前的腳本,除了這個設備。
- 在嘗試 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。
這在本檔案中也有明確規定
uj5u.com熱心網友回復:
試試這樣:
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 需要用雙引號括起來,但是為了在批量呼叫時將它們轉義,您需要使用三重雙引號。
uj5u.com熱心網友回復:
似乎這個問題似乎發生在新安裝的 Windows 上。要修復,請啟動 Internet Explorer 并選擇推薦設定。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/460721.html
