Skip to content

Use PowerShell for API requests

Adapt the following procedure to interact with the Zenoss Resource Manager or Collection Zone instances using PowerShell. This example creates an event, but it can easily be modified by changing the router URL and JSON payload.

  1. Build headers for authorization.

    $user = "myApiUsername"
    $pass = "my$up3rS3cUr3ApiP@$$w0rd"
    $pair = "${user}:${pass}"
    
    $bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
    $base64 = [System.Convert]::ToBase64String($bytes)
    
    $basicAuthValue = "Basic $base64"
    
    $headers = @{ Authorization = $basicAuthValue }
    
  2. Create the JSON format (nested JSON in PowerShell):

    $hash = @([Ordered]@{ action = "EventsRouter";
                method = "add_event"
                data = @(
                    [Ordered]@{
                        summary = "Hard drive failed"
                        device = "my_server.company.com"
                        component = "disk0"
                        severity = "Critical"
                        evclasskey = ""
                        evclass = "/Status/Storage"
                    }
                )
            type = "rpc"
            tid = 1
            }
        )
    
    $JSON = $hash | convertto-json -Depth 2
    
  3. Test the output:

    PS C:\Users\zuser> $JSON
    

    Example output:

    {
        "action":  "EventsRouter",
        "method":  "add_event",
        "data":  [
                     {
                         "summary":  "bad stuff happened",
                         "device":  "my_server.example.com",
                         "component":  "",
                         "severity":  "Critical",
                         "evclasskey":  "",
                         "evclass":  "/App"
                     }
                 ],
        "type":  "rpc",
        "tid":  1
    }
    
  4. (Optional) Disable SSL certificate validation:

    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
    
  5. Invoke the web request:

    Invoke-WebRequest -UseBasicParsing -Uri "https://my_zenoss_instance.company.com/zport/dmd/evconsole_router" -Headers $headers -Method Post -Body $JSON -ContentType "application/json"
    
  6. View the response:

    StatusCode        : 200
    StatusDescription : OK
    Content           : {"uuid": "0242ac11-000b-8632-11e9-463e422701b7", "action": "EventsRouter", "result": {"msg": "Created event", "success": true}, "tid": 1, "type": "rpc", "method": "add_event"}
    RawContent        : HTTP/1.1 200 OK
                        Vary: Accept-Encoding
                        X-Frame-Options: SAMEORIGIN
                        X-Xss-Protection: 1; mode=block
                        Content-Length: 175
                        Content-Type: application/json
                        Date: Fri, 14 May 2019 08:21:37 GMT
                        Server: ...
    Forms             :
    Headers           : {[Vary, Accept-Encoding], [X-Frame-Options, SAMEORIGIN], [X-Xss-Protection, 1; mode=block],
                        [Content-Length, 175]...}
    Images            : {}
    InputFields       : {}
    Links             : {}
    ParsedHtml        :
    RawContentLength  : 175