powershell

package module
v0.0.0-...-1352ee1 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2019 License: MIT Imports: 9 Imported by: 0

README

Powershell

This package is inspired by jPowerShell and allows one to run and remote-control a PowerShell session.

Installation

go get github.com/shinyv/powershell

Usage

PowerShell ISE

It uses os/exec to start a PowerShell process to interactive in default.

package main

import (
    "fmt"
    "github.com/shinyv/powershell"
)

func main() {
    // windows use local powershell
    shell,err:=powershell.Local()
    panicIF(err)
    defer shell.Close()
    
    // execute some command
    stdout, stderr, err := shell.Execute("echo foo bar")
    panicIF(err)
    fmt.Println(stdout,stderr)
}

func panicIF(e error){
    if e!=nil{
        panic(e)
    }
}
PowerShell Core

You can also interact with PowerShell Core, which normally have a binary name pwsh, use powershell.SwitchPowerShellVersion(true) before opening a new shell.

PowerShell Via SSH
package main

import (
    "fmt"
    "github.com/shinyv/powershell"
    "golang.org/x/crypto/ssh"
)

func main(){
    // open powershell core via ssh
    powershell.SwitchPowerShellVersion(true)
    sshClient,err:=ssh.Dial("tcp", "__host__:22", &ssh.ClientConfig{
        User: "__user__",
        Auth: []ssh.AuthMethod{ssh.Password("__password__")},
        HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    })
    panicIF(err)
    sess,err:=sshClient.NewSession()
    panicIF(err)
    shell,err:=powershell.SSH(sess)
    panicIF(err)
    defer shell.Close()

    // execute some command
    stdout, stderr, err := shell.Execute("echo foo bar")
    panicIF(err)
    fmt.Println(stdout,stderr)

}
func panicIF(e error){
    if e!=nil{
        panic(e)
    }
}
Remote Sessions

You can use an existing PS shell to use PSSession cmdlets to connect to remote computers. Instead of manually handling that, you can use the Session middleware, which takes care of authentication. Note that you can still use the "raw" shell to execute commands on the computer where the powershell host process is running.

package main

import (
    "fmt"
    "github.com/shinyv/powershell"
)

func main() {
    // use default backend start a local powershell process
    shell, err := powershell.Local()
    panicIF(err)
    defer shell.Close()

    // create a new shell by wrapping the existing one in the session middleware
    session, err := powershell.NewSession(shell, &powershell.SessionConfig{
        ComputerName: "remote-pc-1",
    })
    panicIF(err)
    defer session.Close() // will also close the underlying ps shell!

    // everything run via the session is run on the remote machine
    stdout, stderr, err := session.Execute("echo foo bar")
    panicIF(err)
    fmt.Println(stdout, stderr)
}
func panicIF(e error) {
    if e != nil {
        panic(e)
    }
}

License

MIT

Documentation

Index

Constants

View Source
const (
	HTTPPort  = 5985
	HTTPSPort = 5986
)

Variables

This section is empty.

Functions

func SSH

func SSH(sess SSHSession) (*shell, error)

func SwitchPowerShellVersion

func SwitchPowerShellVersion(isPowershellCore bool)

Types

type Middleware

type Middleware Shell

func NewSession

func NewSession(upstream Middleware, config *SessionConfig) (Middleware, error)

func NewUTF8

func NewUTF8(upstream Middleware) (Middleware, error)

type SSHSession

type SSHSession interface {
	StdinPipe() (io.WriteCloser, error)
	StdoutPipe() (io.Reader, error)
	StderrPipe() (io.Reader, error)
	Start(string) error
	Wait() error
}

type SessionConfig

type SessionConfig struct {
	ComputerName          string
	AllowRedirection      bool
	Authentication        string
	CertificateThumbprint string
	Credential            interface{}
	Port                  int
	UseSSL                bool
}

func (*SessionConfig) ToArgs

func (c *SessionConfig) ToArgs() []string

type Shell

type Shell interface {
	Execute(cmd string) (string, string, error)
	Close() error
}

func Local

func Local() (Shell, error)

type UserPasswordCredential

type UserPasswordCredential struct {
	Username string
	Password string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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