Receive-SNSApiJson.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
function Receive-SNSApiJson { <# .SYNOPSIS Fetches the API data from scoutnet using the provided credentials. .DESCRIPTION Scoutnet API returns a JSON with the resulting data. This functions uses Invoke-WebRequest to fetch the data, and parses the returned JSON and returns a multilevel hashtable with the data. The credential for the API is configured in scoutnet. .INPUTS None. You cannot pipe objects to Receive-SNSApiJson. .OUTPUTS The multi level hashtable. .PARAMETER Uri Url to the scoutnet API for fetching the group memberlist. .PARAMETER Credential Scoutnet Credentials for the selected API. .LINK https://www.scoutnet.se .EXAMPLE $PWordMembers = ConvertTo-SecureString -String "Your api key" -AsPlainText -Force $User ="0000" # Your API group ID. $CredentialMembers = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWordMembers Receive-SNSApiJson -Credential $CredentialMembers -Uri "https://www.scoutnet.se/api/group/memberlist" #> [OutputType([System.Collections.Hashtable])] param ( [Parameter(Mandatory=$True, HelpMessage="Url to Scoutnet API for fetching the group memberlist")] [ValidateNotNull()] [string]$Uri, [Parameter(Mandatory=$True, HelpMessage="Credentials for api/group/memberlist")] [ValidateNotNull()] [pscredential]$Credential ) # Powershell version 5 do not support basic auth. # Workaround is to create the Authorization header with the data from the Credential parameter. $bytes = [System.Text.Encoding]::UTF8.GetBytes( ('{0}:{1}' -f $Credential.UserName, $Credential.GetNetworkCredential().Password) ) $Authorization = 'Basic {0}' -f ([Convert]::ToBase64String($bytes)) # Create the Authorization header. $Headers = @{ Authorization = $Authorization } # Recuest the data and convert the result to a hash table. Basic parsing is mandatory on Azure automation. $JsonHashTable = Invoke-WebRequest -Uri $Uri -Headers $Headers -UseBasicParsing -ErrorAction "Stop" | ConvertFrom-Json -ErrorAction "Stop" | ConvertTo-SNSJSONHash return $JsonHashTable } |