Search code examples
powershellcredentialswindows-installerrunasstart-process

Launch msiexec as an admin through powershell


I'm having trouble with a piece of code that I'm trying to get working in regards to executing an MSI application. I need to pass credentials that are stored in a variable and then pass those credentials through a "runas" to the MSI package so that it is installed with the escalated credentials I pass to the application.

Here is the section of code I am having trouble with.

if($filter -like "*.msi")
{
    Start-Process -FilePath "msiexec $FullFilePath" -Credential $adminCreds -ArgumentList "-noprofile -command &{Start-Process /i $FullFilePath /passive /norestart -verb runas}" -Wait -WorkingDirectory $path
    exit
}

My variables are as follows:

$filter = Get-ChildItem $path -Filter $($installer[$i]) -name
$FullFilePath = $path + "\" + $filter
$path = Split-Path $script:MyInvocation.MyCommand.Path

Thanks in advance!


Solution

  • To get around my issue I created a code block that exports a temporary batch script to look for any msi application within the folder that the script is exectuted from. Once the script is ran, and application is installed the script deletes itself. I pass the credentials through the batch script instead of the MSI application directly, but this works properly for my needs. Here are the changes to my script.

    $msiBatch = @"
    @echo off
    pushd "%~dp0"
    
    ::Get the MSI file name to install
    for /f "tokens=1* delims=\" %%A in ( 'forfiles /s /m *.msi /c "cmd /c echo @relpath"' ) do for %%F in (^"%%B) do (set myapp=%%~F)
    
    ::Launch our installer
    start /w "" msiexec /i "%~dp0%myapp%" /passive /norestart
    
    ::Self Delete
    DEL "%~f0"
    "@
    

    Here is how it is ran:

    if($filter -like "*.msi")
    {
        $installPath = $path + "\msiInstaller.bat"
        $msiBatch | Out-File -Encoding Ascii -append $installPath
        $FullFilePath = $installPath
    
    }
    
    Start-Process -FilePath powershell.exe -Credential $adminCreds -NoNewWindow -ArgumentList "-noprofile -command &{Start-Process $FullFilePath -verb runas}" -Wait -WorkingDirectory $path        
    break