[英]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)?代码以某种方式运行 Powershell 命令(即 calc.exe),所以具体来说,我想以管理员身份运行该命令(提升)? 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
.在 PowerShell 中运行提升的命令需要使用带有参数-Verb RunAs
的Start-Process
cmdlet 。
Additionally, consider inserting -NoProfile
before -command
to suppress the (here unnecessary) loading of PowerShell's profile files.此外,考虑在-command
之前插入-NoProfile
以抑制(此处不必要)加载 PowerShell 的配置文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.