微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用于在远程机器上安装多个 msi 的 Powershell 脚本

如何解决用于在远程机器上安装多个 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是否安装正确?

感谢您抽出宝贵时间。

解决方法

您可以在安装部分添加:

$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"}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。