是否可以通过 URL 将批处理文件中的数据写入网络服务器上的文件?
类似这样的:
set url=http://example.com/log/logfile.txt
set hello=Hello world
echo %hello% >> %url%
goto :eof
谢谢!
您可以通过 BAT 执行此操作,但使用 powershell。
@echo off
setlocal
set Url=http://example.com/log/logfile.txt
set Hello=Hello world
set POWERSHELL_PATH=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
echo $body = @{ Data = "%Hello%" } > temp.ps1
echo $jsonBody = $body ^| ConvertTo-Json >> temp.ps1
echo Invoke-RestMethod -Uri "%Url%" -Method Post -Body $jsonBody -ContentType "application/json" >> temp.ps1
"%POWERSHELL_PATH%" -ExecutionPolicy Bypass -File temp.ps1
del temp.ps1
endlocal