You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求编写多窗口PowerShell脚本以批量执行软件安装

PowerShell Multi-Window Interactive Installation Script with Menu

Alright, here's a robust solution that lets you manage multiple remote installation sessions through a custom interactive menu, launching separate PowerShell windows for each server so you can handle interactive installation menus without manually logging into each machine.

Complete Script

# Multi-Window Remote Installation Menu Script
Clear-Host

$installScriptPath = $null
$targetServers = @()

do {
    # Display main menu
    Write-Host "`n=== Remote Multi-Window Installation Manager ===" -ForegroundColor Cyan
    Write-Host "1. Set path to your installation script"
    Write-Host "2. Enter target server list (comma-separated or one per line)"
    Write-Host "3. Launch multi-window installation sessions"
    Write-Host "4. Exit`n"
    
    $choice = Read-Host "Enter your choice (1-4)"

    switch ($choice) {
        1 {
            $inputPath = Read-Host "Enter full path to your installation script (e.g., C:\Scripts\Install-Software.ps1 or \\Share\Scripts\Install-Software.ps1)"
            if (Test-Path $inputPath) {
                $installScriptPath = $inputPath
                Write-Host "✅ Installation script path set to: $installScriptPath" -ForegroundColor Green
            }
            else {
                Write-Host "❌ Error: Script path does not exist. Please try again." -ForegroundColor Red
            }
        }
        2 {
            Write-Host "`nEnter target servers (comma-separated, e.g., Server01,Server02, or enter one per line and finish with Ctrl+D):"
            $inputServers = @(Read-Host -Prompt "Input servers").Split(',', [System.StringSplitOptions]::RemoveEmptyEntries) | ForEach-Object { $_.Trim() }
            
            # Optional: Validate server connectivity (basic ping check)
            foreach ($server in $inputServers) {
                if (Test-Connection -ComputerName $server -Count 1 -Quiet -ErrorAction SilentlyContinue) {
                    $targetServers += $server
                    Write-Host "✅ Added server: $server (reachable)" -ForegroundColor Green
                }
                else {
                    Write-Host "⚠️ Server $server is unreachable - skipping" -ForegroundColor Yellow
                }
            }
        }
        3 {
            if (-not $installScriptPath) {
                Write-Host "❌ Error: Please set the installation script path first (Option 1)." -ForegroundColor Red
                continue
            }
            if ($targetServers.Count -eq 0) {
                Write-Host "❌ Error: No valid target servers added. Please use Option 2 first." -ForegroundColor Red
                continue
            }

            Write-Host "`nLaunching installation windows for $($targetServers.Count) servers..." -ForegroundColor Cyan
            foreach ($server in $targetServers) {
                # Launch a new PowerShell window that connects to the server and runs the installation script
                $command = @"
Enter-PSSession -ComputerName '$server';
& '$installScriptPath';
# Keep window open after script finishes (optional)
Read-Host -Prompt 'Installation complete. Press Enter to close this window'
"@
                Start-Process powershell.exe -ArgumentList "-NoExit", "-Command", $command
                Start-Sleep -Milliseconds 500 # Add small delay to avoid overwhelming system
            }
            Write-Host "✅ All installation windows launched!" -ForegroundColor Green
        }
        4 {
            Write-Host "Exiting script. Have a great day!" -ForegroundColor Cyan
            exit
        }
        default {
            Write-Host "❌ Invalid choice. Please enter a number between 1 and 4." -ForegroundColor Red
        }
    }
} while ($true)

Key Features & Explanations

  • Interactive Menu System: The do-while loop keeps the menu active until you choose to exit, making it easy to configure settings step-by-step.
  • Validation Checks: The script verifies your installation script exists and checks if target servers are reachable (via ping) before adding them to the list.
  • Multi-Window Execution: Uses Start-Process to spin up independent PowerShell windows for each server. Each window:
    1. Establishes an interactive remote session with Enter-PSSession
    2. Runs your specified installation script (preserving any custom menus/interactive prompts in the script)
    3. Keeps the window open after completion so you can review output (you can remove the final Read-Host line if you want windows to close automatically)
  • Flexible Server Input: You can enter servers as a comma-separated list or line-by-line, whichever is easier for you.

Important Notes

  • Ensure your user account has administrative permissions on all target servers.
  • Target servers must have PowerShell Remoting enabled. If not, run Enable-PSRemoting -Force on each server first (you can do this via a separate script if needed).
  • The installation script path should be accessible to the target servers—use a network share (e.g., \\FileServer\Scripts\Install.ps1) or ensure the script is copied locally to each server beforehand.

内容的提问来源于stack exchange,提问作者Maverick94

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:25:48