【问题标题】:Powershell script to install software用于安装软件的 Powershell 脚本
【发布时间】:2017-03-06 02:01:37
【问题描述】:

我正在尝试制作一个可以与 RMM 工具一起使用的 powershell 脚本。所以,基本上这个powershell脚本会在本地机器上执行。它需要检查是否安装了应用程序的版本以及至少版本号 xx。如果未安装或版本较低,则会下载可执行文件并静默安装。

我在网上找到了一个适用于 Adob​​e Reader 的示例,该示例可以正常工作,但并未事先进行检查。因此,此脚本每次运行时都会安装 Adob​​e Reader。

$tempFolder=$Env:TEMP

function runProcess ($cmd, $params) {
$p = new-object System.Diagnostics.Process
$p.StartInfo = new-object System.Diagnostics.ProcessStartInfo
$exitcode = $false
$p.StartInfo.FileName = $cmd
$p.StartInfo.Arguments = $params
$p.StartInfo.UseShellExecute = $False
$p.StartInfo.RedirectStandardError = $True
$p.StartInfo.RedirectStandardOutput = $True
$p.StartInfo.WindowStyle = 1;
$null = $p.Start()
$p.WaitForExit()
$output = $p.StandardOutput.ReadToEnd()
$exitcode = $p.ExitCode
$p.Dispose()
$exitcode
$output
}

#download installer
invoke-webrequest "ftp://ftp.adobe.com/pub/adobe/reader/win/AcrobatDC/1500720033/AcroRdrDC1500720033_en_US.msi" -OutFile "$tempFolder\AcroRdrDC1500720033_en_US.msi" -ErrorAction Stop

#run installer
$res = runProcess msiexec "/i $tempFolder\AcroRdrDC1500720033_en_US.msi /qb"

#check if return code is 0
if(0 -ne $res[0]){
return "Failed to install Adobe Reader: $($res[0]) $($res[1])"
}

#download the patch
invoke-webrequest "ftp://ftp.adobe.com/pub/adobe/reader/win/AcrobatDC/1502320070/AcroRdrDCUpd1502320070.msp" -OutFile "$tempFolder\AcroRdrDCUpd1502320070.msp" -ErrorAction Stop

#install it
$res = runProcess msiexec "/p $tempFolder\AcroRdrDCUpd1502320070.msp /qb"

#check if return code is 0
if(0 -ne $res[0]){
return "Failed to install Adobe Reader DC Patch: $($res[0]) $($res[1])"
}else{
#Attempt to silently disable the auto updater if set in this script
new-itemproperty "HKLM:\Software\Policies\Adobe\Acrobat Reader\DC\FeatureLockDown" -name bUpdater -value 0 -ErrorAction SilentlyContinue
}

我认为这个脚本中可能不需要一些东西。它也没有版本检查。

在另一个网站上找到了这个,但不知道如何实现它。此外,它看起来不像检查版本号。

function Is-Installed( $program ) {

    $x86 = ((Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall") |
        Where-Object { $_.GetValue( "DisplayName" ) -like "*$program*" } ).Length -gt 0;

    $x64 = ((Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") |
        Where-Object { $_.GetValue( "DisplayName" ) -like "*$program*" } ).Length -gt 0;

    return $x86 -or $x64;
}

理想情况下,我想将参数设置在顶部,这样我就可以将模板用于其他可执行文件。比如

$app_name
$app_version
$app_url
$app_filename
$app_executable
$app_arguments

任何帮助将不胜感激。

【问题讨论】:

  • 有什么帮助吗?为您设计交钥匙解决方案? (这不是本网站的目的。)

标签: powershell


【解决方案1】:

我做了更多的挖掘,发现了这个帖子:How to check if a program is installed and install it if it is not?

允许匹配名称和版本:

$tempdir = Get-Location
$tempdir = $tempdir.tostring()
$appName = '*AirParrot*'
$appVersion = "2.6.8"
$msiFile = $tempdir+"\microsoft.interopformsredist.msi"
$msiArgs = "-qb"

function Get-InstalledApps
{
    if ([IntPtr]::Size -eq 4) {
        $regpath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
    }
    else {
        $regpath = @(
            'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
            'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
        )
    }
    Get-ItemProperty $regpath | .{process{if($_.DisplayName -and $_.UninstallString) { $_ } }} | Select DisplayName, Publisher, InstallDate, DisplayVersion, UninstallString |Sort DisplayName

}

$result = Get-InstalledApps | where {$_.DisplayName -like $appName -and $_.DisplayVersion -ge $appVersion}

If ($result -eq $null) {
    (Start-Process -FilePath $msiFile -ArgumentList $msiArgs -Wait -Passthru).ExitCode
}

【讨论】:

  • 我才发现我的不比较版本。
猜你喜欢
  • 2018-01-27
  • 1970-01-01
  • 2021-10-12
  • 2018-06-17
  • 2021-10-13
  • 1970-01-01
  • 2021-10-29
  • 2020-07-19
  • 1970-01-01
相关资源
最近更新 更多