【发布时间】:2026-01-09 13:40:01
【问题描述】:
我有一个运行 PowerShell 并提交结果的 VBA 脚本。我遇到的问题是 GUI 窗口弹出然后消失。有没有办法让 PowerShell GUI 在运行时完全隐藏?
这是代码的核心。我添加了-WindowStyle Hidden,它使GUI消失了,但它仍然闪烁几秒钟然后消失。
Do Until i = LRow + 1
ADGroup = Cells(i, 2)
' Construct PowerShell Command (PS syntax)
strPSCommand = "Get-ADGroupMember -Identity " & ADGroup & " -Recursive |select name"
Debug.Print strPSCommand
' Consruct DOS command to pass PowerShell command (DOS syntax)
strDOSCommand = "powershell -WindowStyle Hidden -command " & strPSCommand & ""
' Create shell object
Set objShell = CreateObject("Wscript.Shell")
' Execute the combined command
Set objExec = objShell.Exec(strDOSCommand)
' Read output into VBS variable
strPSResults = objExec.StdOut.ReadAll
Cells(i, 3).Value = strPSResults
i = i + 1
Loop
【问题讨论】:
-
不是powershell窗口,而是试图执行powershell的shell窗口停留在屏幕上。是
Exec()方法提出来的,您对此无能为力 -
尝试使用
,0(Hidden)而不是 ExecSet objExec = objShell.Run(strDOSCommand),0运行
标签: excel vba powershell excel-2013