无法通过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
相关产品推荐
相关产品推荐

