Just like your script-file path, your arguments must also be enclosed in what PowerShell ultimately sees as `\"
, which is represented as `\""
inside a VBScript string literal (see this answer for the PowerShell command line being invoked and why two, nested calls to powershell.exe
are required).
For instance, to add the named -FName
argument with its value (ultimately) enclosed in (just) double quotes:
"... -FName `\""" & FName & "`\"" ..."
To put it all together:
strCommand = "powershell.exe -command Start-Process -Verb RunAs powershell.exe \"" -ExecutionPolicy Unrestricted -NoExit -file `\""C:\Users\mbaradihi\Desktop\AutoADTest\test.ps1`\"" -FName `\""" & FName & "`\"" -middleIn `\""" & middleIn & "`\"" -LName `\""" & LName & "`\"" -Branch `\""" & Branch & "`\"" -Title `\""" & Title & "`\"" -employeeNum `\""" & employeeNum & "`\"" -company `\""" & company & "`\"" -dept `\""" & dept & "`\"" -ad `\""" & ad & "`\"" -city `\""" & city & "`\"" -state `\""" & state & "`\"" -zip `\""" & zip & "`\"" -manager `\""" & manager & "`\"" \"""
Since constructing this string by hand is tedious and it is easy to make mistakes - which result in quiet failure - it's better to construct the string algorithmically, as the following example shows:
' The *full* target script path.
' Note that the elevated process will see C:\Windows\System32 as its working dir.
' (In PowerShell (Core) 7+, with pwsh.exe, the caller's working dir. is now inherited).
scriptPath = "C:\Users\mbaradihi\Desktop\AutoADTest\test.ps1"
' Create a dictionary of parameter name-value pairs.
Set dict = CreateObject("Scripting.Dictionary")
dict.Add "FName", "Anna"
dict.Add "middleIn", "E."
dict.Add "LName", "Roosevelt"
dict.Add "Branch", "White House"
dict.Add "Title", "First Lady"
dict.Add "employeeNum", "2"
dict.Add "company", "USA"
dict.Add "dept", "PCSW"
dict.Add "ad", "aeroosevelt@example.org"
dict.Add "city", "Washington"
dict.Add "state", "DC"
dict.Add "zip", "20500"
dict.Add "manager", "None"
' Synthesize the string that encodes all arguments.
args=""
for each key in dict.Keys
args = args & " -" & key & " `\""" & dict(key) & "`\"""
next
' Synthesize the overall command line.
strCommand = "powershell.exe -command Start-Process -Verb RunAs powershell.exe \"" -ExecutionPolicy Unrestricted -NoExit -file `\""" & scriptPath & "`\""" & args & "\"""
' Execute it.
' Note: WshShell.Exec() executes asynchronously and provides no direct feedback.
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)