pwsh

package
v0.1.0-dev.20260203232125 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package pwsh provides a persistent PowerShell session for lore.

Architecture: Why a persistent session?

Starlark is the orchestration layer (logic, control flow, variables). The execution backend differs by platform:

  • Unix: stateless shell.run() is fine — each command is a new process.
  • Windows: a persistent PowerShell session is the natural backend.

PowerShell modules (Az, ActiveDirectory, PackageManagement) establish authenticated sessions on import. COM/.NET objects (WMI, Registry, IIS) are expensive to instantiate. DSC operations require compile→test→apply within a single session. Spawning `pwsh -Command` for each operation loses all of this state.

This package is intended as a Starlark binding (pwsh.run, pwsh.set) — the Starlark script decides what to do, the PowerShell session handles how on Windows.

Usage

Commands run directly in session scope. Variables, functions, and module imports persist across calls. If a command calls `exit N`, the session terminates and the exit code is captured from the process state.

ps, _ := pwsh.New()
defer ps.Close()

ps.Run("$greeting = 'Hello'")
result := ps.Run("Write-Output $greeting")
fmt.Println(result.Stdout) // "Hello"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Available

func Available() bool

Available returns true if PowerShell is installed.

func Bootstrap

func Bootstrap() error

Bootstrap installs PowerShell if not present. This is platform-specific and may require elevated privileges.

func Ensure

func Ensure() error

Ensure checks that PowerShell 7+ is installed, returns an error if not.

func Version

func Version() (string, error)

Version returns the PowerShell version string.

Types

type Result

type Result struct {
	Command  string        `json:"command"`
	Stdout   string        `json:"stdout,omitempty"`
	Stderr   string        `json:"stderr,omitempty"`
	ExitCode int           `json:"exit_code"`
	Start    time.Time     `json:"start"`
	Duration time.Duration `json:"duration"`
	Error    string        `json:"error,omitempty"`
}

Result captures the output of a PowerShell command.

func (*Result) Failed

func (r *Result) Failed() bool

Failed returns true if the command failed.

func (*Result) JSON

func (r *Result) JSON() string

JSON returns the result as indented JSON.

func (*Result) OK

func (r *Result) OK() bool

OK returns true if the command succeeded.

func (*Result) String

func (r *Result) String() string

String returns a human-readable summary.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session is a persistent PowerShell session.

func New

func New() (*Session, error)

New creates and starts a new PowerShell session. Returns an error if PowerShell is not installed or fails to start.

func (*Session) Audit

func (s *Session) Audit(w io.Writer) *Session

Audit sets the audit log writer.

func (*Session) Close

func (s *Session) Close() error

Close terminates the PowerShell session.

func (*Session) History

func (s *Session) History() []*Result

History returns all commands run in this session.

func (*Session) Must

func (s *Session) Must(command string) *Result

Must runs a command and panics if it fails.

func (*Session) Run

func (s *Session) Run(command string) *Result

Run executes a PowerShell command and returns the result. Commands run directly in session scope, so variables persist across calls. If the command calls `exit N`, the session terminates and the exit code is captured from the process state.

func (*Session) Script

func (s *Session) Script(commands ...string) *Result

Script runs multiple commands in sequence, stopping on first failure.

func (*Session) Set

func (s *Session) Set(name, value string) *Session

Set sets a PowerShell variable for subsequent commands.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL