是否可以使用 .vbs 脚本下载并执行远程服务器上托管的文件?我的以下尝试似乎不起作用。
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""\\12.345.67.789\filename.exe""")
Set objShell = Nothing
答案1
下面的操作将帮你得到想要的结果。确保将本地文件名 (sLocalFile) 设置为用户有权写入的路径,否则会失败。此外,大多数防病毒应用程序会将其视为恶意软件。
' variables
Dim xHttp, oStream, oShell, sRemoteURI, sLocalFile
' constants
Const klBinary = 1
Const klOverwrite = 2
' defined values
sRemoteURI = "https://example.com/filename.exe"
sLocalFile = """%appdata%\filename.exe"""
' create the web request
Set xHttp = CreateObject("Microsoft.XMLHTTP")
xHttp.Open "GET", sRemoteURI, False
xHttp.Send
' save the file locally
Set oStream = CreateObject("Adodb.Stream")
oStream.Type = klBinary
oStream.Open
oStream.Write xHttp.responseBody
oStream.SaveToFile sLocalFile, klOverwrite
Set oStream = Nothing
Set xHttp = Nothing
' run the file
Set oShell = WScript.CreateObject("WScript.Shell")
oShell.Run(sLocalFile)
Set oShell = Nothing