【问题标题】:Uninstalling Java 6 and Reinstalling Java 7 using Powershell使用 Powershell 卸载 Java 6 并重新安装 Java 7
【发布时间】:2013-02-28 14:38:42
【问题描述】:

我正在使用以下 Powershell 脚本。前半部分(卸载)完美无缺。后半部分(安装)仅在我允许用户输入时才有效。任何人都可以提供一些帮助吗?这是脚本:(抱歉格式不好)

#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"{ 
    }

【问题讨论】:

  • 对于格式化,去掉所有的勾号,只需将代码缩进4个空格,就会自动格式化为代码。
  • “允许用户输入”是什么意思?他们是否需要在向导中单击“下一步”?您在安装代码中缺少像“/qn”这样的静默参数。另外,当Java在exe文件jre..blah.blah.exe /s中有自己的静默触发器时,为什么要使用msiexec来安装阅读更多:java.com/en/download/help/silent_install.xml

标签: java powershell installation uninstallation


【解决方案1】:
#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

至于您的脚本,将#Install 行更改为:

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

Best Practice use mostly single quotes

【讨论】:

  • 我已经通过 MSI 和 .exe 进行了尝试 - 谁能向我解释为什么这样有效: Start-Process -FilePath "msiexec.exe" -ArgumentList "/i "C:\temp\jre1.7.0_17.msi"" -Credential $cred -wait 但这不会: Start-Process -FilePath "msiexec.exe" -ArgumentList "/i "C:\temp\jre1.7.0_17.msi" ""/qn REBOOT=ReallySuppress JAVAUPDATE=0 WEBSTARTICON=0 SYSTRAY=0" ""/log c:\temp\javainst.log" -Credential $cred -等待
  • 您最好编写另一个脚本,在安装后更改 Java 安装的属性。当您打开一个 DOS 窗口并运行命令 msiexec /i "C:\jre17.msi" /qn REBOOT=ReallySuppress JAVAUPDATE=0 ... 时,它会起作用吗? Oracle 没有提供太多关于 MSI 的文档。他们甚至提供 MSI 吗?
猜你喜欢
  • 2015-01-31
  • 1970-01-01
  • 1970-01-01
  • 2015-01-16
  • 2013-05-29
  • 1970-01-01
  • 2014-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多