Scripts/Set-iSpyAgentSystemProfile.ps1


Function Set-iSpyAgentSystemProfile {

    <#
 
    .SYNOPSIS
        Apply the selected profile to the system
 
    .DESCRIPTION
        Apply the selected profile to the system
 
    .PARAMETER iSpyHost
        Computer name or IP (optional, default = LOCALHOST)
 
    .PARAMETER Port
        Port number to access AgentDVR (optional, default = 8090)
 
    .PARAMETER Profile
        The profile to apply: Home, Away, Night, Disabled
 
    .EXAMPLE
        Set-iSpyAgentSystemProfile -iSpyHost MyCamsSystem
 
    .EXAMPLE
        Set-iSpyAgentSystemProfile -iSpyHost MyCamsSystem -Port 7000
 
    .EXAMPLE
        Set-iSpyAgentSystemProfile -iSpyHost 192.168.10.20
 
    .EXAMPLE
        Set-iSpyAgentSystemProfile -iSpyHost 192.168.10.20 -Port 7000
 
    .NOTES
        More than one host name and port can be entered,
        values must be seperated by commas:
 
        Set-iSpyAgentSystemProfile -iSpyHost MyCamsSystem1, MyCamsSystem2, MyCamsSystem3 -Profile Home
 
        Set-iSpyAgentSystemProfile -iSpyHost MyCamsSystem1, MyCamsSystem2, MyCamsSystem3 -Port 7000, 8000, 9000 -Profile Home, Night, Away
 
        Set-iSpyAgentSystemProfile -iSpyHost 192.168.10.20, 192.168.10.30, 192.168.10.40 -Profile Home
 
        Set-iSpyAgentSystemProfile -iSpyHost 192.168.10.20, 192.168.10.30, 192.168.10.40 -Port 7000, 8000, 9000 -Profile Home, Night, Away
 
    .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,

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

        [ValidateSet ('Home', 'Away', 'Night', 'Disabled')]

        [String[]]$Profile

    )

    BEGIN {

        $Index = 0
        $pIndex = 0

        Function Show-Output ($Values) {

            [PSCustomObject]@{

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

            }

        }

    }

    PROCESS {

        ForEach ($Computer In $iSpyHost) {

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

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

                Try {

                    If ($Port) {

                        $hPort = $Port[$Index]

                    }

                    If ($hPort.Length -eq 0) {

                        $hPort = '8090'

                    }

                    If ($Profile) {

                        $hProfile = $Profile[$Index]

                    }

                    If ($hProfile.Length -eq 0) {

                        $hProfile = 'Home'

                    }

                    $iSpyHostURL = -join ('http://', $Computer, ":$hPort")

                    Switch ($hProfile) {

                        'Home' { $pIndex = 0 }
                        'Away' { $pIndex = 1 }
                        'Night' { $pIndex = 2 }
                        'Disabled' { $pIndex = 3 }

                    }

                    $Results = Invoke-WebRequest -Uri $iSpyHostURL/Command/SetProfile?Ind=$pIndex | ConvertFrom-Json

                    If ($Results.Status.toUpper() -eq 'OK') {

                        $Status = 'Profile Set'

                    }

                    Else {

                        $Status = $Results.Status.toUpper()

                    }

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

                }

                Catch {

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

                }

            }

            Else {

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

            }

            $Index++

        }

    }

    END {}

}