public/Get-D4IOTDeviceConnections.ps1


function Get-D4IOTDeviceConnections {

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

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

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

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

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

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

        [Parameter(Mandatory = $false,
        ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [bool]$IgnoreSSL
    )

    begin {
        #precheck
    }

    process {

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

            try {
             
                Write-Verbose -Message "Header Formation Started..."

                $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
                $headers.Add("Authorization", "$APIKey")


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

                Write-Verbose -Message "Parameters Formation Started...";

                $bodyContent =@{};

                if($LastActiveInMinutes -gt 0)
                {
                    $bodyContent.Add("lastActiveInMinutes", $LastActiveInMinutes);
                    Write-Verbose -Message "Parameter Added lastActiveInMinutes $LastActiveInMinutes"
                }

                
                if($DiscoveredBefore -gt 0)
                {
                    $bodyContent.Add("discoveredBefore", $DiscoveredBefore);
                    Write-Verbose -Message "Parameter Added discoveredBefore $DiscoveredBefore"
                }

                
                if($DiscoveredAfter -gt 0)
                {
                    $bodyContent.Add("discoveredAfter", $DiscoveredAfter);
                    Write-Verbose -Message "Parameter Added discoveredAfter $DiscoveredAfter"
                }

                if($DeviceId -le 0)
                {
                    $BaseUri=[System.Uri]"https://$HostName//api//v1//devices//connections";
                }
                else
                {
                    $BaseUri=[System.Uri]"https://$HostName//api//v1//devices//$DeviceId//connections";
                }

                if($IgnoreSSL)
                {
                    $jsonresponse = Invoke-RestMethod $BaseUri -Method 'GET' -Headers $headers -Body $bodyContent -SkipCertificateCheck
                }
                else
                {
                    $jsonresponse = Invoke-RestMethod $BaseUri -Method 'GET' -Headers $headers -Body $bodyContent
                }

                $jsonresponse = $jsonresponse | ConvertTo-Json
                return $jsonresponse;
            }
            catch {
                $return = $_.Exception.Message;
                Write-Verbose $_;
                Write-Error "Error in Get-D4IOTDevices $return";
                return $return ;
            }
    }
}