How do I run a Windows installer and get a succeed/fail value in PowerShell?

后端 未结 4 1639
-上瘾入骨i
-上瘾入骨i 2021-02-19 19:21

I would like to install a set of applications: .NET 4, IIS 7 PowerShell snap-ins, ASP.NET MVC 3, etc. How do I get the applications to install and return a value that determines

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-19 19:50

    I have implemented exactly what you are looking for at my current project. We need to automate deployment and instillation of n number of apps across multiple environments and datacenters. These scripts are slightly modified from the original version for simplicity sake since my complete code is reaching 1000 lines but the core functionality is intact. I hope this does what you are asking for.

    This PS function pulls all the apps from the registry (what add/remove programs reads from) and then search's for the supplied app name and display version. In my code (PSM1) I run this function before I install to whether or not it is their and then afterword’s to verify that it got installed…. All this can be wrapped in one master function to manager flow control.

    function Confirm-AppInstall{
    param($AppName,$AppVersion)
    $Apps = Get-ItemProperty Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*|?{$_.DisplayName -ne $Null}|?{$_.DisplayName -ne ""}
    
    $Apps += Get-ItemProperty Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*|?{$_.DisplayName -ne $Null}|?{$_.DisplayName -ne ""}
    
    $Installed = $Apps|?{$_.DisplayName -eq ""}|?{$_.DisplayVersion -eq ""}|select -First 1
    if($Installed -ne $null){return $true}else{return $false}
    }
    

    This PS function will load a txt file that has the install commands prepopulated (one command per line). and run each line indivaduly and wait for the install to complete before moving on to the next.

    function Install-Application{
    param($InstallList = "C:\Install_Apps_CMDS.txt")
    
    $list = gc -Path $InstallList
    foreach ($Command in $list){
        Write-Output ("[{0}]{1}"  -f (Get-Date -Format G),$call)
        #Make install process wait for exit before continuing.
        $p = [diagnostics.process]::Start("powershell.exe","-NoProfile -NoLogo -Command $Command")
        $p.WaitForExit()
        Start-Sleep -Seconds 2
        #Searches for the installer exe or msi that was directly opened by powershell and gets the process id.
        $ProcessID = (gwmi -Query ("select ProcessId from Win32_Process WHERE ParentProcessID = {0} AND Name = '{1}'" -f $p.Id,$ParentProcessFile)|select ProcessId).ProcessId
        #waits for the exe or msi to finish installing
        while ( (Get-Process -Id $ProcessID -ea 0) -ne $null){
            Start-Sleep -Seconds 2
            $ElapsedTime = [int](New-TimeSpan -Start $P.StartTime -End (Get-Date)|select TotalSeconds).TotalSeconds
            #install times out after 1000 seconds so it dosent just sit their forever this can be changed 
            if(2000 -lt $ElapsedTime){
                Write-Output ('[{0}] The application "{1}" timed out during instilation and was forcfully exited after {2} seconds.'  -f (Get-Date -Format G),$App.Name,(([int]$App.InstallTimeOut) * 60))
                break
            }
        }
        #clean up any old or hung install proccess that should not be running at this point.
        Stop-Process -Name $ParentProcessName -ea 0 -Force
        Stop-Process -Name msiexec -ea 0 -Force
        }
    }
    

    The TXT file should be formatted as such... you will need to do you research on how to each app needs to be installed. a good resource is appdeploy.com

    C:\Install.exe /q
    C:\install.msi /qn TRANSFORMS='C:\transform.mst'
    C:\install2.msi /qn /norestart
    C:\install3.exe /quiet
    

    Let me know if there are any errors I had to modify my existing code to remove the proprietary values and make this a little more simplistic. I am pulling my values from a custom XML answer sheet. But this code should work as I have supplied it.

    If you would like for me to discuss more about my implementation let me know and i can make a more detailed explanation and also add more of the supporting functions that I have implemented.

提交回复
热议问题