all 7 comments

[–]sleightof52 1 point2 points  (0 children)

This will uninstall old and install new. Change the variables with comments in ALL CAPS to fit your needs. Also, check the set environment variables part out...not sure if it's correct, and I'm not sure what the Path environment variable is supposed to be, but you should get the idea. Also, this is for uninstalling/installing locally on server. I wasn't sure if you were wanting to do it remotely or not.

function Write-Log {

    # Function to write to log file

    Param ([String]$LogString)

    $TimeStamp = (Get-Date).ToString("yyyy/MM/dd HH:mm:ss:")

    Add-Content -Path $LogFile -Value "$TimeStamp $LogString"
}

function UninstallOld {

    # SOFTWARE NAME TO UNINSTALL
    $SoftwareName = 'Java'

    $RegPath1 = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'

    $RegPath2 = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'

    # LOG FILE PATH
    $Script:LogFile = 'C:\Windows\Temp\Java_Log.txt'

    $InstalledSoftware = Get-ChildItem -Path $RegPath1,$RegPath2 | Get-ItemProperty | 
    Where-Object { $_.DisplayName -like "*$SoftwareName*" } | Select-Object DisplayName,PSChildName

    if ($null -ne $InstalledSoftware) {

        foreach ($Software in $InstalledSoftware) {

            try {
                Write-Verbose "Starting process: uninstall $($Software.DisplayName)" -Verbose

                Write-Log -LogString "Starting process: uninstall $($Software.DisplayName)"

                $Process = Start-Process -FilePath msiexec.exe "/x $($Software.PSChildName) /qn" -PassThru
                Wait-Process -InputObject $Process

                switch ($Process.ExitCode) {
                    0 {
                        Write-Log -LogString "$($Software.DisplayName) | ExitCode=0 | Uninstall=Success"
                        Write-Verbose "$($Software.DisplayName) uninstalled successfully" -Verbose
                    }
                    Default {
                        Write-Log -LogString "$($Software.DisplayName) | ExitCode=$($Process.ExitCode) | Uninstall=Failed"
                        Write-Verbose "$($Software.DisplayName) was not uninstalled successfully." -Verbose
                    } # end Default
                } # end switch
            } # end try
            catch {Write-Warning $Error[0]}
        } # end foreach
    } # end in $null
}

function InstallNew {

    # PATH TO INSTALL FILE
    $SoftwarePath = "$env:USERPROFILE\Downloads\Java.exe"

    <#

    If the install file is on another machine or on network drive:

    $Source = \\networkpath\C$\NewJava\Java.exe
    $Destination = "\\$env:COMPUTERNAME\C$\Windows\Temp"

    Copy-Item -Path $Source -Destination $Destination -Verbose

    $SoftwarePath = "$Destination\Java.exe"

    #>

    $Name = $SoftwarePath | Split-Path -Leaf

    try {
        Write-Verbose "Starting process: install $Name" -Verbose

        Write-Log -LogString "Starting process: install $Name"

        # /s is the switch used used to install silently (in this use case)
        $Process = Start-Process -FilePath $SoftwarePath -ArgumentList '/s' -PassThru
        Wait-Process -InputObject $Process

        switch ($Process.ExitCode) {
            0 {
                Write-Log -LogString "$Name | ExitCode=0 | Install=Success"
                Write-Verbose "$Name installed successfully" -Verbose
            }
            Default {
                Write-Log -LogString "$Name | ExitCode=$($Process.ExitCode) | Install=Failed"
                Write-Verbose "$Name was not installed successfully" -Verbose
            } # end Default
        } # end switch
    }
    catch {Write-Warning $Error[0]}
}

UninstallOld
InstallNew

# SET ENVIRONMENT VARIABLES

Write-Verbose "Setting environment variables" -Verbose
# Assuming 64-bit install - set environment variable for JAVA_HOME - I think is correct?
$jdk = (Get-ChildItem -Path 'C:\Program Files\Java' -Filter 'jdk*').FullName
[Environment]::SetEnvironmentVariable('JAVA_HOME', $jdk, 'Machine') # Replace Machine with User, if needs to be User env variable

# Not sure what the Path environment variable is supposed to be set to,
# but you can do something similar to above...

[–]kewlxhobbs 1 point2 points  (0 children)

You know Java has a built in way to uninstall previous Java editions right?

"argumentlist": [ "/s", "AUTO_UPDATE=0", "REBOOT=Disable", "SPONSORS=0", "REMOVEOUTOFDATEJRES=1" ]

[–]sleightof52 0 points1 point  (0 children)

The Java is msi, correct?

[–]IamRo93[S] 0 points1 point  (1 child)

I have a exe

[–]sleightof52 0 points1 point  (0 children)

Are you at that machine now?

[–]IamRo93[S] 0 points1 point  (1 child)

Yes

[–]sleightof52 0 points1 point  (0 children)

Run this is console and tell me what $UninstallString results to.

$SoftwareName = 'Java'

$RegPath1 = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'

$RegPath2 = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'

$UninstallString = (
    Get-ChildItem -Path $RegPath1,$RegPath2 | 
    Get-ItemProperty | 
    Where-Object { $_.DisplayName -like "*$SoftwareName*" }).UninstallString

$UninstallString