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
impl<'a> PowerShellSession
Sourcepub fn new() -> Self
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");
Sourcepub fn with_variables(self, variables: Variables) -> Self
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
- AVariables
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();
Sourcepub fn safe_eval(&mut self, script: &str) -> Result<String, ParserError>
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");
Sourcepub fn parse_input(&mut self, input: &str) -> Result<ScriptResult, ParserError>
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§
Auto Trait Implementations§
impl Freeze for PowerShellSession
impl !RefUnwindSafe for PowerShellSession
impl !Send for PowerShellSession
impl !Sync for PowerShellSession
impl Unpin for PowerShellSession
impl !UnwindSafe for PowerShellSession
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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