TECHZEN Zenoss User Community ARCHIVE  

Powershell V2 to get events

Subject: Powershell V2 to get events
Author: Renee Wayland
Posted: 2017-07-19 14:01

How do you get events from Zenoss using Powershell V2?   I have tried various attempts at passing data like " {"action":"EventsRouter","method":"detail","data":[{"evid":"022221-0012-a35b-11e7-6afssdfsfe9"}], "tid":1}" and the credentials to System.Net.HttpWebRequest with no success.

------------------------------
Renee
------------------------------


Subject: RE: Powershell V2 to get events
Author: Renee Wayland
Posted: 2017-07-19 14:31

I answered my own question and now have this working.  This is not the finished product but it will get the event details for a specific evid.
param($evid,$URL,$username,$password)

$pair = "${username}:${password}"
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)

$basicAuthValue = "Basic $base64"
$headers = @{ Authorization = $basicAuthValue }

$DATA= '{"evid":"' + $evid + '"}'
$DATA='['+$DATA+']'
$JSON = '{"action":"EventsRouter","method":"detail","data":' + $DATA + ', "tid":1}'

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

$URI1 = "$URL/zport/dmd/evconsole_router"
$request = [System.Net.HttpWebRequest]::Create($URI1)
$request.ServicePoint.ConnectionLimit =10;
$conn=$request.ServicePoint.CurrentConnections
$UserAgent = "AO Agent";
$request.UserAgent = $("{0} (PowerShell {1}; .NET CLR {2}; {3})" -f $UserAgent, $(if($Host.Version){$Host.Version}else{"1.0"}),
[Environment]::Version,
[Environment]::OSVersion.ToString().Replace("Microsoft Windows ", "Win"));

$request.PreAuthenticate = $false;
$request.Headers.Add("Authorization", "Basic $base64")
$request.Pipelined = $true;
#$request.UnsafeAuthenticatedConnectionSharing = $true;
$request.AllowAutoRedirect = $true;
$request.Method = "POST";
$request.ContentType = "application/json";
$request.Accept = "application/json";
$request.KeepAlive = $true;
$request.ServicePoint.MaxIdleTime = 5000;
$request.ServicePoint.ConnectionLeaseTimeout = 5000;
$request.ServicePoint.Expect100Continue = "false"
$request.ContentLength = $JSON.Length
write-output "request is"
$request
try
{
$requestStream = $request.GetRequestStream()
$streamWriter = New-Object System.IO.StreamWriter($requestStream)
$streamWriter.Write($JSON)
#$requestStream.Close();
# $streamWriter.Dispose();
}
catch [System.Net.WebException] {
$Request = $_.Exception
Write-host "Exception caught: $Request"
}
finally
{
if ($null -ne $streamWriter) { $streamWriter.Dispose() }
if ($null -ne $requestStream) { $requestStream.Dispose() }
}
$response = $request.GetResponse()
$requestStream = $response.GetResponseStream()
$readStream = New-Object System.IO.StreamReader $requestStream
$results=$readStream.ReadToEnd()
write-output "results"
$results


------------------------------
Renee
------------------------------


Subject: RE: Powershell V2 to get events
Author: Jane Curry
Posted: 2017-08-02 10:16

It isn't in Powershell but Python; however you might look at  https://gist.github.com/jcurry/ebb20c6665ba249bce55 on Gist for a parameterised script to drag events out of Zenoss using the JSON API.

Cheers,
Jane

------------------------------
Jane Curry
Skills 1st United Kingdom
jane.curry@skills-1st.co.uk
------------------------------


< Previous
Cannot install microsoft zenpack 2.7.8
  Next
JSON API
>