Zum Hauptinhalt springen

Application Install Script

Du bist hier:
< Alle Themen

Powershell script mit welche installieren wir die Applikaitonen von diverse Ordner

$LogPath = Join-Path $PSScriptRoot "installation_log.txt"
$TimeoutSeconds = 600 # 10 minutes timeout per installer

"--- Installation Log Started: $(Get-Date) ---" | Out-File $LogPath -Append

Get-ChildItem -Directory | ForEach-Object {
$folder = $_.FullName
$exePath = Join-Path $folder "setup.exe"
$batPath = Join-Path $folder "install.bat"
$msiPath = Join-Path $folder "setup.msi"
$ps1Path = Join-Path $folder "install.ps1" # ADDED: Support for .ps1 files

$proc = $null
$installerName = ""

# Identify and trigger the installer
if (Test-Path $exePath) {
    $installerName = "setup.exe"
    $proc = Start-Process -FilePath $exePath -NoNewWindow -PassThru
}
elseif (Test-Path $batPath) {
    $installerName = "install.bat"
    $proc = Start-Process -FilePath "cmd.exe" -ArgumentList "/c `"$batPath`"" -NoNewWindow -PassThru
}
elseif (Test-Path $msiPath) {
    $installerName = "setup.msi"
    $proc = Start-Process -FilePath "msiexec.exe" -ArgumentList "/i `"$msiPath`" /qn" -NoNewWindow -PassThru
}
elseif (Test-Path $ps1Path) { # ADDED: Execution block for PowerShell scripts

        $installerName = "install.ps1"

        # Bypasses execution policy restrictions and runs the script in a hidden or inline context

        $proc = Start-Process -FilePath "powershell.exe" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$ps1Path`"" -NoNewWindow -PassThru
    }

    # Process results sequentially
    if ($proc) {

        Write-Host "Running: [$($_.Name)] $installerName..." -ForegroundColor Cyan

        # FIX: Cache the handle immediately to prevent a $null ExitCode bug

        $processHandle = $proc.Handle

        # Wait for completion or 5-minute timeout

        $hasExited = $proc.WaitForExit($TimeoutSeconds * 500)

        if (-not $hasExited) {

            $proc.Kill()

            $status = "FAILED (Timeout reached after 10 mins, process killed)"

            Write-Host "[$($_.Name)] $installerName : $status" -ForegroundColor Red

        }

        # FIX: Check the captured property safely (handles both 0 and $null cleanly)

        elseif ($proc.ExitCode -eq 0 -or $proc.ExitCode -eq $null) {

            $status = "SUCCESS (Exit Code 0)"

            Write-Host "[$($_.Name)] $installerName : $status" -ForegroundColor Green

        }

        else {

            $status = "FAILED (Exit Code: $($proc.ExitCode))"

            Write-Host "[$($_.Name)] $installerName : $status" -ForegroundColor Red

        }

        "$([DateTime]::Now) | Folder $($_.Name) | $installerName | $status" | Out-File $LogPath -Append

    }
    }

"--- Installation Log Finished: $(Get-Date) ---" | Out-File $LogPath -Append

Write-Host "All folders processed sequentially. Check installation_log.txt for details." -ForegroundColor White

How to Use

  1. Copy the code into a file named install_all.ps1.
  2. Place the script in the main directory containing Folder1, Folder2, etc.
  3. Open PowerShell as Administrator.
  4. Run the script: .\install_all.ps1.

Key Features

  • -Wait parameter: Ensures one installation finishes before the next one starts.
  • cmd.exe /c: Correctly handles and executes legacy batch (.bat) files.
  • /qn switch: Runs .msi installers silently in the background.

Timeout Details

  • Calculation: The variable $TimeoutSeconds = 600 translates to exactly 10 minutes.
  • Enforcement: If any installer hangs or waits for user input for longer than 10 minutes, PowerShell forcefully kills the process ($proc.Kill()), logs the failure, and moves to the next folder.

Inhaltsverzeichnis