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

PowerShell脚本实现网络共享EXE本地运行以规避Defender ASR拦截

实用PowerShell脚本方案(带状态反馈)

以下是兼顾操作简便性和清晰状态反馈的脚本实现,完全满足每次运行前复制最新EXE、本地执行的需求:


脚本核心代码

# 配置参数(根据实际环境修改)
$networkSourcePath = "\\server\share\target.exe"  # 网络共享的EXE路径
$localTargetPath = "$env:TEMP\target.exe"         # 本地临时目录(无需管理员权限)
$delaySeconds = 10                                # 延迟时间(可调整)

# 清屏并显示初始状态
Clear-Host
Write-Host "=== 正在准备运行目标程序 ===" -ForegroundColor Cyan
Write-Host "源路径: $networkSourcePath" -ForegroundColor Gray
Write-Host "本地路径: $localTargetPath`n" -ForegroundColor Gray

try {
    # 复制最新文件
    Write-Host "[1/3] 正在从网络共享复制最新程序文件..." -ForegroundColor Yellow
    Copy-Item -Path $networkSourcePath -Destination $localTargetPath -Force -ErrorAction Stop
    Write-Host "✅ 文件复制完成" -ForegroundColor Green

    # 倒计时延迟
    Write-Host "`n[2/3] 等待 $delaySeconds 秒后启动程序(可按Ctrl+C取消)..." -ForegroundColor Yellow
    for ($i = $delaySeconds; $i -ge 1; $i--) {
        Write-Host "剩余 $i 秒..." -NoNewline
        Start-Sleep -Seconds 1
        Write-Host "`r" -NoNewline  # 覆盖当前行
    }
    Write-Host "`n✅ 延迟结束" -ForegroundColor Green

    # 启动本地程序
    Write-Host "`n[3/3] 正在启动目标程序..." -ForegroundColor Yellow
    Start-Process -FilePath $localTargetPath -ErrorAction Stop
    Write-Host "✅ 程序已成功启动" -ForegroundColor Green
}
catch {
    Write-Host "`n❌ 操作失败: $($_.Exception.Message)" -ForegroundColor Red
    Read-Host "按任意键退出"
    exit 1
}

# 自动退出(或保留窗口查看状态)
Start-Sleep -Seconds 3

优化点说明

  • 无需管理员权限:用$env:TEMP临时目录,普通用户即可写入,避免权限问题
  • 实时状态反馈:用颜色区分不同状态(黄色=进行中,绿色=成功,红色=失败),倒计时动态刷新
  • 错误处理:捕获复制/启动过程中的异常,明确提示错误原因
  • 可配置性:顶部参数区可快速修改路径、延迟时间

简化用户操作的额外建议

  1. 打包成快捷方式:

    • 右键脚本文件 → 发送到 → 桌面快捷方式
    • 右键快捷方式 → 属性 → 目标栏末尾添加 -ExecutionPolicy Bypass(解决脚本执行权限问题)
    • 可自定义快捷方式图标,让用户一键点击运行
  2. 隐藏PowerShell窗口(可选):
    如果不想让用户看到命令行窗口,可创建一个批处理文件(.bat)调用脚本:

    @echo off
    powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File "C:\路径\你的脚本.ps1"
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 16:42:08