|
| 1 | +# Overview |
| 2 | + |
| 3 | +The powershell package provides a standard go-compatible method for invoking |
| 4 | +powershell on Windows. It handles errors returned by the shell and by default |
| 5 | +also handles errors thrown by cmdlets and returns them to the caller. The |
| 6 | +powershell configuration is configurable. |
| 7 | +## Features |
| 8 | + |
| 9 | +* **Simple calling, handling and defaults** - Running powershell from your go |
| 10 | +binary is as simple as making a single function call to Command. Calls to |
| 11 | +command require no additional input outside of the psCmd you wish to run. Errors |
| 12 | +from powershell are automatically handled and returned. |
| 13 | + |
| 14 | + |
| 15 | +* **Modifiable Powershell Configuration** - If a configuration other than the |
| 16 | +default is desired, the user can provide an alternative PSConfig when making |
| 17 | +powershell calls. This allows for calls to specify alternate errorActions and |
| 18 | +additional parameters. |
| 19 | + |
| 20 | + |
| 21 | +* **Customizable error handling** - The caller can specify alternate text that |
| 22 | +indicates an error as a parameter, this provides the ability to throw an error |
| 23 | +if the output of the powershell command contains output that the caller |
| 24 | +considers an error, regardless of whether an error was returned by powershell. |
| 25 | + |
| 26 | +* **Simpler Testing** - Consumers of this library can use dependency injection |
| 27 | +to mock the powershell calls, confident that handling of the powershell call |
| 28 | +itself is consistent. They can then write tests that do not need to test |
| 29 | +that powershell is run successfully, only that its output or errors are handled. |
| 30 | + |
| 31 | +## Requirements |
| 32 | + |
| 33 | +* **Windows** - Windows 10 or Windows Server 2016 with Powershell 5.0 or higher. |
| 34 | + |
| 35 | +* **Linux** - This library is not supported on Linux. |
| 36 | + |
| 37 | +## How to use this library |
| 38 | + |
| 39 | +A few example uses for this library are illustrated below. This list is not |
| 40 | +exhaustive, see the codebase for additional information. |
| 41 | + |
| 42 | +### Run a powershell command with the default configuration. |
| 43 | + |
| 44 | +This call runs a single powershell command (contained on a single line) and |
| 45 | +returns the output to the caller. |
| 46 | + |
| 47 | +``` |
| 48 | +psCmd := `Get-Process Chrome | Select ProcessName | ConvertTo-Json` |
| 49 | +
|
| 50 | +out, err := powershell.Command(psCmd, nil, nil) |
| 51 | +if err != nil { |
| 52 | + // Log Error message here. |
| 53 | +} |
| 54 | +// Process out here. |
| 55 | +``` |
| 56 | + |
| 57 | +### Run a powershell command with an alternate configuration. |
| 58 | + |
| 59 | +This call runs a single powershell command (contained on a single line), using |
| 60 | +an alternate configuration (PSConfig). |
| 61 | + |
| 62 | +``` |
| 63 | +psCmd := `Get-Process Chrome | Select ProcessName | ConvertTo-Json` |
| 64 | +cfg := &powershell.PSConfig{ErrAction: powershell.SilentlyContinue} |
| 65 | +
|
| 66 | +out, err := powershell.Command(psCmd, nil, cfg) |
| 67 | +if err != nil { |
| 68 | + // Log Error message here. |
| 69 | +} |
| 70 | +// Process out here. |
| 71 | +``` |
| 72 | + |
| 73 | +### Run a powershell command and throw an error when specific output is found. |
| 74 | + |
| 75 | +This call runs a single powershell command (contained on a single line), and |
| 76 | +specifies an error should be thrown if the output contains 'chrome'. This is to |
| 77 | +say that if a process named 'chrome' is found running, an error should be |
| 78 | +returned. |
| 79 | + |
| 80 | +``` |
| 81 | +psCmd := `Get-Process Chrome | Select ProcessName | ConvertTo-Json` |
| 82 | +suppErr := []string{"chrome"} |
| 83 | +
|
| 84 | +out, err := powershell.Command(psCmd, suppErr, nil) |
| 85 | +if err != nil { |
| 86 | + // Do something now that you've found Chrome in the output. |
| 87 | +} |
| 88 | +// Process out here. |
| 89 | +``` |
| 90 | + |
| 91 | +## Contact |
| 92 | + |
| 93 | +We have a public discussion list at |
| 94 | +[google-winops@googlegroups.com](https://groups.google.com/forum/#!forum/google-winops) |
| 95 | + |
| 96 | +## Disclaimer |
| 97 | + |
| 98 | +This is not an official Google product. |
| 99 | + |
0 commit comments