Skip to content

Commit 8da227b

Browse files
committed
add simple example and update docs
1 parent 70e2af0 commit 8da227b

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

azure-pipelines.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ steps:
8080
script: 'go test github.com/KnicKnic/go-powershell/tests'
8181
- task: CmdLine@2
8282
inputs:
83-
script: 'go build -a -o go-powershell.exe .\test_app'
83+
script: 'go build -a -o go-powershell.exe .\test_app\cmd'
8484
- task: PowerShell@2
8585
inputs:
8686
targetType: 'inline'

readme.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,61 @@
11
[![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)
22

33
# Status
4-
This project is not api stable. It does work(can call scripts, communicate from golang to powershell, and powershell back to golang).
4+
It works
5+
1. call scripts / cmdlets
6+
1. reuse variables between calls / invocation
7+
1. communicate from golang to powershell
8+
1. communicate from powershell to golang
9+
1. trap host output in powershell and call custom logging routines in golang
10+
1. has tests
11+
1. Docs - if you missed the badge above go to https://godoc.org/github.com/KnicKnic/go-powershell
12+
13+
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.
14+
15+
**TODO:**
16+
1. add some code for easy back and forth json serialization of complex objects
17+
2. more examples / tests
18+
3. example / helper classes around exception
19+
4. a doc overview
520

621
# Goal
722
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.
823

24+
# Usage
25+
```go
26+
package main
27+
28+
import (
29+
"fmt"
30+
powershell "github.com/KnicKnic/go-powershell"
31+
)
32+
33+
func main() {
34+
// create a runspace (where you run your powershell statements in)
35+
runspace := powershell.CreateRunspaceSimple()
36+
// auto cleanup your runspace
37+
defer runspace.Delete()
38+
39+
// execute a statement in powershell consisting of "emit this string"
40+
// this will output that object into the results
41+
results := runspace.ExecStr( `"emit this string"`, true)
42+
// auto cleanup all results returned
43+
defer results.Close()
44+
45+
// print the string result of the first object
46+
fmt.Println(results.Objects[0].ToString())
47+
// Output: emit this string
48+
}
49+
```
50+
951
## Dependencies
1052
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.
1153

1254
### Using native-powershell
13-
1. copy host.h into the powershell folder
55+
1. copy host.h into this root folder
1456
1. Copy the compiled psh_host.dll into
15-
1. powershell folder
16-
1. any folder that uses the powershell package
57+
1. this root folder
58+
1. the location you compile your golang app from
1759
1. the same folder where you distribute the golang binary
1860

1961
### Getting cgo (so you can compile)

test_app/main.go renamed to test_app/cmd/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package main
22

3-
// "bitbucket.org/creachadair/shell"
43
import (
54
"flag"
65
"fmt"

test_app/simple/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
powershell "github.com/KnicKnic/go-powershell"
6+
)
7+
8+
func main() {
9+
// create a runspace (where you run your powershell statements in)
10+
runspace := powershell.CreateRunspaceSimple()
11+
// auto cleanup your runspace
12+
defer runspace.Delete()
13+
14+
// execute a statement in powershell consisting of "emit this string"
15+
// this will output that object into the results
16+
results := runspace.ExecStr( `"emit this string"`, true)
17+
// auto cleanup all results returned
18+
defer results.Close()
19+
20+
// print the string result of the first object
21+
fmt.Println(results.Objects[0].ToString())
22+
// Output: emit this string
23+
}

0 commit comments

Comments
 (0)