# Carbon

### Functions/Install-Msi.ps1

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 # 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-CMsi {     <#     .SYNOPSIS     Installs software from an MSI file.       .DESCRIPTION     Install-CMsi 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-CMsi only runs the MSI if the software isn't installed. Use the -Force switch to always run the installer.           .EXAMPLE     Install-CMsi -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-CMsi       Demonstrates how to pipe MSI files into Install-CMsi 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-CMsi''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-CMsi -Path$Path |         Where-Object {             if( $Force ) { return$true             }             $installInfo = Get-CProgramInstallInfo -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-CMsi'