以管理员身份在宏中运行Powershell命令(已提升)

时间:2018-08-12 15:35:45

标签: vba excel-vba powershell

我写了一些在宏中运行的代码。代码以某种方式运行Powershell命令(即calc.exe),所以具体来说,我想以admin(高架)身份运行命令吗?有人可以帮我吗?

这是代码

Sub Auto_Open()
        o
End Sub

Sub AutoOpen()
        o
End Sub

Sub Document_Open()
        o
End Sub

Public Function o() As Variant
        Dim fSfv As String
        fSfv = "powershell calc.exe"
        Const HIDDEN_WINDOW = 0
        strComputer = "."
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        Set objStartup = objWMIService.Get("Win32_ProcessStartup")
        Set objConfig = objStartup.SpawnInstance_
        objConfig.ShowWindow = HIDDEN_WINDOW
        Set objProcess = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
        objProcess.Create fSfv, Null, objConfig, intProcessID
End Function

1 个答案:

答案 0 :(得分:3)

fSfv = "powershell -command Start-Process -Verb RunAs calc.exe"

运行在PowerShell中提升的命令需要使用带有参数-Verb RunAs的{​​{3}}。

另外,请考虑在-NoProfile之前插入-command,以禁止加载当前用户的个人资料。