diff --git a/Cargo.toml b/Cargo.toml index 2705b4f..8933aed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,8 @@ A library for running Windows PowerShell scripts # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +# Use PowerShell Core instead of Windows Powershell. +core = [] + [dependencies] diff --git a/examples/hello/main.rs b/examples/hello/main.rs new file mode 100644 index 0000000..6580501 --- /dev/null +++ b/examples/hello/main.rs @@ -0,0 +1,14 @@ +extern crate powershell_script; + +/// Print 'Hello' to console. +fn main() { + let hello = include_str!("script.ps1"); + match powershell_script::run(hello, false) { + Ok(output) => { + eprint!("{}", output); + } + Err(e) => { + println!("Error: {}", e); + } + } +} diff --git a/examples/hello/script.ps1 b/examples/hello/script.ps1 new file mode 100644 index 0000000..d37be9b --- /dev/null +++ b/examples/hello/script.ps1 @@ -0,0 +1 @@ +echo "Hello" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 93fc1df..d518b6a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,7 +68,14 @@ type Result = std::result::Result; /// ## Panics /// If there is an error retrieving a handle to `stdin` in the child process. pub fn run_raw(script: &str, print_commands: bool) -> Result { + #[cfg(all(not(feature = "core"), windows))] + // Windows PowerShell let mut cmd = Command::new("PowerShell"); + + #[cfg(any(feature = "core", not(windows)))] + // PowerShell Core + let mut cmd = Command::new("pwsh"); + cmd.stdin(Stdio::piped()); cmd.stdout(Stdio::piped()); cmd.stderr(Stdio::piped());