Skip to content

Instantly share code, notes, and snippets.

@mohitt
Created October 15, 2025 01:28
Show Gist options
  • Select an option

  • Save mohitt/460a130e778b9284e6f20e0d08a0a2e2 to your computer and use it in GitHub Desktop.

Select an option

Save mohitt/460a130e778b9284e6f20e0d08a0a2e2 to your computer and use it in GitHub Desktop.
express start old sites.
function Start-Legacy-Teton {
param(
[string]$ConfigPath = "D:\uw\teton-work-dir\mohit-teton-tcc-mapi\.vs\temp-cs-vb\config\applicationhost.config",
[string]$LogDir = "D:\uw\teton-work-dir\mohit-teton-tcc-mapi\iislogs"
)
$iisPath = "$env:ProgramFiles\IIS Express\iisexpress.exe"
# Validation
if (-not (Test-Path $ConfigPath)) {
Write-Error "Config file not found: $ConfigPath"
return
}
if (-not (Test-Path $iisPath)) {
Write-Error "IIS Express not found: $iisPath"
return
}
if (-not (Test-Path $LogDir)) {
New-Item -Path $LogDir -ItemType Directory | Out-Null
}
# Kill any existing IIS Express processes first
Write-Host "Checking for existing IIS Express processes..."
$existingProcesses = Get-Process iisexpress -ErrorAction SilentlyContinue
if ($existingProcesses) {
Write-Warning "Found existing IIS Express processes. Stopping them..."
$existingProcesses | Stop-Process -Force
Start-Sleep -Seconds 2
}
# Check ports in config
[xml]$xml = Get-Content $ConfigPath
$sites = $xml.configuration.'system.applicationHost'.sites.site
if (-not $sites) {
Write-Warning "No sites found in config."
return
}
# Display port information
Write-Host "`nSites to start:"
foreach ($site in $sites) {
$bindings = $site.bindings.binding
Write-Host " - $($site.name):"
foreach ($binding in $bindings) {
$port = ($binding.bindingInformation -split ':')[1]
Write-Host " $($binding.protocol): port $port"
# Check if port is already in use
$portInUse = Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue
if ($portInUse) {
Write-Warning " WARNING: Port $port is already in use by process ID: $($portInUse.OwningProcess)"
$process = Get-Process -Id $portInUse.OwningProcess -ErrorAction SilentlyContinue
if ($process) {
Write-Warning " Process: $($process.ProcessName) ($($process.Id))"
}
}
}
}
$global:LegacyTetonJobs = @()
foreach ($site in $sites) {
$name = $site.name
$logFile = Join-Path $LogDir "$name.log"
$args = @("/site:$name", "/config:$ConfigPath", "/trace:error")
Write-Host "`nStarting site '$name' as background job..."
Write-Host " Log: $logFile"
Write-Host " Command: $iisPath $($args -join ' ')"
$job = Start-Job -ScriptBlock {
param($exe, $argList, $log)
# Clear or create log file
"" | Out-File $log -Force
# Start IIS Express and redirect all output
& $exe $argList *>&1 | Out-File $log -Append
} -ArgumentList $iisPath, $args, $logFile
$global:LegacyTetonJobs += @{
Name = $name
Job = $job
Log = $logFile
}
# Give it a moment to start
Start-Sleep -Milliseconds 500
}
Write-Host "`nAll sites started as background jobs."
Write-Host "Use 'Get-LegacyTetonLogs' to tail logs."
Write-Host "Use 'Get-LegacyTetonStatus' to check status."
Write-Host "Use 'Stop-Legacy-Teton' to stop all sites."
}
function Stop-Legacy-Teton {
Write-Host "Stopping all IIS Express background jobs and processes..."
if ($global:LegacyTetonJobs) {
foreach ($entry in $global:LegacyTetonJobs) {
Write-Host "Stopping job for site: $($entry.Name)"
try {
Stop-Job -Job $entry.Job -Force -ErrorAction SilentlyContinue
Remove-Job -Job $entry.Job -Force -ErrorAction SilentlyContinue
} catch {
Write-Warning "Failed to stop job for $($entry.Name): $_"
}
}
$global:LegacyTetonJobs = @()
}
# Kill all IIS Express processes
$processes = Get-Process iisexpress -ErrorAction SilentlyContinue
if ($processes) {
Write-Host "Killing $($processes.Count) IIS Express process(es)..."
$processes | Stop-Process -Force
}
Write-Host "All sites stopped."
}
function Get-LegacyTetonStatus {
if (-not $global:LegacyTetonJobs) {
Write-Warning "No Legacy Teton jobs found. Run 'Start-Legacy-Teton' first."
return
}
Write-Host "`nLegacy Teton Status:"
Write-Host "=" * 60
foreach ($entry in $global:LegacyTetonJobs) {
$job = $entry.Job
$name = $entry.Name
Write-Host "`nSite: $name"
Write-Host " Job State: $($job.State)"
Write-Host " Job ID: $($job.Id)"
Write-Host " Log File: $($entry.Log)"
# Check if log file has errors
if (Test-Path $entry.Log) {
$logContent = Get-Content $entry.Log -Tail 5
$errors = $logContent | Select-String -Pattern "error|exception|failed" -SimpleMatch
if ($errors) {
Write-Warning " Recent errors found in log:"
$errors | ForEach-Object { Write-Host " $_" -ForegroundColor Red }
} else {
Write-Host " Status: OK (no errors in recent log entries)" -ForegroundColor Green
}
}
}
# Check IIS Express processes
$iisProcesses = Get-Process iisexpress -ErrorAction SilentlyContinue
Write-Host "`nIIS Express Processes: $($iisProcesses.Count)"
if ($iisProcesses) {
$iisProcesses | ForEach-Object {
Write-Host " PID $($_.Id) - $($_.ProcessName)"
}
}
}
function Get-LegacyTetonLogs {
param(
[switch]$All,
[int]$Tail = 10
)
if (-not $global:LegacyTetonJobs) {
Write-Warning "No Legacy Teton jobs found."
return
}
if ($All) {
# Show recent logs from all sites
Write-Host "Recent logs from all sites:"
Write-Host "=" * 60
foreach ($entry in $global:LegacyTetonJobs) {
if (Test-Path $entry.Log) {
Write-Host "`n[$($entry.Name)]" -ForegroundColor Cyan
Get-Content $entry.Log -Tail $Tail | ForEach-Object {
Write-Host " $_"
}
}
}
} else {
# Tail all logs in real-time with site prefix
$logStreams = @()
foreach ($entry in $global:LegacyTetonJobs) {
$site = $entry.Name
$logFile = $entry.Log
if (-not (Test-Path $logFile)) {
Write-Warning "Log file not found for $site"
continue
}
$job = Start-Job -ScriptBlock {
param($file, $site)
Get-Content $file -Wait -Tail 0 | ForEach-Object {
"[$site] $_"
}
} -ArgumentList $logFile, $site
$logStreams += $job
}
if ($logStreams.Count -eq 0) {
Write-Warning "No log files found to tail."
return
}
Write-Host "Tailing logs for all sites (Press Ctrl+C to stop)..."
Write-Host "=" * 60
try {
while ($true) {
foreach ($job in $logStreams) {
$output = Receive-Job -Job $job
if ($output) {
$output | ForEach-Object { Write-Host $_ }
}
}
Start-Sleep -Milliseconds 100
}
} finally {
# Cleanup log streaming jobs
$logStreams | Stop-Job -ErrorAction SilentlyContinue
$logStreams | Remove-Job -ErrorAction SilentlyContinue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment