PowerShell – uninstall Java

It’s solution for PowerShell is a script to remove Java from remote computers.

#-------------[deklarations]--------------------------
#source
$computers=Get-Content '\\host\shares\!scripts\!source\!computers.txt'
#logs
$successLog='\\host\shares\!scripts\!logs\done.LOG'
$errorLog='\\host\shares\!scripts\!logs\error.LOG'
$unreachableLog='\\host\shares\!scripts\!logs\unreachable.LOG'
#params

#-------------[functions]--------------------------
foreach($computer in $computers) {
    if(Test-Connection -ComputerName $computer -Count 1 -Quiet) {
        Try {
            Write-host "Uninstalling: $computer" -foreground 'gray'
            Invoke-Command -computerName $computer -ScriptBlock {$app = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -match 'Java' } ; $app.Uninstall}
            Write-host "updated: $computer" -foreground 'green'
            $computer >> $successLog
        } Catch {
            Write-host "error: $computer" -foreground 'red'
            $computer >> $errorLog
        }
    } else {
        Write-host "offline: $computer" -foreground 'yellow'
        $computer >> $unreachableLog
    }
}
#-------------krolaki.eu--------------------------

Leave a Reply

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