$ErrorActionPreference = 'Stop'
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Start-Process powershell.exe -Verb RunAs -ArgumentList '-NoProfile','-ExecutionPolicy','Bypass','-File',('"' + $MyInvocation.MyCommand.Path + '"')
    exit
}
$installDirectory = Join-Path $env:ProgramFiles 'Prefeitura de Extrema\Agente TI Windows'
$dataDirectory = Join-Path $env:ProgramData 'PrefeituraExtrema\AgenteTIWindows'
$taskName = 'Prefeitura de Extrema - Inventario TI Windows'
$uninstallKey = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\AgenteTIExtremaWindows'
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $uninstallKey -Recurse -Force -ErrorAction SilentlyContinue
Write-Host 'Tarefa automatica removida. O historico permanece no servidor.' -ForegroundColor Yellow
Write-Host "Para remover os arquivos locais apos fechar esta janela: $installDirectory e $dataDirectory"
Start-Process powershell.exe -WindowStyle Hidden -ArgumentList '-NoProfile','-Command',"Start-Sleep -Seconds 2; Remove-Item -LiteralPath '$installDirectory' -Recurse -Force; Remove-Item -LiteralPath '$dataDirectory' -Recurse -Force" 
