public/Get-D4IOTDeviceEvents.ps1


function Get-D4IOTDeviceEvents {
    <#
    .SYNOPSIS
    Get-D4IOTDevices
    .DESCRIPTION
    Get-D4IOTDevices
    .PARAMETER HostName
    Enter the subscription ID, if no subscription ID is provided then current AZContext subscription will be used
    .PARAMETER APIKey
    Enter the Workspace name
    #>


    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
    param (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string] $HostName,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$APIKey,

        [Parameter(Mandatory = $false,
            ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [int]$DeviceId
    )

    begin {
        #precheck
    }

    process {

        Write-Verbose -Message "Starting Get-D4IOTDeviceEvents Method"

            try {
                add-type @"
                using System.Net;
                using System.Security.Cryptography.X509Certificates;
                public class TrustAllCertsPolicy : ICertificatePolicy {
                    public bool CheckValidationResult(
                    ServicePoint srvPoint, X509Certificate certificate,
                    WebRequest request, int certificateProblem) {
                    return true;
                                                                }
                                         }
"@

                [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
                $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
                #$headers.Add("Authorization", "3fa31c74e083409dba9de80942d05ebb")
                $headers.Add("Authorization", "$APIKey")


                Write-Debug -Message "$APIKey"
                Write-Debug -Message "$HostName"

              
                $BaseUri=[System.Uri]"https://$HostName//api//v1//events";
               
                #$jsonresponse = Invoke-RestMethod 'https://192.168.1.100//api//v1//devices' -Method 'GET' -Headers $headers
                $jsonresponse = Invoke-RestMethod $BaseUri -Method 'GET' -Headers $headers
                $jsonresponse = $jsonresponse | ConvertTo-Json
                return $jsonresponse;
            }
            catch {
                $return = $_.Exception.Message
                Write-Verbose $_
                Write-Error "error in Get-D4IOTDeviConnections"
                return "Error";
            }
    }
}