使用Powershell卸载Java 6并重新安装Java 7
我正在使用以下Powershell脚本。上半场(卸载)完美地运作。后半部分(安装)只有在允许用户输入的情况下才有效。任何人都可以提供协助吗?下面是脚本:(抱歉格式差)使用Powershell卸载Java 6并重新安装Java 7
#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"{
}
#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
我已经通过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 -wait – user2162722 2013-03-13 17:04:20
您可能最好写另一个脚本来改变当你打开一个DOS窗口并运行命令'msiexec/i“C:\ jre17.msi”/ qn REBOOT = ReallySuppress JAVAUPDATE = 0 ...'这是否正常工作?太多的文件与MSI一起。他们甚至提供MSI吗? – 2013-03-19 18:13:35
对于格式,摆脱所有的刻度线,并简单地缩进代码4个空格,并且它将自动被格式化为代码。 – mellamokb 2013-03-12 21:09:51
“允许用户输入”是什么意思?他们是否需要点击向导中的“下一步”?您在安装代码中缺少像'/ qn'这样的无声参数。此外,为什么当Java在exe文件“jre..blah.blah.exe/s'中拥有自己的无声触发器时使用'msiexec'来安装更多信息:http://java.com/en/download/help/ silent_install.xml – 2013-03-12 22:24:30