Unable to run this particular shell script

I wanted to run this particular command. When I ran it as a batch script for Windows:

powershell -command "&{Start-Process -filepath 'cmd' -argumentlist '/c cmd.exe' -verb runas}"

It runs just fine. But when I tried to do the same thing for Rust:

use std::process::Command;

fn main()
{
    // powershell -command "&{Start-Process -filepath 'cmd' -argumentlist '/c cmd.exe' -verb runas}"

    let test = Command::new("cmd.exe")
    .arg("/c")
    .arg("powershell")
    .arg(r#"-command "&{Start-Process -filepath 'cmd' -argumentlist '/c cmd.exe' -verb runas}""#)
    .output()
    .unwrap();

    println!("{:?}", test);
}

It fails and I get this stderr.

Output { status: ExitStatus(ExitStatus(1)), stdout: "", stderr: "The string is missing the terminator: \".\r\n    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException\r\n    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString\r\n \r\n\'{Start-Process\' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n" }

Any idea how to solve this?

AFAIK, cmd /c receives one argument - the full command line, not two. However, I'm not sure why do you need it in the first place, since powershell should be available to run directly.

What would be the correct command to execute for this entire line?

I am trying to write a Rust application and part of it would require this specific command.

The exact line you had in batch script.

I mean, why can't you use Command::new("powershell.exe")?

2 Likes

Ah right.

Oh, why didn't I think of that.


Edit: Seems to work now, thanks mate :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.