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

无法通过VBA以管理员权限运行PowerShell脚本求助

问题:VBA无法以管理员权限执行PowerShell脚本

尝试通过VBA以管理员权限运行PowerShell脚本,参考各类论坛方案适配后仍未解决。执行VBA脚本时窗口一闪即关,脚本未实际执行(未完成网卡断开/启用操作、无10秒等待),且无StdOut或StdErr输出。但在手动提升权限的命令提示符或PowerShell窗口中运行该脚本可正常生效。脚本用于临时断开名为Ethernet的网卡,以下为简化版本:

PowerShell脚本

Disable-NetAdapter -name Ethernet -confirm:$false
Start-Sleep -Seconds 10
Enable-NetAdapter -name Ethernet -confirm:$false

可正常运行的提升权限命令行代码

C:\PowerShell Scripts>"disable network temporarily.ps1"

手动运行脚本的命令行界面

无法运行的VBA代码

'需引用Windows Script Host Object Model
Option Explicit
Sub TempNetworkDisable()
    Dim wshShell As wshShell
    Dim wshShellExec    As Object
    Dim strCommand      As String
    Dim strOutput

    strCommand = "pwsh.exe -c Start-Process -Verb RunAs pwsh.exe \" -ExecutionPolicy Unrestricted -NoExit -f `\"C:\PowerShell Scripts\Disable Network Temporarily.ps1`\"""
    
    Set wshShell = New wshShell
    Set wshShellExec = wshShell.Exec(strCommand)
    strOutput = wshShellExec.StdOut.ReadAll()
    Debug.Print "StdOut:", strOutput

    strOutput = wshShellExec.StdErr.ReadAll()
    Debug.Print "StdErr:", strOutput
End Sub

系统环境

  • Windows 11 Pro(版本 10.0.22631 Build 22631)
  • PowerShell版本 7.4.6
  • Microsoft® Excel® for Microsoft 365 MSO 64位(版本 2409 Build 16.0.18025.20160)
  • VBA 7.1.1143

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 22:38:22