From 8da227b3876f6a2450bde175d908f0505662daad Mon Sep 17 00:00:00 2001 From: KnicKnic Date: Wed, 3 Jul 2019 09:10:10 -0700 Subject: [PATCH] add simple example and update docs --- azure-pipelines.yml | 2 +- readme.md | 50 +++++++++++++++++++++++++++++++++++--- test_app/{ => cmd}/main.go | 1 - test_app/simple/main.go | 23 ++++++++++++++++++ 4 files changed, 70 insertions(+), 6 deletions(-) rename test_app/{ => cmd}/main.go (98%) create mode 100644 test_app/simple/main.go diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 6851332..b9fdf76 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -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' diff --git a/readme.md b/readme.md index dfd6a22..7781506 100644 --- a/readme.md +++ b/readme.md @@ -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) diff --git a/test_app/main.go b/test_app/cmd/main.go similarity index 98% rename from test_app/main.go rename to test_app/cmd/main.go index e813d39..1009ac2 100644 --- a/test_app/main.go +++ b/test_app/cmd/main.go @@ -1,6 +1,5 @@ package main -// "bitbucket.org/creachadair/shell" import ( "flag" "fmt" diff --git a/test_app/simple/main.go b/test_app/simple/main.go new file mode 100644 index 0000000..98f607e --- /dev/null +++ b/test_app/simple/main.go @@ -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 +}