0

how to start, a program with the arguments using powershell via cmd :)

powershell -command "& {get-service -computername server1,server2 -displayname 'instance *'|where {$_.status -eq 'Running'}|foreach-object {start-process -filepath 'C:\monitor.exe' -argumentlist "-adminhost $($_.machinename) -servername $($_.name)"} } "

Now when replacing " " with the carts on ' ' program I get a text representation of variables

-argumentlist "-adminhost $($_.machinename) -servername $($_.name)"

is it possible to write such a command powershell starts the process with parameters?

1 Answer 1

3

Your best option would be to write the PowerShell code to a file and run that:

powershell.exe -File "C:\path\to\your.ps1"

If you must execute a command string with nested double quotes you need to escape the nested double quotes with backslashes:

powershell.exe -Command "&{Write-Output \"foo bar\"}"

In your case:

powershell.exe -Command "&{Get-Service -ComputerName server1,server2 -DisplayName 'instance *'|where {$_.Status -eq 'Running'}|ForEach-Object {Start-Process -FilePath 'C:\monitor.exe' -ArgumentList \"-adminhost $($_.MachineName) -servername $($_.Name)\"}}"

Or, since the individual arguments don't seem to contain spaces, you could pass the argument list as an actual list/array:

powershell.exe -Command "&{Get-Service -ComputerName server1,server2 -DisplayName 'instance *'|where {$_.Status -eq 'Running'}|ForEach-Object {Start-Process -FilePath 'C:\monitor.exe' -ArgumentList '-adminhost', $_.MachineName, '-servername', $_.Name}}"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.