Skip to content

Commit

Permalink
Add examples to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
willemm committed Jun 11, 2022
1 parent 689d380 commit 1d7a916
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,85 @@ func main() {
}
```

For setting winrm protocol options ([Remote Shell Options](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-wsmv/593f3ed0-0c7a-4158-a4be-0b429b597e31#Appendix_A_Target_110) and [Command Options](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-wsmv/c793e333-c409-43c6-a2eb-6ae2489c7ef4#Appendix_A_Target_117))

```go
package main
import (
"github.com/masterzen/winrm"
"fmt"
"os"
)

endpoint := winrm.NewEndpoint("localhost", 5985, false, false, nil, nil, nil, 0)

params := DefaultParameters
params.RequestOptions["WINRS_NOPROFILE"] = "TRUE"
params.RequestOptions["WINRS_CONSOLEMODE_STDIN"] = "FALSE"
params.RequestOptions["WINRS_SKIP_CMD_SHELL"] = "TRUE"

client, err := NewClientWithParameters(endpoint, "test", "test", params)
if err != nil {
panic(err)
}

_, err := client.RunWithInput("ipconfig", os.Stdout, os.Stderr, os.Stdin)
if err != nil {
panic(err)
}

```

To get command output as a byte array, use ExecuteDirect on a shell
NB: This also has a convenience function to write command strings

```go
package main

import (
"github.com/masterzen/winrm"
"fmt"
"bytes"
"log"
"os"
)

endpoint := winrm.NewEndpoint("localhost", 5985, false, false,nil, nil, nil, 0)
client , err := winrm.NewClient(endpoint, "Administrator", "secret")
if err != nil {
panic(err)
}
shell, err := client.CreateShell()
if err != nil {
panic(err)
}
var cmd *winrm.Command
cmd, err = shell.ExecuteDirect("powershell.exe", "-NonInteractive", "-NoProfile", "-Command", "-")
if err != nil {
panic(err)
}
err = cmd.SendCommand("$PSVersionTable.PSVersion | ConvertTo-Json")
if err != nil {
panic(err)
}
stdin, stderr, finished, exitcode, err := cmd.ReadOutput()
if err != nil {
panic(err)
}
log.Printf("Stdout: '%v'", string(stdout))
log.Printf("Stderr: '%v'", string(stderr))

var result map[string]int
err = json.Unmarshal(stdout, &result)
if err != nil {
panic(err)
}
log.Printf("Version: %d.%d-%d", result["Major"], result["Minor"], result["Build"])


shell.Close()
```

## Developing on WinRM

If you wish to work on `winrm` itself, you'll first need [Go](http://golang.org)
Expand Down

0 comments on commit 1d7a916

Please sign in to comment.