首页 > 解决方案 > 用于在远程机器上安装多个 msi 的 Powershell 脚本

问题描述

美好的一天,我会请你帮助我找到解决方案,如何使用 nas 存储上的链接将每个 MSI 包复制到远程机器。

# Get list of servers
param(
  [ValidateSet('STUDENT_LAB', 'LIBRARY_LAB', 'TEACHER_LAB')]
  [Parameter(Mandatory = $true, 
    HelpMessage = 'Select one of the valid servers by typing one of these names: STUDENT_LAB, LIBRARY_LAB, TEACHER_LAB')]
  [string]$ServerGroup
)
$servers = @{
  STUDENT_LAB      = ('192.168.1.1','192.168.1.2','192.168.1.3')
  LIBRARY_LAB      = ('192.168.10.1','192.168.10.2','192.168.10.3')
  TEACHER_LAB = ('192.168.15.1','192.168.15.2','192.168.15.3')
}[$ServerGroup]
Write-Output "The user chose $ServerGroup"

#this is what I don't know how to implement - download file from nas storage on remote machine

$sourcefiles = '\\NASSTORAGE\MSI\MICROSOFT\Microsoft-ODBCDriver-11-SQLServer-x64\msodbcsql.msi' ; '\\NASSTORAGE\MSI\MICROSOFT\Microsoft-ODBCDriver-17-SQLServr-x64\msodbcsql_17.2.0.1_x64.msi'; '\\NASSTORAGE\MSI\MICROSOFT\Microsoft-OLEDBDriver-SQL Server-x64\msoledbsql_18.1.0.0_x64.msi'

foreach($server in $servers) {
    
  # Destination UNC path changes based on server name 

  $destinationPath = "\\$server\D$\tmp\"

  # Check that full folder structure exists and create if it doesn't

  if(!(Test-Path $destinationPath)) {
    New-Item -ItemType Directory -Force -Path $destinationPath
  }
  # Copy the file across
  Copy-Item $sourcefiles $destinationPath
#list of packages to install  
  $msiList = @(
            'Microsoft-ODBCDriver-11-SQLServer-x64\msodbcsql.msi'
            'Microsoft-ODBCDriver-17-SQLServr-x64\msodbcsql_17.2.0.1_x64.msi'
            'Microsoft-OLEDBDriver-SQL Server-x64\msoledbsql_18.1.0.0_x64.msi'

        )
#now I'm trying to install on remote machine
        foreach ($msi in $msiList) {
            $install = Join-Path -Path $destinationPath -ChildPath $msi
            Start-Process "msiexec.exe" -ArgumentList "/I $install",'/qn' -Wait
            
        }
}

有什么方法可以检查 msi 是否安装正确?

感谢您的时间。

标签: powershellwindows-installerremote-input

解决方案


您可以在安装部分添加它:

$LaunchMsi = Start-Process "msiexec.exe" -ArgumentList "/I $install",'/qn' -Wait -PassThru
$ReturnCode = $LaunchMsi.ExitCode

if (($ReturnCode -eq "0") -OR ($ReturnCode -eq "3010")) {Write-Host "installation OK, return code : $ReturnCode"}
Else {Write-host "installation KO, return code : $ReturnCode"}

推荐阅读