I am having difficulty with executing a command line in cmd from Powershell. The problem is I can't seem to use my global variables in the command line when cmd is opened from Poweshell. Below is the code I am working with but to no avail. Can someone please provide guidance on this issue?
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -verb RunAs -ArgumentList {/k set Name="$cmdname" set Item="$cmditem" setlocal EnableDelayedExpansion echo "%Name%" }
Thanks, Roger
CodePudding user response:
The syntax is awkward for sure:
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -ArgumentList "/k set Name=$cmdname & set Item=$cmditem & call echo %name%"
Some of the reasoning here is:
- cmd: additional commands need to be separated by
& - cmd: the
setcommand takes everything after=until special characters like& - cmd:
setlocal EnableDelayedExpansiondoesn't really apply here. Usecallto delay instead.- delayed variable syntax is also
!var!rather than%var%
- delayed variable syntax is also
- Powershell: using brackets in
-ArgumentList {stuff}sends as a literal string, and variables aren't expanded
