Scripts/Invoke-iSpyAgentApplyScheduleToCamera.ps1


Function Invoke-iSpyAgentApplyScheduleToCamera {

    <#
 
    .SYNOPSIS
        Apply schedule to the selected device
 
    .DESCRIPTION
        Apply schedule to the selected device
 
    .PARAMETER iSpyHost
        Computer name or IP (optional, default = LOCALHOST)
 
    .PARAMETER Port
        Port number to access AgentDVR (optional, default = 8090)
 
    .PARAMETER CameraId
        The Id of the camera to schedule
 
    .PARAMETER Type
        Type of object: Camera, Mic (optional, default = Camera)
 
    .EXAMPLE
        Invoke-iSpyAgentApplyScheduleToCamera -iSpyHost MyCamsSystem -CameraId 1
 
    .EXAMPLE
        Invoke-iSpyAgentApplyScheduleToCamera -iSpyHost 192.168.10.20 -CameraId 1 -Type Mic
 
    .EXAMPLE
        Invoke-iSpyAgentApplyScheduleToCamera -iSpyHost MyCamsSystem -CameraId 1, 2, 3, 4
 
    .EXAMPLE
        Invoke-iSpyAgentApplyScheduleToCamera -iSpyHost 192.168.10.20 -CameraId 1, 2, 3, 4
 
    .NOTES
        More than 1 CameraId can be entered, values must be seperated by commas
 
    .LINK
        N/A
 
    #>


    [CmdletBinding ()]

    Param (

        [Parameter (Mandatory = $False,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True,
                    HelpMessage = 'Enter computer name or IP'
                   )
        ]

        [String]$iSpyHost = 'localhost',

        [Parameter (Mandatory = $False,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True,
                    HelpMessage = 'Enter port number'
                   )
        ]

        [String]$Port = '8090',

        [Parameter (Mandatory = $True,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True,
                    HelpMessage = 'Enter camera Id'
                   )
        ]

        [String[]]$CameraId,

        [Parameter (Mandatory = $False,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True,
                    HelpMessage = 'Enter type'
                   )
        ]

        [ValidateSet ('Camera', 'Mic')]

        [String]$Type = 'Camera'

    )

    BEGIN {

        Function Show-Output ($Values) {

            [PSCustomObject]@{

                iSpyHost = $Values[0]
                Status = $Values[1]

            }

        }

    }

    PROCESS {

        Show-ProgressBar -Activity 'iSpy Agent DVR' -TotalItems 1 -Counter 1 -ProcessItem $iSpyHost.ToUpper()

        If (Test-Connection -ComputerName $iSpyHost -Count 1 -Quiet) {

            $iSpyHostURL = -join ('http://', $iSpyHost, ":$Port")

            ForEach ($Camera In $CameraId) {

                Try {

                    Switch ($Type) {

                        'Mic' { $pIndex = 1 }
                        'Camera' { $pIndex = 2 }

                    }

                    $Results = Invoke-WebRequest -Uri "$iSpyHostURL/Command/ApplyScheduleToDevice?OId=$Camera&OT=$pIndex" | ConvertFrom-Json

                    If ($Results.Error) {

                        $Status = -join ('Camera ', $Camera, ' ' , $Results.Error)

                    }

                    Else {

                        $Status = -join ('Camera ', $Camera, ' Scheduled')

                    }

                    Show-Output ($iSpyHost.ToUpper(), $Status)

                }

                Catch {

                    Show-Output ($iSpyHost.ToUpper(), $PSItem.Exception.Message)

                }

            }

        }

        Else {

            Show-Output ($iSpyHost.ToUpper(), 'Unreachable')

        }

    }

    END {}

}