Created
July 29, 2020 18:15
-
-
Save KelvinTegelaar/8770e9b5053a1b46a1f2d9c852631965 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#What is an API, What is REST. | |
#Some PowerShell examples, using Netsapiens. The platform behind OITVoip. | |
#Set your credentials | |
$baseurl = "https://manage.oitvoip.com/ns-api/" | |
$ClientID = '99999demo.client' | |
$Secret = 'SuperSecret' | |
$Username = 'Username' | |
$Password = 'Password' | |
#Grab a token. | |
$tokenURL = "$baseurl/oauth2/token/?grant_type=password&client_id=$($ClientID)&client_secret=$($Secret)&username=$($Username)&password=$($Password)" | |
$Token = Invoke-RestMethod $tokenURL | |
#Explain Tokens, both refresh and access tokens. | |
$Token | |
#Now that we have the token, we can actually start doing stuff. Let's try! We're going to collect some data. Time to show documentation | |
#https://manage.oitvoip.com/ns-api/webroot/apidoc/ | |
#First, we have to create the 'header' this is the information we use for authenticating to the API. | |
$headers = @{"Authorization" = "Bearer $($token.access_token)" } | |
#We make a payload. This is how we ask the API the data we want to get. | |
$payload = @{ | |
object = 'device' | |
action = 'read' | |
domain = $token.domain | |
format = 'json' | |
} | |
$Devices = Invoke-RestMethod $baseurl -Headers $headers -Body $payload -Method Post | |
$devices | Out-GridView | |
#So lets collect more data. This time we're collecting the 'dial plan' data | |
$payload = @{ | |
object = 'dialplan' | |
action = 'read' | |
domain = $token.domain | |
format = 'json' | |
} | |
$Plans = Invoke-RestMethod $baseurl -Headers $headers -Body $payload | |
#We'll also collect all the call queue info. | |
$payload = @{ | |
object = 'callqueue' | |
action = 'read' | |
domain = $token.domain | |
format = 'json' | |
} | |
$callqueues = Invoke-RestMethod $baseurl -Headers $headers -Body $payload | |
#And the call queue statistics | |
$payload = @{ | |
object = 'callqueuestat' | |
action = 'read' | |
domain = $token.domain | |
format = 'json' | |
} | |
$callqueueStats = Invoke-RestMethod $baseurl -Headers $headers -Body $payload | |
#Lastely, Lets also collect the CDR info. As you can see, this requires extra parameters. | |
$payload = @{ | |
object = 'cdr2' | |
action = 'read' | |
domain = $token.domain | |
format = 'json' | |
'start_date' = '2020-07-01 00:00:00' | |
'end_date' = '2020-07-31 00:00:00' | |
} | |
$cdr = Invoke-RestMethod $baseurl -Headers $headers -Body $payload | |
#Now that we have all the data, lets try to get that to look pretty. | |
New-HTML { | |
New-HTMLTab -Name 'Summary' { | |
New-HTMLSection -Invisible { | |
New-HTMLSection -HeaderText 'Devices' { | |
New-HTMLTable -DataTable $Devices | |
} | |
New-HTMLSection -HeaderText 'Dial Plans' { | |
New-HTMLTable -DataTable $Plans | |
} | |
} | |
New-HTMLSection -Invisible { | |
New-HTMLSection -HeaderText "Call queues" { | |
New-HTMLTable -DataTable $callqueues | |
} | |
New-HTMLSection -HeaderText "Call Queue Stats" { | |
New-HTMLTable -DataTable $callqueueStats | |
} | |
} | |
} | |
New-HTMLTab -Name 'CDR Viewer' { | |
New-HTMLSection -HeaderText 'CDR' { | |
New-HTMLTable -DataTable $CDR { | |
} | |
} | |
} | |
} -FilePath 'C:\temp\temp1.html' -Online -ShowHTML | |
#Ok, so that all looks super complex right?! Well get this OITVoip Exclusive: I've made a PowerShell module just for anyone wanting to play with it. | |
#For example, we're going to turn on recording for devices we click on | |
Install-module PSNetsapiens | |
Connect-NSAPI | |
$Devices = (Get-NSAPI -Resource device) | Out-GridView -PassThru | |
foreach ($Device in $devices) { | |
write-host "Enabling recording for $($device.aor)" | |
new-NSAPI -Resource recording -Parameters @{'aor' = "$($device.aor)"} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
some more cool stuff you could do:
Get-NSAPI -Resource device | where-object {$_.model -like "yealink"}
or
connect-NSAPI -domain "superdomain"
Get-NSAPI -Resource device | where-object {$_.model -like "yealink"}