Functions/Install-Msi.ps1
# Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. function Install-Msi { <# .SYNOPSIS Installs software from an MSI file. .DESCRIPTION `Install-Msi` installs software from an MSI file. If the install fails, it writes an error. Installation is always done in quiet mode, i.e. you won't see any UI. In Carbon 1.9 and earlier, this function was called `Invoke-WindowsInstaller`. Beginning with Carbon 2.0, `Install-Msi` only runs the MSI if the software isn't installed. Use the `-Force` switch to always run the installer. .EXAMPLE Install-Msi -Path Path\to\installer.msi Runs installer.msi, and waits untils for the installer to finish. If the installer has a UI, it is shown to the user. .EXAMPLE Get-ChildItem *.msi | Install-Msi Demonstrates how to pipe MSI files into `Install-Msi` for installation. #> [CmdletBinding(SupportsShouldProcess=$true)] param( [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] [Alias('FullName')] [string[]] # The path to the installer to run. Wildcards supported. $Path, [Parameter(DontShow=$true)] [Switch] # OBSOLETE. Installers are run in quiet mode by default. This switch will be removed in a future major version of Carbon. $Quiet, [Switch] # Install the MSI even if it has already been installed. Will cause a repair/reinstall to run. $Force ) Set-StrictMode -Version 'Latest' Use-CallerPreference -Cmdlet $PSCmdlet -Session $ExecutionContext.SessionState if( $PSBoundParameters.ContainsKey( 'Quiet' ) ) { Write-Warning ('Install-Msi''s `Quiet` switch is obsolete and will be removed in a future major version of Carbon. Installers are run in quiet mode by default. Please remove usages of the `Quiet` switch.') } Get-Msi -Path $Path | Where-Object { if( $Force ) { return $true } $installInfo = Get-ProgramInstallInfo -Name $_.ProductName -ErrorAction Ignore if( -not $installInfo ) { return $true } $result = ($installInfo.ProductCode -ne $_.ProductCode) if( -not $result ) { Write-Verbose -Message ('[MSI] [{0}] Installed {1}.' -f $installInfo.DisplayName,$installInfo.InstallDate) } return $result } | ForEach-Object { $msi = $_ if( $PSCmdlet.ShouldProcess( $msi.Path, "install" ) ) { Write-Verbose -Message ('[MSI] [{0}] Installing from {1}.' -f $msi.ProductName,$msi.Path) $msiProcess = Start-Process -FilePath "msiexec.exe" -ArgumentList "/quiet","/i",('"{0}"' -f $msi.Path) -NoNewWindow -Wait -PassThru if( $msiProcess.ExitCode -ne $null -and $msiProcess.ExitCode -ne 0 ) { Write-Error ("{0} {1} installation failed. (Exit code: {2}; MSI: {3})" -f $msi.ProductName,$msi.ProductVersion,$msiProcess.ExitCode,$msi.Path) } } } } Set-Alias -Name 'Invoke-WindowsInstaller' -Value 'Install-Msi' |