Documentation
¶
Overview ¶
Package powershell provides an interface to run PowerShell code in a session that remains "hot", such that you do not have to create a new instance of the powershell process with each invocation, with many enhancements over other packages of the same name! Supports Windows PowerShell and pwsh, contexts and multithreaded use.
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrInvalidCommandString is returned if the command passed contains any CR or LF characters // since this will stall the pipe. Note that leading and trailing space will be trimmed automatically. // Multiple commands should be chained with semicolon, not line breaks. ErrInvalidCommandString = errors.New("invalid command") // ErrShellClosed is returned if an attempt is made to perform an operation on a closed shell ErrShellClosed = errors.New("shell is closed") // ErrPipeWrite is returned if there was a problem sending the command // to the session's sdtin pipe. ErrPipeWrite = errors.New("error sending command") // ErrCommandFailed will be returned if any output from the command sent was // read from stderr. This will include unhandled exceptions (uncaught throws) // and any direct writes to stderr like Console::Error.WriteLine(). ErrCommandFailed = errors.New("data written to stderr") // ErrScript can be returned by the ExecuteScript functions if the // type of script passed as an argument cannot be determined ErrScript = errors.New("cannot determine script type") )
Functions ¶
This section is empty.
Types ¶
type Shell ¶
type Shell interface {
// Execute runs PowerShell commands in the session instance, capturing stdout and stderr streams
Execute(cmd string) (string, string, error)
// ExecuteWithContext runs PowerShell commands in the session instance, capturing stdout and stderr streams.
//
// The context allows cancellation of the command. Streams to/from the PowerShell session may
// block indefinitely if the command is not well formed as the input may still be waiting.
//
// Note that if the error is "context deadline exceeded", the underlying session will be
// unstable and it will be restarted. A restarted shell _may_ be unstable!
ExecuteWithContext(ctx context.Context, cmd string) (string, string, error)
// ExecuteScript runs a multiline script or external script file in the session instance, capturing stdout and stderr streams
//
// If the argument is a path to an existing script file, then that will be executed,
// otherwise the value is assumed to be a multiline string.
ExecuteScript(scriptOrPath string) (string, string, error)
// ExecuteScriptWithContext runs a multiline script or external script file in the session instance, capturing stdout and stderr streams.
//
//
// If the argument is a path to an existing script file, then that will be executed,
// otherwise the value is assumed to be a multiline string.
//
// The context allows cancellation of the command. Streams to/from the PowerShell session may
// block indefinitely if the command is not well formed as the input may still be waiting.
//
// Note that if the error is "context deadline exceeded", the underlying session will be
// unstable and it will be restarted. A restarted shell _may_ be unstable!
ExecuteScriptWithContext(ctx context.Context, scriptOrPath string) (string, string, error)
// Version returns the PowerShell version as reported by the $Host built-in variable.
// If there was an error reading this, version.Major will be -1.
Version() *semver.Version
// Exit terminates the underlying PowerShell process.
// Error will be non-nil if shell already closed.
Exit() error
}
Shell is the interface to a PowerShell session
type ShellOptionFunc ¶ added in v0.2.0
type ShellOptionFunc func(*ShellOptions)
ShellOptionFunc describes optional argmuments to add to the New call.
func WithModules ¶ added in v0.2.0
func WithModules(modules ...string) ShellOptionFunc
WithModules specifies a list of PowerShell modules to import into the shell when it starts
type ShellOptions ¶ added in v0.2.0
type ShellOptions struct {
// contains filtered or unexported fields
}
ShellOptions represents options passed to the shell when it is started.
Click to show internal directories.
Click to hide internal directories.