powershell

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2021 License: MIT Imports: 17 Imported by: 0

README

go-powershell

go-powershell is a Go library to execute commands on a local PowerShell session.

This is a slimed down rewrite of github.com/bhendo/go-powershell which is a fork of github.com/gorillalabs/go-powershell.

The API is not compatible with github.com/bhendo/go-powershell

The original package was inspired by jPowerShell and allows one to run and remote-control a PowerShell session. Use this if you don't have a static script that you want to execute, bur rather run dynamic commands.

Usage

package main

import (
	"fmt"
	"log"

	"github.com/hnakamur/go-powershell"
)

func main() {
	shell, err := powershell.New()
	if err != nil {
		log.Fatal(err)
	}
	defer shell.Exit()

	stdout, err := shell.Exec("echo こんにちは")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(stdout)

	stdout, err = shell.Exec("Get-TimeZone | Select-Object StandardName")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(stdout)
}

License

MIT, see LICENSE file.

Documentation

Overview

powershell provides a PowerShell session in which you can execute commands serially (one at a time).

It detects the current code page when a session is created. And when running a command, it encodes the command and decodes the output from stdout or stderr with the encoding corresponding to the code page.

Example
shell, err := powershell.New()
if err != nil {
	log.Fatal(err)
}
defer shell.Exit()

stdout, err := shell.Exec("echo こんにちは")
if err != nil {
	log.Fatal(err)
}
fmt.Println(stdout)

stdout, err = shell.Exec("Get-TimeZone | Select-Object StandardName")
if err != nil {
	log.Fatal(err)
}
fmt.Println(stdout)
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var Encodings = map[int]encoding.Encoding{
	932: japanese.ShiftJIS,
	949: korean.EUCKR,

	65001: unicode.UTF8,
}

Encodings contains a mapping from code page to encoding. Only code page 932 and 65001 are supported by default. To use with other code pages, you need to add an entry before calling the New method.

View Source
var ErrUnsupportedCodePage = errors.New("unsupported code page")

ErrUnsupportedCodePage is the error returned from the New method if the detected code page is not in the Encodings map.

Functions

This section is empty.

Types

type Shell

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

Shell represents a PowerShell session.

func New

func New() (*Shell, error)

New creates a new PowerShell session.

func (*Shell) CodePage

func (s *Shell) CodePage() int

CodePage returns the detected code page of the session.

func (*Shell) Exec

func (s *Shell) Exec(cmd string) (stdout string, err error)

Exec execute a command in the session. This method is not goroutine safe.

func (*Shell) Exit

func (s *Shell) Exit() error

Exit closes the session.

Jump to

Keyboard shortcuts

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