Get-PowerIPSite.ps1

<#
.SYNOPSIS
Performs managment functions for Test-PowerPing Site list (Add,Remove,Import).
.DESCRIPTION
Function utilized by Test-PowerPing to manage Site List.
.NOTES
Author: Hunter Hirsch
#>

function Get-PowerIPSite {
[CmdletBinding()]
    Param (
    [Parameter(ValueFromPipeline)]
    [string]
    $Add,
    [Parameter(ValueFromPipeline)]
    [string]
    $Remove,
    [Parameter(ValueFromPipeline)]
    [object]
    $Import,
    [Parameter(ValueFromPipeline)]
    [string[]]
    $OList
    )
Process {
##Manages processes for updating PPSites.csv, containing all saved range lists.##
##Each succesfful action (add/remove/import) will update .csv and then remove function from session. Changes should be present on next function run (test-powerping).##
##Input is validated for length and excluded characters only (ppsites.csv is limited to 1mb size to prevent session bloat when running module).##
##Additional validation provided in parent function, any patterns that are not recognized are treated as single entry 'targets'.##
    if ($Add -or $Import){
        if ($Sites.Count -ge '5000'){
            Throw Write-Output '###Error: Sitelist limit reached. Please remove Sites to add New Site ###'
            }
        if ((Get-ItemProperty -Path $sitep -Name Length -ErrorAction Ignore | Select-Object -ExpandProperty Length) -ge '1000000'){
            Throw Write-Output '###Error: Sitelist size limit reached (1MB). Please remove Sites to add New Site ###'
            }
        if ($Import){
            if (Test-Path -Path $sitep -PathType Leaf){
                [int]$ilength = Get-ItemProperty -Path $sitep -Name Length -ErrorAction Ignore | Select-Object -ExpandProperty Length
                }
            }
        }
    if ($Add){
        if ($Sites.Keys -notcontains $Add){
            $Add = $Add.Trim() -replace '"','' -replace "'",''
            if ($OList.Count -gt '1'){
                $OList = $OList -join ','
                }
            if ($OList.Length -le 30000 -and $OList -notmatch  '[<>^`{|}]'){
                $Sites.Add("$Add","$OList")
                $Sites.GetEnumerator() | Select-Object -Property Name,Value | Export-Csv $sitep -NoTypeInformation -ErrorAction Stop
                Write-Output '<<< Update Successful - Module Re-import may be needed >>>'
                Remove-Item -Path Function:\Test-PowerPing
                return
                }
            }
        Else{
            Write-Output 'Check Entry and Try Again. Duplicate entry, or entry too large (must be 30000 characters or less), or entry matches excluded character(s): <>^`{|}'
            return
            }
        }
Elseif ($Remove){
        if ($Sites.Keys -contains $Remove -and $Remove -ne 'NewSite'){
            $Sites.Remove("$Remove")
            $Sites.GetEnumerator() | Select-Object -Property Name,Value | Export-Csv $sitep -NoTypeInformation -ErrorAction Stop
            Write-Output '<<< Update Successful - Module Re-import may be needed >>>'
            Remove-Item -Path Function:\Test-PowerPing
            return
            }
        Else{
            Write-Output 'No entry found matching RemoveSite name, check value and try again - "NewSite" cannot be modified'
            return
            }
        }
Elseif ($Import){
        if (!$ilength){
            [int]$ilength = 0
            }
        if ($Import.GetType().name -eq 'Hashtable'){
            $vtest = $Import.values | ForEach-Object {
                if ($_.count -gt 1){
                    $length = ($_ -join ',').Length
                    }
                Else{
                    $length = $_.length
                    }
                $ilength = $ilength + $length
                if ($length -le 30000){
                    $true
                    }
                Else{
                    $false
                    }
                if ($_ -notmatch '[<>^`{|}]'){
                    $true
                    }
                Else{
                    $false
                    }
                }
            $klist = $Import.Keys | Where-Object {$Sites.Keys -notcontains $_}
            if ($vtest -notcontains $false -and $klist.count -gt '0' -and $ilength -le 999000){
                $klist | ForEach-Object {$Sites.Add("$_",$import[$_])} -ErrorAction Stop
                $Sites.GetEnumerator() | Select-Object -Property Name,Value | Export-Csv $sitep -NoTypeInformation -ErrorAction Stop
                Write-Output '<<< Update Successful - Module Re-import may be needed >>>'
                Remove-Item -Path Function:\Test-PowerPing
                return
                }
            Else{
                Write-Output 'Bad Import List. Review List for Errors/Duplicates and try again. Enter "Get-Help Test-PowerPing -Examples" for entry syntax (Example#:4), "Get-Help Test-PowerPing -Parameter Rangelist" for rangelist syntax.'
                return
                }
            }
        if ($Import.GetType().name -eq 'Object[]'){
            $vtest = $Import.value | ForEach-Object {
                if ($_.count -gt 1){
                    $length = ($_ -join ',').Length
                    }
                Else{
                    $length = $_.length
                    }
                $ilength = $ilength + $length
                if ($length -le 30000){
                    $true
                    }
                Else{
                    $false
                    }
                if ($_ -notmatch '[<>^`{|}]'){
                    $true
                    }
                Else{
                    $false
                    }
                }
            $klist = $Import | Where-Object {$Sites.Keys -notcontains $_.name}
            if ($vtest -notcontains $false -and $klist.count -gt '0' -and $ilength -le 999000){
                    $klist | ForEach-Object {$Sites.Add($_.name,$_.value)} -ErrorAction Stop
                    $Sites.GetEnumerator() | Select-Object -Property Name,Value | Export-Csv $sitep -NoTypeInformation -ErrorAction Stop
                    Write-Output '<<< Update Successful - Module Re-import may be needed >>>'
                    Remove-Item -Path Function:\Test-PowerPing
                    return
                    }
            Else{
                Write-Output 'Bad Import List. Review List for Errors/Duplicates and try again. Enter "Get-Help Test-PowerPing -Examples" for entry syntax (Example#:4), "Get-Help Test-PowerPing -Parameter Rangelist" for rangelist syntax.'
                return
                }
            }
        }
    }
}