$FolderToRename = "${Env:ProgramFiles}\ATERA Networks\AteraAgent\Packages\AgentPackageMonitoring"
$RenameSuffix = "-Old"
$FolderNewName = "${FolderToRename}${RenameSuffix}"

# Kill processes that can interfere
'reg', 'AgentPackageTaskManagement' | ForEach-Object { Get-Process -Name $_ -ErrorAction SilentlyContinue | Stop-Process -Force }

LogMsg "Stopping Atera agent"
Stop-Service -Name 'AteraAgent' -Force

If ( Test-Path $FolderNewName ) {
    LogMsg "Deleting previously renamed folder: ${FolderNewName}"
    Try { Remove-Item $FolderNewName -Recurse -Force }
    Catch { LogErr $_.Exception.Message }
}

LogMsg "Stopping AgentPackageMonitoring process"
Get-Process -Name "AgentPackageMonitoring" -ErrorAction SilentlyContinue | Stop-Process -Force

LogMsg "Renaming ${FolderToRename}"
Try { Rename-Item $FolderToRename $FolderNewName -Force }
Catch { LogErr $_.Exception.Message }

LogMsg "Starting Atera agent"
Start-Service -Name 'AteraAgent'