Managing agents on multiple servers

I recently encountered a situation where i was asked to uninstall multiple agents from multiple servers. In this environment we did not have a system config manager like SCCM.

So the first task becomes to identify the magnitude of the problem i.e. to know all the servers where this application is installed. This can be done by running the following script. Here i am entering each server name you could easily enter a Get-ADComputer query, using appropriate filters or “[system.directoryservices.activedirectory.Forest]::GetCurrentForest().domains | %{$}” for DCs in a domain

$DCs = 'Server1','Server2'
$name = 'LogRhythm System Monitor Service' # Run this to find exact name of agent you wanna uninstall "Get-WmiObject -Class Win32_Product | Select-Object Name,IdentifyingNumber"
foreach ($DC in $DCs) {
if (gwmi win32_product -filter "Name = '$name'" -namespace root/cimv2 -comp $DC){
Write-Host "Present on $dc"}

Then comes the part of uninstalling the agent. This we can try doing by running below.

$list = [system.directoryservices.activedirectory.Forest]::GetCurrentForest().domains | %{$}
$name = 'LogRhythm System Monitor Service'
$list | foreach {
    $hostname = $_
    gwmi win32_product -filter "Name = '$name'" -namespace root/cimv2 -comp $_ | foreach {
        if ($_.uninstall().returnvalue -eq 0) { write-host "Successfully uninstalled $name from $($hostname)" }
        else { write-warning "Failed to uninstall $name from $($hostname)." }

There are chances that the above script fails on few hosts, but there is a workaround we can try on such hosts. Do this on the remaining servers.
Put below script in Netlogon share. This script is a modification of the above script to uninstall the agent locally on the system. In my case, i saved this script with the name Uninstalllogrhythm.ps1

gwmi win32_product -filter "Name = 'LogRhythm System Monitor Service'" -namespace root/cimv2 | foreach {
        if ($_.uninstall().returnvalue -eq 0) { write-host "Successfully uninstalled" }
        else { write-warning "Failed to uninstall" }

And run below script

$DCs = 'Remaining DCs'
$name = 'LogRhythm System Monitor Service'
foreach ($DC in $DCs) {
Invoke-Command -ComputerName $DC -ScriptBlock {Set-ExecutionPolicy -ExecutionPolicy Bypass }
Invoke-Command -ComputerName $DC -ScriptBlock { \\\netlogon\Uninstalllogrhythm.ps1 }
Invoke-Command -ComputerName $DC -ScriptBlock {Set-ExecutionPolicy -ExecutionPolicy AllSigned }

And to verify if the agent really got uninstalled from all these servers, you can make use of script #1 again.

Be the first to comment

Leave a Reply

Your email address will not be published.