Reputation: 25
I am writing a VBA script that send parameters to a Powershell script to create AD accounts. Powershell needs to be running in administrator mode. Down below is how I call the script to run. If anyone could help me in running the application in admin mode that would be amazing!!
strCommand = "Powershell.exe -ExecutionPolicy Unrestricted -file ""<FilePath>""<Parameters>"
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)
Upvotes: 1
Views: 2580
Reputation: 439317
Assuming a file named script.ps1
in the current directory, and arguments foo
and bar
(alternatively, remove $PWD
below and specify the full script file path); -NoExit
keeps the elevated console window that is created open after running the script:
strCommand = "powershell.exe -c Start-Process -Verb RunAs powershell.exe \"" -ExecutionPolicy Unrestricted -NoExit -f `\""$PWD\script.ps1`\"" foo bar \"""
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)
Note the need to call the Windows PowerShell CLI, powershell.exe
, twice, nested:
First, in order to be able to run the PowerShell-internal Start-Process
cmdlet with -Verb RunAs
so as to create an elevated (run-as-admin) process, which invariably runs in a new console window. (Note that WshShell.Exec()
itself runs asynchronously and provides no direct feedback.)
Second, as the process to be elevated in order to invoke the target script elevated.
C:\Windows\System32
as its working directory, not the caller's. To use the caller's working directory, you'd have to switch from a -file
(-f
)-based call to a -command
(-c
)-based one and execute a Set-Location
statement first, which can notably also change how pass-through arguments for the script are interpreted; see this answer.This complicates quoting, and requires requires that the nested call's "
characters be seen as `\"
(represented as `\""
inside a VBScript string literal) on the outer call's command line, and the outer call's "
as \"
(\""
in VBScript).
See this answer for how to construct such a command line for use with many parameters (arguments) algorithmically, via a dictionary of parameter name-value pairs.
Upvotes: 3