PowerShellSession

Struct PowerShellSession 

Source
pub struct PowerShellSession { /* private fields */ }
Expand description

Represents a PowerShell parsing and evaluation session.

This is the main entry point for parsing and evaluating PowerShell scripts. It maintains the session state including variables, tokens, and error information.

§Examples

use ps_parser::PowerShellSession;

// Create a new session
let mut session = PowerShellSession::new();

// Evaluate a simple expression
let result = session.safe_eval("$a = 1 + 2; Write-Output $a").unwrap();
assert_eq!(result, "3");

// Parse and get detailed results
let script_result = session.parse_input("$b = 'Hello World'; $b").unwrap();
println!("Result: {:?}", script_result.result());

Implementations§

Source§

impl<'a> PowerShellSession

Source

pub fn new() -> Self

Creates a new PowerShell parsing session with default settings.

The session is initialized with built-in variables like $true, $false, $null, and special variables like $? for error status tracking.

§Returns

A new PowerShellSession instance ready for script evaluation.

§Examples
use ps_parser::PowerShellSession;

let mut session = PowerShellSession::new();
let result = session.safe_eval("$true").unwrap();
assert_eq!(result, "True");
Source

pub fn with_variables(self, variables: Variables) -> Self

Creates a new PowerShell session with the provided variables.

This constructor allows you to initialize the session with a custom set of variables, such as environment variables or variables loaded from configuration files.

§Arguments
  • variables - A Variables instance containing the initial variable set.
§Returns

A new PowerShellSession instance with the provided variables.

§Examples
use ps_parser::{PowerShellSession, Variables};

let env_vars = Variables::env();
let mut session = PowerShellSession::new().with_variables(env_vars);
let username = session.safe_eval("$env:USERNAME").unwrap();
Source

pub fn safe_eval(&mut self, script: &str) -> Result<String, ParserError>

Safely evaluates a PowerShell script and returns the output as a string.

This method parses and evaluates the provided PowerShell script, handling errors gracefully and returning the result as a formatted string. It’s the recommended method for simple script evaluation.

§Arguments
  • script - A string slice containing the PowerShell script to evaluate.
§Returns
  • Result<String, ParserError> - The output of the script evaluation, or an error if parsing/evaluation fails.
§Examples
use ps_parser::PowerShellSession;

let mut session = PowerShellSession::new();

// Simple arithmetic
let result = session.safe_eval("1 + 2 * 3").unwrap();
assert_eq!(result, "7");

// Variable assignment and retrieval
let result = session.safe_eval("$name = 'World'; \"Hello $name\"").unwrap();
assert_eq!(result, "Hello World");
Source

pub fn parse_input(&mut self, input: &str) -> Result<ScriptResult, ParserError>

Parses and evaluates a PowerShell script, returning detailed results.

This method provides comprehensive information about the parsing and evaluation process, including the final result, generated output, any errors encountered, and the tokenized representation of the script. It’s particularly useful for debugging and deobfuscation.

§Arguments
  • input - A string slice containing the PowerShell script to parse and evaluate.
§Returns
  • Result<ScriptResult, ParserError> - A detailed result containing the evaluation outcome, output, errors, and tokens, or a parsing error if the script is malformed.
§Examples
use ps_parser::PowerShellSession;

let mut session = PowerShellSession::new();
let script_result = session.parse_input("$a = 42; Write-Output $a").unwrap();

println!("Final result: {:?}", script_result.result());
println!("Generated output: {:?}", script_result.output());
println!("Parsing errors: {:?}", script_result.errors());
println!("Deobfuscated code: {:?}", script_result.deobfuscated());

Trait Implementations§

Source§

impl Default for PowerShellSession

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T