简体   繁体   中英

Run Powershell command in macro as Admin (Elevated)

I have written some code that runs in a macro. Somehow the code runs the Powershell command (which is calc.exe), so specifically, I want to run the command as admin (elevated)? Can someone help me?

This is the code

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
fSfv = "powershell -command Start-Process -Verb RunAs calc.exe"

Running a command elevated in PowerShell requires use of the Start-Process cmdlet with parameter -Verb RunAs .

Additionally, consider inserting -NoProfile before -command to suppress the (here unnecessary) loading of PowerShell's profile files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM