Include an Script and put it to the Command-Execution


at the moment i made my peace with lacking functions in rust on windows (windows yeah uses bytecompiled filetype like .lnk :smile: and linux uses text and mimetypes). I have all my problems solved allready with powershell scripts for years ago and now i have something:

let process = Command    ::new("powershell.exe")
    		.expect("failed to execute process");

Yeah functioned but that is a little embarrassing to me :slight_smile:. No i want following, i want capsule all powershell scripting in the executable.

I know rust has the ability with include/_str/_bytes but how can i push the string from an include_str or so give to an std::Command that the handle this reference to an include like an script-file?

So that all is capsuled in an exe and the user gets nothing from the call with?


Use the tempfile crate to create a temporary directory that you can put a file inside.

(I suggest a tempdir rather than a tempfile because you're on windows and so you probably want to be able to name the file with the correct file extension)

You can even make a little wrapper type that represents a file in its own temporary directory:

use std::io;
use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use tempfile::TempDir;

/// A temporary file with a user-specified filename, suitable for
/// use on windows.
struct TempFile {
    filepath: PathBuf,
    dir: TempDir,

impl TempFile {
    pub fn new<N, S>(name: N, contents: S) -> io::Result<Self>
        N: AsRef<OsStr>,
        S: AsRef<[u8]>,
        let dir = TempDir::new()?;
        let filepath = dir.path().join(name.as_ref());
        std::fs::write(&filepath, contents)?;
        Ok(TempFile { dir, filepath })

    pub fn path(&self) -> &Path { &self.filepath }
use std::process::Command;

fn main() {
    let script_bytes = include_bytes!("path/to/script");
    let script = TempFile::new("script.ps1", script_bytes).unwrap();

    let process = Command::new("powershell.exe")
                .expect("failed to execute process");

@ExpHP Thx for the help, its functioned very well and is now my dealing with external powershell scripts. But a pure memory based solution is not possible? I mean is only hope :slight_smile: why i think this is not possible by system.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.