使用PowerShell进行静默安装

时间:2017-11-01 13:01:49

标签: powershell powershell-v3.0 powershell-v4.0

I am trying to install software using powershell silent scripting. To install this software we need to have JRE installed on machine. For this first we need to check weather JRE installed or not, if not installed then it needs to be installed. What approach needs to be followed?

I have tried with the below of code.
$LASTEXITCODE = 0
$workdir = "C:\Program Files (x86)\Java"

If (!(Test-Path $workdir))
{ 
 $LASTEXITCODE = (Start-Process "D:\jre-6u26-windows-i586.exe" -ArgumentList "/s" -Wait -PassThru).Exitcode
}
If($LASTEXITCODE -eq 0)
{
 $DCBdir = "C:\Program Files (x86)\Compart"
 If (!(Test-Path $DCBdir))
 {
 $Installer="D:\sw.exe"
 $responsefile="D:\Sresponse.varfile"
 $a=@("-q", "-varfile", "$responsefile")
 start-process $Installer -ArgumentList $a -wait
 }
}  
$chkdir = "C:\Program Files (x86)\SWFolder"
if(Test-Path -eq $chkdir)
{
 [System.Windows.MessageBox]::Show('Installation completed successfully')
}

When I run script its workingfine as it is checking the previous installation and performing installation if not found the installation. But here I am getting as issue with this code. 

如果Java安装alredy意味着它应该启动其他安装。但在我的情况下,它停止了完整的安装。 安装完成后,我需要显示“安装完成”之类的消息。但这里没有用。 AnNy在上面的代码中错了.. ??

1 个答案:

答案 0 :(得分:1)

我想使用的一个包管理器是Chocolatey,它有一个批准的JRE包,看起来像。快速检查wmi会告诉你是否安装了java:

$x = Get-WmiObject -Class Win32_Product -Filter "Name like 'Java(TM)%'" | Select -Expand Version

您还可以使用Test-Path指向您知道的包的注册表项。一旦确认机器上没有JRE,就可以调用Chocolatey进行安装。