Désinstallation de Java 6 et réinstallation de Java 7 à l'aide de Powershell


J'utilise le script Powershell suivant. La première moitié (désinstallation) fonctionne parfaitement. La seconde moitié (installation) ne fonctionne que si j'autorise la saisie utilisateur. Quelqu'un peut-il fournir de l'aide? Voici le script: (désolé pour une mauvaise mise en forme)

#uninstall
$java = Get-WmiObject -Class win32_product | where { $_.Name -like "*Java*"}
$msiexec = "C:\Windows\system32\msiexec.exe";
$msiexecargs = '/x "$($app.IdentifyingNumber)" /qn /norestart'

if ($java -ne $null)
{
    foreach ($app in $java)
    {
        write-host $app.LocalPackage
        write-host $app.IdentifyingNumber
        C:\Windows\system32\cmd.exe /c "C:\Windows\system32\msiexec.exe /x $($app.IdentifyingNumber) /qn"
        Start-Process -FilePath $msiexec -Arg $msiexecargs -Wait -Passthru
        [Diagnostics.Process]::Start($msiexec, $msiexecargs);
    }
}
if ($java -ne $null)
{
    foreach ($app in $java)

{
        write-host $app.LocalPackage
        write-host $app.IdentifyingNumber

C:\Windows\system32\cmd.exe /c 

"C:\Windows\system32\msiexec.exe /x $($app.IdentifyingNumber) /qn"

        Start-Process -FilePath $msiexec -Arg $msiexecargs -Wait -Passthru
        [Diagnostics.Process]::Start($msiexec, $msiexecargs);
    }
}

function Get-ScriptDirectory{
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value

try {
        Split-Path $Invocation.MyCommand.Path -ea 0
    }

catch {
    Write-Warning 'You need to call this function from within a saved script.'
    }
}

function Get-Architecture{
    return $(gwmi win32_operatingsystem).OSArchitecture
}


$Path = Get-ScriptDirectory

#Close all instances of IE, Firefox, & Chrome
Get-Process | where {$_.ProcessName -match "iexplore"} | Stop-Process -Force
Get-Process | where {$_.ProcessName -match "chrome"} | Stop-Process -Force
Get-Process | where {$_.ProcessName -match "firefox"} | Stop-Process -Force

#Install
Start-Process -FilePath "msiexec.exe" -ArgumentList "/i "C:\temp\jre1.7.0_17.msi" ""/log "c:\temp\javainst.log " -Credential $cred -wait

#Also Install the 64-bit JRE if on a 64 workstation

if(Get-Architecture -match "64")
{
    $cred = Get-Credential
    Start-Process -FilePath "msiexec.exe" -ArgumentList "/i "C:\temp\jre1.7.0_17 (x64).msi" ""/log c:\temp\javainst.log " -Credential $cred -wait
}

#Import reg keys to disable auto updating
reg import "C:\temp\JavaUpdate.reg"{ 
    }
Author: Frode F., 2013-03-13

1 answers

#uninstall everything Java
Get-WmiObject -Class win32_product | ? {$_.Name -like "*Java*"} | % {msiexec /x "$($_.IdentifyingNumber)" /qn | Out-Null}
#The Out-Null waits for the command to finish

#If you have made a Java MSI use this
msiexec /i $pathtomsi /qn

#If you only have the exe you'll need to look up the Command Line Interface (CLI) for Java
$cmd = "$pathtoexe /s"
cmd /c $cmd

Quant à votre script, changez la ligne # Install en:

Start-Process -FilePath 'msiexec.exe' -ArgumentList '/i "C:\temp\jre1.7.0_17.msi" /log "c:\temp\javainst.log" /qn' -Credential $cred -wait

Les meilleures pratiques utilisent principalement des guillemets simples

 1
Author: Mad Tom Vane, 2013-03-13 00:42:30