2013-03-12 93 views
-1

我正在使用以下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"{ 
    } 
+0

對於格式,擺脫所有的刻度線,並簡單地縮進代碼4個空格,並且它將自動被格式化爲代碼。 – mellamokb 2013-03-12 21:09:51

+0

「允許用戶輸入」是什麼意思?他們是否需要點擊向導中的「下一步」?您在安裝代碼中缺少像'/ qn'這樣的無聲參數。此外,爲什麼當Java在exe文件「jre..blah.blah.exe/s'中擁有自己的無聲觸發器時使用'msiexec'來安裝更多信息:http://java.com/en/download/help/ silent_install.xml – 2013-03-12 22:24:30

回答

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

+0

我已經通過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

+0

您可能最好寫另一個腳本來改變當你打開一個DOS窗口並運行命令'msiexec/i「C:\ jre17.msi」/ qn REBOOT = ReallySuppress JAVAUPDATE = 0 ...'這是否正常工作?太多的文件與MSI一起。他們甚至提供MSI嗎? – 2013-03-19 18:13:35