Skip to content

Commit 1d7a916

Browse files
committed
Add examples to readme
1 parent 689d380 commit 1d7a916

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,85 @@ func main() {
308308
}
309309
```
310310

311+
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))
312+
313+
```go
314+
package main
315+
import (
316+
"github.com/masterzen/winrm"
317+
"fmt"
318+
"os"
319+
)
320+
321+
endpoint := winrm.NewEndpoint("localhost", 5985, false, false, nil, nil, nil, 0)
322+
323+
params := DefaultParameters
324+
params.RequestOptions["WINRS_NOPROFILE"] = "TRUE"
325+
params.RequestOptions["WINRS_CONSOLEMODE_STDIN"] = "FALSE"
326+
params.RequestOptions["WINRS_SKIP_CMD_SHELL"] = "TRUE"
327+
328+
client, err := NewClientWithParameters(endpoint, "test", "test", params)
329+
if err != nil {
330+
panic(err)
331+
}
332+
333+
_, err := client.RunWithInput("ipconfig", os.Stdout, os.Stderr, os.Stdin)
334+
if err != nil {
335+
panic(err)
336+
}
337+
338+
```
339+
340+
To get command output as a byte array, use ExecuteDirect on a shell
341+
NB: This also has a convenience function to write command strings
342+
343+
```go
344+
package main
345+
346+
import (
347+
"github.com/masterzen/winrm"
348+
"fmt"
349+
"bytes"
350+
"log"
351+
"os"
352+
)
353+
354+
endpoint := winrm.NewEndpoint("localhost", 5985, false, false,nil, nil, nil, 0)
355+
client , err := winrm.NewClient(endpoint, "Administrator", "secret")
356+
if err != nil {
357+
panic(err)
358+
}
359+
shell, err := client.CreateShell()
360+
if err != nil {
361+
panic(err)
362+
}
363+
var cmd *winrm.Command
364+
cmd, err = shell.ExecuteDirect("powershell.exe", "-NonInteractive", "-NoProfile", "-Command", "-")
365+
if err != nil {
366+
panic(err)
367+
}
368+
err = cmd.SendCommand("$PSVersionTable.PSVersion | ConvertTo-Json")
369+
if err != nil {
370+
panic(err)
371+
}
372+
stdin, stderr, finished, exitcode, err := cmd.ReadOutput()
373+
if err != nil {
374+
panic(err)
375+
}
376+
log.Printf("Stdout: '%v'", string(stdout))
377+
log.Printf("Stderr: '%v'", string(stderr))
378+
379+
var result map[string]int
380+
err = json.Unmarshal(stdout, &result)
381+
if err != nil {
382+
panic(err)
383+
}
384+
log.Printf("Version: %d.%d-%d", result["Major"], result["Minor"], result["Build"])
385+
386+
387+
shell.Close()
388+
```
389+
311390
## Developing on WinRM
312391

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

0 commit comments

Comments
 (0)