Posted On May 21, 2020

PowerShell: Disable Forticlient Web Filtering

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Disable Forticlient Web Filtering
# 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

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Hyper-V: Attach a New Virtual Disk

$vmName='TestVm' $storageLocation='\\FILESERVER06\SHAREXOXO' $newDiskSize='100GB' $dynamic=$true function createNewDisk{ param( $vmName, $storageLocation, $newDiskSize='100GB', $dynamic=$true ) $ErrorActionPreference='stop' try{ write-host…

PowerShell: DHCP Server Migration

Nowadays, being lazy is good. Why waste energy with clicking buttons when there are a…

PowerShell: Remove or Disable Windows Defender

$username='domain\serviceAccount' $password='PasswordHere' $encryptedPassword=ConvertTo-SecureString $password -AsPlainText -Force $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName,$encryptedPassword; $computerNames=@( 'SERVER01',…