# Resource Monitor Service - Remote VM Deployment Script # Deploys the packaged service to a remote VM param( [Parameter(Mandatory=$true)] [string]$VMAddress, [Parameter(Mandatory=$false)] [string]$Username, [Parameter(Mandatory=$false)] [string]$PackagePath, [switch]$UseWinRM, [switch]$CopyOnly ) Write-Host "=== Resource Monitor Service - Remote VM Deployment ===" -ForegroundColor Cyan Write-Host # Find the latest package if not specified if (-not $PackagePath) { $latestPackage = Get-ChildItem -Path ".\release-packages\ResourceMonitorService-v*.zip" | Sort-Object LastWriteTime -Descending | Select-Object -First 1 if ($latestPackage) { $PackagePath = $latestPackage.FullName Write-Host "Using latest package: $($latestPackage.Name)" -ForegroundColor Green } else { Write-Host "✗ No packages found. Run .\package-release.ps1 first" -ForegroundColor Red exit 1 } } if (-not (Test-Path $PackagePath)) { Write-Host "✗ Package not found: $PackagePath" -ForegroundColor Red exit 1 } $packageName = [System.IO.Path]::GetFileNameWithoutExtension($PackagePath) Write-Host "Package: $packageName" -ForegroundColor White Write-Host "Target VM: $VMAddress" -ForegroundColor White Write-Host if ($UseWinRM) { Write-Host "Using WinRM for deployment..." -ForegroundColor Yellow # Test WinRM connection Write-Host "Testing WinRM connection to $VMAddress..." -ForegroundColor Yellow try { $session = New-PSSession -ComputerName $VMAddress -Credential (Get-Credential -Message "Enter credentials for $VMAddress") Write-Host "✓ WinRM connection successful" -ForegroundColor Green } catch { Write-Host "✗ WinRM connection failed: $($_.Exception.Message)" -ForegroundColor Red Write-Host "Make sure WinRM is enabled on the target VM:" -ForegroundColor Yellow Write-Host " winrm quickconfig" -ForegroundColor Gray Write-Host " Enable-PSRemoting -Force" -ForegroundColor Gray exit 1 } # Copy package to VM Write-Host "Copying package to VM..." -ForegroundColor Yellow try { $remoteTemp = "C:\Temp\$packageName" Invoke-Command -Session $session -ScriptBlock { param($remotePath) if (Test-Path $remotePath) { Remove-Item $remotePath -Recurse -Force } New-Item -ItemType Directory -Path $remotePath -Force | Out-Null } -ArgumentList $remoteTemp Copy-Item -Path $PackagePath -Destination "$remoteTemp.zip" -ToSession $session Write-Host "✓ Package copied to $remoteTemp.zip" -ForegroundColor Green } catch { Write-Host "✗ Failed to copy package: $($_.Exception.Message)" -ForegroundColor Red exit 1 } if (-not $CopyOnly) { # Extract and install on VM Write-Host "Extracting and installing on VM..." -ForegroundColor Yellow try { $installResult = Invoke-Command -Session $session -ScriptBlock { param($remotePath, $packageName) # Extract package Expand-Archive -Path "$remotePath.zip" -DestinationPath $remotePath -Force Set-Location $remotePath # Run installation $installOutput = & ".\install-service.ps1" 2>&1 return @{ Success = $LASTEXITCODE -eq 0 Output = $installOutput -join "`n" ExitCode = $LASTEXITCODE } } -ArgumentList $remoteTemp, $packageName if ($installResult.Success) { Write-Host "✓ Installation completed successfully!" -ForegroundColor Green Write-Host "Web Dashboard: http://$VMAddress:5000" -ForegroundColor Cyan } else { Write-Host "✗ Installation failed (Exit Code: $($installResult.ExitCode))" -ForegroundColor Red Write-Host "Output:" -ForegroundColor Yellow Write-Host $installResult.Output -ForegroundColor Gray } } catch { Write-Host "✗ Installation failed: $($_.Exception.Message)" -ForegroundColor Red } } # Clean up session Remove-PSSession $session } else { # Manual deployment instructions Write-Host "Manual Deployment Instructions:" -ForegroundColor Yellow Write-Host Write-Host "1. Copy the package to your VM:" -ForegroundColor White Write-Host " - Use RDP, shared folders, or network copy" -ForegroundColor Gray Write-Host " - Package location: $PackagePath" -ForegroundColor Gray Write-Host Write-Host "2. On the VM, extract the ZIP file to a temporary directory" -ForegroundColor White Write-Host Write-Host "3. Open PowerShell as Administrator and navigate to the extracted directory" -ForegroundColor White Write-Host Write-Host "4. Run the installation:" -ForegroundColor White Write-Host " .\install-service.ps1" -ForegroundColor Cyan Write-Host " OR" -ForegroundColor Gray Write-Host " .\INSTALL.bat" -ForegroundColor Cyan Write-Host Write-Host "5. Access the web dashboard:" -ForegroundColor White Write-Host " http://$VMAddress:5000" -ForegroundColor Cyan Write-Host Write-Host "Alternative deployment methods:" -ForegroundColor Yellow Write-Host " PowerShell Remoting: .\deploy-to-vm.ps1 -VMAddress $VMAddress -UseWinRM" -ForegroundColor Gray Write-Host " Copy only: .\deploy-to-vm.ps1 -VMAddress $VMAddress -UseWinRM -CopyOnly" -ForegroundColor Gray } Write-Host Write-Host "=== Deployment Script Complete ===" -ForegroundColor Cyan