Skip to content

Commit aef272d

Browse files
committed
extra help around error handling and example screens
1 parent 24c3206 commit aef272d

File tree

1 file changed

+74
-6
lines changed

1 file changed

+74
-6
lines changed

README.md

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
Turn any PowerShell script into a HTTP REST API!
55

6+
### Builds
7+
8+
* __Tested Build__ https://github.com/DimensionDataCBUSydney/PowerShell.REST.API/releases
9+
* __Latest Build__ https://ci.appveyor.com/project/tonybaloney/powershell-rest-api/build/artifacts
10+
611
## Overview
712

813
This project is a HTTP service written in C#.NET using the Microsoft OWIN libraries.
@@ -14,7 +19,7 @@ The web service configures the web methods as boot based on the configuration of
1419
It hosts a PowerShell runspace pool to load, run and check PowerShell scripts, capturing errors and stack traces to the logs
1520
and parsing complex PowerShell response objects back into JSON.
1621

17-
It also supports async jobs to be run as seperate threads, with the job results to be stored on disk.
22+
It also supports async jobs to be run as separate threads, with the job results to be stored on disk.
1823

1924
## How it works
2025

@@ -89,13 +94,17 @@ First, add a WebApi element with the name __foo__
8994

9095
Then for your script, bar.ps1, by convention each script should pipe the return object through [ConvertTo-Json](https://technet.microsoft.com/en-us/library/hh849922.aspx), this is because PowerShell's dynamic objects can contain circular references and cause JSON convertors to crash.
9196

92-
Take your named parameters, for example `$message`
97+
Take your named parameters, for example `$message` and do something with them, in this example
98+
9399

94100
```powershell
95-
params(
96-
[string] $message
97-
)
98-
$message | ConvertTo-Json -Compress
101+
param (
102+
$message
103+
)
104+
# go backwards
105+
$back_message = -join $message[-1..-$message.Length]
106+
107+
@{ "message" = $back_message } | ConvertTo-Json -Compress
99108
```
100109

101110
Now, add the method to the configuration file by adding an `WebMethod` Element
@@ -119,6 +128,26 @@ Then, add a `Parameter` Element to the `Parameters` collection for each paramete
119128
</WebMethods>
120129
```
121130

131+
### Testing your script
132+
133+
Start up the API host from a console
134+
135+
```cmd
136+
.\DynamicPowerShellApi.Host.exe --console
137+
```
138+
139+
![Console](http://s28.postimg.org/4h2oquti5/ps_host_test1.png)
140+
141+
Using a tool like Postman you can check your script output
142+
143+
![Example response](http://s13.postimg.org/wab4f3cbr/ps_host_response.png)
144+
145+
returns
146+
```json
147+
{
148+
"message": "zab"
149+
}
150+
```
122151

123152
## Authentication
124153

@@ -152,4 +181,43 @@ In DynamicPowerShellApi.Owin/Startup.cs replace the existing auth configuration
152181
});
153182

154183
}
184+
```
185+
186+
## Error Handling
187+
188+
By default, any terminal errors in your powershell script will cause the HTTP response code to be HTTP500,
189+
190+
You will get the following response from the API
191+
192+
```json
193+
{
194+
"Message": "Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path '', line 1, position 5.",
195+
"Success": false,
196+
"LogFile": "bar130899475577107290.xml",
197+
"ActivityId": "bc346446-9964-4ff2-ad45-d7b13efe84b5"
198+
}
199+
```
200+
201+
Also, it will log the error in a `Logs` folder underneath the host directory.
202+
203+
### Example error log
204+
```xml
205+
<?xml version="1.0" encoding="utf-8"?>
206+
<CrashLogEntry xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
207+
<ActivityId>bc346446-9964-4ff2-ad45-d7b13efe84b5</ActivityId>
208+
<LogTime>2015-10-22T11:32:37.710729+11:00</LogTime>
209+
<RequestUrl>http://localhost:9000/api/foo/bar?message=baz</RequestUrl>
210+
<RequestAddress />
211+
<Exceptions>
212+
<PowerShellException>
213+
<ScriptName>GenericController.cs</ScriptName>
214+
<ErrorMessage>Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path '', line 1, position 5.</ErrorMessage>
215+
<LineNumber>0</LineNumber>
216+
<StackTrace> at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader)
217+
at Newtonsoft.Json.Linq.JObject.Parse(String json)
218+
at DynamicPowerShellApi.Controllers.GenericController.&lt;ProcessRequestAsync&gt;d__1f.MoveNext()</StackTrace>
219+
</PowerShellException>
220+
</Exceptions>
221+
<RequestMethod>bar</RequestMethod>
222+
</CrashLogEntry>
155223
```

0 commit comments

Comments
 (0)