powershellif-statementwindows-installersilent-installer

msiexec Powershell silent install


I am searching for a Powershell Script which allows me to silent install a msi file. We have over 25000 PCs so i have to do that with a script.

Unfortunately at the moment a window popping up (Windows Installer) after the execution which shows the parameter of a msi file. Nothing more, no other "error messages" are popping up.

The first thing the Script should do is to check if the PC is a Desktop or Mobile Device. If its a Desktop device he should write in a file "Desktop Configuration was used". In the same time the msi installer should start with some parameter. If its a Laptop the procedure should be nearly the same.

After the installation is successful the user should be signed out.

I need this script to implement 2FA in our company.

The code at the moment looks like this:

IF ( ((Get-ComputerInfo | select -expand CsPCSystemType) -LIKE "Desktop") )
    {
        Write-Output "Desktop Configuration was used." >> \\XXX\XXX\XXX\XXX\Log\$env:Computername.txt 
        
        
        msiexec.exe /i "%~dp0setup.msi" /passive /norestart /L*v "%~dp0setup.log"

    }    

ELSE {
        Write-Output "Laptop Configuration was used." >> \\XXX.XXX.XX\X\XX\XXX\XXXX\$env:Computername.txt 
        msiexec.exe /i "%~dp0setup.msi" /passive /norestart  /L*v "%~dp0setup.log"

    }   

Write-Output "Lock Configuration was used." >> \\XXX\XXX\XXX\XXX\Log\$env:Computername.txt
rundll32.exe user32.dll,LockWorkStation

Any help is really appreciated.


Solution

  • The token %~dp0 (which resolves to the directory where the current script resides in) only works in batch scripts.

    In PowerShell, replace $~dp0 by $PSScriptRoot. That should solve the problem of msiexec showing up with the available command-line options.

    Another problem of your script is that msiexec runs asynchronously, when called directly as a command. So your script will be finished while the installation is still running, which is propably not what you want. This is caused by msiexec.exe being a GUI application. To run GUI applications synchronously, use Start-Process -Wait.

    
    $Arguments = "/i", "`"$PSScriptRoot\setup.msi`"", "/passive", "/norestart", "/L*v", "`"$PSScriptRoot\setup.log`""
    $msiProcess = Start-Process msiexec.exe -Wait -ArgumentList $Arguments -PassThru
    
    # Check if installation was successful 
    # 0 = ERROR_SUCCESS, 3010 = ERROR_SUCCESS_REBOOT_REQUIRED
    if( $msiProcess.ExitCode -in 0, 3010 ) {
        Write-Host "Installation succeeded with exit code $($msiProcess.ExitCode)"
    }