prince skosana
prince skosana

Reputation: 55

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

Upvotes: 2

Views: 2037

Answers (1)

mklement0
mklement0

Reputation: 439307

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.

Upvotes: 4

Related Questions