powershell-script icon indicating copy to clipboard operation
powershell-script copied to clipboard

Design for executing either script or file

Open erick-thompson opened this issue 1 year ago • 1 comments

I was looking at issue #11 which was attempted to be fixed with #20. The change wasn't landed because of the difference in launching PowerShell file and a script block. My particular use case is mainly around files with parameters, so I'm looking at how this would work.

What do you think about a builder approach where you need to specify script or file, which can then allow for args only when you're going to run a file?

If this seems acceptable, I'd be happy to work on a PR

Something like

fn main() {
    let ps = PsScriptBuilder::new()
        .no_profile(true)
        .non_interactive(true)
        .hidden(false)
        .print_commands(false)
        .file("path/to/file.ps1")
        .arguments.add("argument")
        .build();
    // below fails
    let output = ps.run(r#"echo "hello world""#).unwrap();        
    
    // below works
    let output = ps.run().unwrap();
}

likewise

fn main() {
    let ps = PsScriptBuilder::new()
        .no_profile(true)
        .non_interactive(true)
        .hidden(false)
        .print_commands(false)
        .build();

    // below works
    let output = ps.run(r#"echo "hello world""#).unwrap();        
    
    // below fails
    let output = ps.run().unwrap();
}

I think it would be better to have both approaches specify the payload (file or script) in the builder methods, but that would conflict with how it works currently.

erick-thompson avatar Oct 04 '24 22:10 erick-thompson

Hi and sorry for the delayed response.

I think this might be a good idea. I think this will be a breaking change anyway, but if we end up with a better API I think it's worth it. Since there seems to be quite a bit of interest for this I think the solution you suggest seems fine and I would really appreciate any help I can get on this.

cfsamson avatar Dec 04 '24 22:12 cfsamson