Reputation: 1183
I want to launch a admin powershell with two commands :
$command1 = "Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{example}' -Name DHCPClassId -Value National"
$command2 = "Restart-NetAdapter -Name Ethernet"
Start-Process powershell -Verb 'RunAs' -ArgumentList "-NoExit","-Command &{$command1,$command2}"
And i have an error : he don't want to execute these two because they have a same parameter (-Name
), so, what can i do ?
Upvotes: 1
Views: 501
Reputation: 437062
PowerShell's statement separator is ;
, not ,
(the latter constructs arrays).
Also:
Even though passing arguments individually, as elements of an array to Start-Process
is conceptually clean, it should be avoided due to a long-standing bug: pass a single string encoding all arguments to -ArgumentList
- see this answer for more information.
When using PowerShell's CLI, -Command & { ... }
is never necessary - just use -Command ...
; that is, there is no reason to construct a script block ({ ... }
) that you must then invoke with &
- all commands given are directly executed by default.
To put it all together:
$command1 = "Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{example}' -Name DHCPClassId -Value National"
$command2 = "Restart-NetAdapter -Name Ethernet"
Start-Process powershell -Verb RunAs "-NoExit -Command $command1; $command2"
Upvotes: 1