# disableForticlientWebfiltering.ps1
# Note: untested script - use as sample as this snippet is incomplete

$forticlientConfigFile="\\FILESHERVER009\forticlient.config"

function confirmation($content,$testValue="I confirm",$maxAttempts=3){
    $confirmed=$false;
    $attempts=0;        
    $content|write-host
    write-host "Please review this content for accuracy.`r`n"
    while ($attempts -le $maxAttempts){
        if($attempts++ -ge $maxAttempts){
            write-host "A maximum number of attempts have reached. No confirmations received!`r`n"
            break;
            }
        $userInput = Read-Host -Prompt "Please type in this value => $testValue <= to confirm";
        if ($userInput.ToLower() -ne $testValue.ToLower()){
            cls;
            $content|write-host
            write-host "Attempt number $attempts of $maxAttempts`: $userInput does not match $testValue. Try again..`r`n"
            }else{
                $confirmed=$true;
                write-host "Confirmed!`r`n";
                break;
                }
        }
    return $confirmed;
}

function disableForticlientWebfiltering($forticlientConfigFile){    
    $forticlientConfig=get-content $forticlientConfigFile
    $hasWebfilterSettings=$forticlientConfig -match 'webfilter'

    if($hasWebfilterSettings){
        $forticlientConfig=$forticlientConfig -replace '<enable_filter>\d{1}','<enable_filter>0'
        }else{
        $webFilter=@"
    <webfilter>
        <enable_filter>0</enable_filter> 
    </webfilter>
</configuration>
"@
        $forticlientConfig=$forticlientConfig -replace '</configuration>',$webFilter
        }
    
    $confirmOverwrite=confirmation -content $forticlientConfig
    if($confirmOverwrite){
        $backupFile=$forticlientConfigFile+"_backup"
        try{
            Rename-Item -Path $forticlientConfigFile -NewName $backupFile -ErrorAction Stop
            }
            catch{
                Remove-item $backupFile -Force
                Rename-Item -Path $forticlientConfigFile -NewName $backupFile
                }
        $forticlientConfig|set-Content $forticlientConfigFile
        write-host "$record has been added to $hostfile successfully."
        return $true
        }else{
            write-host "No changes were made as there were no confirmations."
            return $false;
            }
        
}

disableWebfiltering