一个项目需要这样的脚本,特记录下。保存bat可直接执行。
@echo off
chcp 65001
rmdir /s /q C:\temp
set "Save=C:\temp"
if not exist "%Save%" (
mkdir "%Save%"
attrib +h "%Save%"
)
set "Url=[Url]"
del %Save%\temp.rdp 2>nul
del %Save%\test.ps1 2>nul
powershell -Command "& { try { Invoke-WebRequest -Uri '%Url%' -OutFile '%Save%\temp.rdp' } catch { Write-Host 'Error: ' $_.Exception.Message; exit 1 } }"
if %errorlevel% neq 0 goto downloadVBS
:processRDP
set /p a=<%Save%\temp.rdp
echo "%a%"
(echo -Join^("password 51:b:",^(^([password] ^| ConvertTo-SecureString -AsPlainText -Force^) ^| ConvertFrom-SecureString^) ^) ^| Out-File -Append %Save%\temp.rdp -encoding utf8;) > %Save%\test.ps1
powershell -executionpolicy remotesigned -file "%Save%\test.ps1"
start "" "%Save%\temp.rdp"
del %Save%\test.ps1 2>nul
exit
:downloadVBS
echo PowerShell download failed, switching to VBScript.
(echo Download Wscript.Arguments^(0^),Wscript.Arguments^(1^)
echo Sub Download^(url,target^)
echo Const adTypeBinary = 1
echo Const adSaveCreateOverWrite = 2
echo Dim http,ado
echo Set http = CreateObject^("Msxml2.ServerXMLHTTP"^)
echo http.open "GET", url, False, "", ""
echo http.send
echo Set ado = createobject^("Adodb.Stream"^)
echo ado.Type = adTypeBinary
echo ado.Open
echo ado.Write http.responseBody
echo ado.SaveToFile target, adSaveCreateOverWrite
echo ado.Close
echo End Sub) > %Save%\DownloadFile.vbs
cscript //Nologo %Save%\DownloadFile.vbs "%Url%" "%Save%\temp.rdp"
del %Save%\DownloadFile.vbs
goto processRDP