Skip to content

Commit

Permalink
add simple example and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
KnicKnic committed Jul 3, 2019
1 parent 70e2af0 commit 8da227b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ steps:
script: 'go test github.com/KnicKnic/go-powershell/tests'
- task: CmdLine@2
inputs:
script: 'go build -a -o go-powershell.exe .\test_app'
script: 'go build -a -o go-powershell.exe .\test_app\cmd'
- task: PowerShell@2
inputs:
targetType: 'inline'
Expand Down
50 changes: 46 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,61 @@
[![Build Status](https://dev.azure.com/oneeyedelf1/powershell.native/_apis/build/status/KnicKnic.go-powershell?branchName=master)](https://dev.azure.com/oneeyedelf1/powershell.native/_build/latest?definitionId=3&branchName=master) [![Go Report Card](https://goreportcard.com/badge/github.com/KnicKnic/go-powershell)](https://goreportcard.com/report/github.com/KnicKnic/go-powershell) [![GoDoc](https://godoc.org/github.com/KnicKnic/go-powershell?status.svg)](https://godoc.org/github.com/KnicKnic/go-powershell)

# Status
This project is not api stable. It does work(can call scripts, communicate from golang to powershell, and powershell back to golang).
It works
1. call scripts / cmdlets
1. reuse variables between calls / invocation
1. communicate from golang to powershell
1. communicate from powershell to golang
1. trap host output in powershell and call custom logging routines in golang
1. has tests
1. Docs - if you missed the badge above go to https://godoc.org/github.com/KnicKnic/go-powershell

This project is not api stable, however I believe it will be simple if you do use the current api to migrate to any future changes.

**TODO:**
1. add some code for easy back and forth json serialization of complex objects
2. more examples / tests
3. example / helper classes around exception
4. a doc overview

# Goal
The goal of this project is to enable you to quickly write golang code and interact with windows via powershell. Because powershell is a powerful scripting language you will sometimes want to call back into golang. This is also permitted. Also due to sometimes wanting to host .net and powershell giving you an easy way to wrap .net modules and functions and objects, this project also enables that.

# Usage
```go
package main

import (
"fmt"
powershell "github.com/KnicKnic/go-powershell"
)

func main() {
// create a runspace (where you run your powershell statements in)
runspace := powershell.CreateRunspaceSimple()
// auto cleanup your runspace
defer runspace.Delete()

// execute a statement in powershell consisting of "emit this string"
// this will output that object into the results
results := runspace.ExecStr( `"emit this string"`, true)
// auto cleanup all results returned
defer results.Close()

// print the string result of the first object
fmt.Println(results.Objects[0].ToString())
// Output: emit this string
}
```

## Dependencies
This project has a dependency on [native-powershell](https://github.com/KnicKnic/native-powershell). This is a c++/cli project that enables interacting with powershell through a C DLL interface.

### Using native-powershell
1. copy host.h into the powershell folder
1. copy host.h into this root folder
1. Copy the compiled psh_host.dll into
1. powershell folder
1. any folder that uses the powershell package
1. this root folder
1. the location you compile your golang app from
1. the same folder where you distribute the golang binary

### Getting cgo (so you can compile)
Expand Down
1 change: 0 additions & 1 deletion test_app/main.go → test_app/cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package main

// "bitbucket.org/creachadair/shell"
import (
"flag"
"fmt"
Expand Down
23 changes: 23 additions & 0 deletions test_app/simple/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
powershell "github.com/KnicKnic/go-powershell"
)

func main() {
// create a runspace (where you run your powershell statements in)
runspace := powershell.CreateRunspaceSimple()
// auto cleanup your runspace
defer runspace.Delete()

// execute a statement in powershell consisting of "emit this string"
// this will output that object into the results
results := runspace.ExecStr( `"emit this string"`, true)
// auto cleanup all results returned
defer results.Close()

// print the string result of the first object
fmt.Println(results.Objects[0].ToString())
// Output: emit this string
}

0 comments on commit 8da227b

Please sign in to comment.