Coder Social home page Coder Social logo

powershell-script's Introduction

Windows Powershell script runner

This crate is pretty basic. It uses std::process::Command to pipe commands to PowerShell. In addition to that there is a convenient wrapper around process::Output especially tailored towards the usecase of running Windows PowerShell commands.

Usage

I recommend that you write the commands to a *.ps file to be able to take advantage of existing tools to create the script.

This example creates a shortcut of notepad.exe to the desktop.

In script.ps

$SourceFileLocation="C:\Windows\notepad.exe"
$ShortcutLocation=[Environment]::GetFolderPath("Desktop")+"\notepad.lnk"
$WScriptShell=New-Object -ComObject WScript.Shell
$Shortcut=$WScriptShell.CreateShortcut($ShortcutLocation)
$Shortcut.TargetPath=$SourceFileLocation
$Shortcut.Save()

In main.rs

use powershell_script;

fn main() {
    let create_shortcut = include_str!("script.ps");
    match powershell_script::run(create_shortcut) {
        Ok(output) => {
            println!("{}", output);
        }
        Err(e) => {
            println!("Error: {}", e);
        }
    }
}

You can of course provide the commands as a string literal instead. Just beware that we run each line as a separate command.

The flag print_commands can be set to true if you want each command to be printed to the stdout of the main process as they're run which can be useful for debugging scripts or displaying the progress.

Use the PsScriptBuilder for better control

Instead of running a script using powershell_script::run() you can use PsScriptBuilder to configure several options:

use powershell_script::PsScriptBuilder;

fn main() {
    let ps = PsScriptBuilder::new()
        .no_profile(true)
        .non_interactive(true)
        .hidden(false)
        .print_commands(false)
        .build();
    let output = ps.run(r#"echo "hello world""#).unwrap();

    assert!(output.stdout().unwrap().contains("hello world"));
}

Features and compatability

On Windows it defaults to using the PowerShell which ships with Windows, but you can also run scripts using PowerShell Core on Windows by enabling the core feature.

On all other operating systems it will run scripts using PowerShell core.

Contributing

Right now this is only meant as a convenient wrapper for running PowerShell scripts, and I've been thinking about creating a utils crate with common tasks on Windows like creating a shortcut to a file (symlinking requires administrative privileges) but that will be better off in a separate crate so this can focus on running scripts.

Any pull requests with bugfixes or efficiency improvements is greatly appreciated.

powershell-script's People

Contributors

cfsamson avatar guilhermewerner avatar jknightguru avatar thatcoolcoder avatar ahadley1124 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.