2,187
edits
(Initial creation) |
(Updated to v2,0) |
||
Line 1: | Line 1: | ||
<source lang="powershell"> | <source lang="powershell"> | ||
<# | <# | ||
============================================================================================= | ============================================================================================= | ||
Line 22: | Line 21: | ||
-- Displays configs in yellow if they're not shared | -- Displays configs in yellow if they're not shared | ||
Version | Version 2.0 - Apr 2011 | ||
- Changed from single run to modular, continuous operation | |||
- Added RDP port test | - Added RDP port test | ||
============================================================================================= | ============================================================================================= | ||
#> | #> | ||
$UserFile = "User.fil" | $UserFile = "User.fil" | ||
$PassFile = "Pass.fil" # Encrypted file to store password in | $PassFile = "Pass.fil" # Encrypted file to store password in | ||
$LabMgrSvr = " | $LabMgrSvr = "ukb-sr-lbmgr-10" | ||
$ScriptName = ($MyInvocation.MyCommand.Name).Replace(".ps1", "") | |||
$host.UI.RawUI.WindowTitle = $ScriptName | |||
# ============================================================================================= | # ============================================================================================= | ||
Line 42: | Line 41: | ||
Write-Host "Press any key to exit..." | Write-Host "Press any key to exit..." | ||
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | ||
$labManager.Dispose() | |||
Exit | Exit | ||
} | } | ||
Line 163: | Line 163: | ||
if (!$failed) { | if (!$failed) { | ||
Write-Host "Connection successful" -BackgroundColor DarkGreen | Write-Host "Connection successful" -BackgroundColor DarkGreen | ||
} | |||
} | |||
# Bussiness functions ========================================================================== | |||
function Select-Configuration { | |||
# Get list of configurations, query user which to do, and return selected config ID | |||
$ConfIDs = @() | |||
$confID = -99 | |||
# Get list of config's and display deployed only | |||
Write-Host ("Deployed configurations...`n") | |||
$Configs = $labManager.ListConfigurations(1) | |||
$Configs = $Configs | Sort name | |||
foreach ($config in $Configs) { | |||
if ($config.isDeployed) { | |||
if ($FontColour -eq "Gray") { | |||
$FontColour = "White" | |||
} else { | |||
$FontColour = "Gray" | |||
} | |||
if ($config.isPublic) { | |||
Write-host -ForegroundColor $FontColour ("ID " + $config.id + " - " + $config.name + " [" + $config.bucketName + "]") | |||
} else { | |||
Write-Host -ForegroundColor Yellow ("ID " + $config.id + " - " + $config.name + " [" + $config.bucketName + "] - Not Shared !!") | |||
} | |||
$ConfIDs += $config.ID # List of config ID's (with which to validate user input) | |||
} | |||
} | |||
# Get config ID from user | |||
While (!($ConfIDs -contains $ConfID)) { # Don't leave loop until user selected config ID is valid! | |||
$ConfID = Read-Host "`nSelect a configuration ID [0 to quit]" | |||
if (!$ConfID -or ($ConfID -eq 0)) { | |||
# Dump the Lab Manager connection | |||
$labManager.Dispose() | |||
exit | |||
} elseif (!($ConfIDs -contains $ConfID)) { | |||
Write-Host "Invalid, should be one of" ([string]::join(", ", $ConfIDs)) | |||
} | |||
} | |||
Return $labManager.GetConfiguration($ConfID) | |||
} | |||
function Get-ConfigVMs { | |||
Param($objConfig) | |||
$VMs = @() | |||
# Get VM's for selected config | |||
$labManager.ListMachines($objConfig.id) | foreach { | |||
$vm = "" | Select VM_Name, VM_IntIP, VM_ExtIP, VM_State | |||
$vm.VM_Name = $_.name | |||
$vm.VM_IntIP = $_.internalIP | |||
$vm.VM_ExtIP = $_.externalIP | |||
$vm.VM_State = $_.status # 1=Off, 2=On, 3=Suspended, 4=Stuck, 128=Invalid | |||
$VMs = $VMs + $vm | |||
} | |||
$VMs = $VMs | Sort VM_Name | |||
#$VMs | Select VM_Name, VM_IntIP, VM_ExtIP | Format-Table | |||
Return $VMs | |||
} | |||
function Send-ConfigIPs { | |||
Param($objConfig, $objConfVMs) | |||
# Email results to user | |||
$EmailTo = $env:username + "@michaelpage.com" | |||
$tmp = Read-Host "Email to [$EmailTo]" | |||
if ($tmp) {$EmailTo = $tmp} | |||
Write-Host "Emailing $EmailTo..." -nonewline | |||
# Create HTML for email | |||
[string]$msgHTML = "<!-- Genenerated by $ScriptName at " + (Get-Date).ToString() + " hrs -->`n" | |||
$msgHTML += "<html xmlns='http://www.w3.org/TR/REC-html40'>`n`n<head>`n<meta http-equiv=Content-Type content='text/html; charset=uk'`n>" | |||
$msgHTML += "<meta name=Generator content='Lab Manager NATs script'>`n<meta name=Author content='Simon Strutt'>`n</head>`n`n" | |||
$msgHTML += "<body style='font-family: arial; font-size: 10;'><table style='font-family: arial; font-size: 11';><tr><td>`nConfiguration:<td><b>" + $objConfig.name + "</b><tr><td>`nWorkspace:<td>" + $objConfig.bucketName | |||
$msgHTML += "<tr><td>`nDescription:<td>" + $objConfig.description + "<tr><td>Info correct at:<td>" + (Get-Date).ToString() + " hrs</table>`n" | |||
$msgHTML += "<br><br>`n`n" | |||
$msgHTML += "<table border=1 style='border-width: 1px; border-spacing: 0; border-color: black; background-color: #faf0e6; font-family: arial; font-size: 11; font-weight: bold'>`n" | |||
$msgHTML += "<tr style='border-width: 2px; background-color: #fcf7f8;'><th>VM Name<th>Internal IP<th>External IP`n" | |||
foreach ($vm in $objConfVMs) { | |||
$msgHTML += "<tr><td>" + $vm.VM_Name + "<td style='font-weight: normal'>" + $vm.VM_IntIP + "<td>" + $vm.VM_ExtIP | |||
} | |||
$msgHTML += "</table>`n`n<br><br>`n<table style='font-family: arial; font-size: 10; color: #888888'>`n<tr><td>Generated by script: <td>$ScriptName" | |||
$msgHTML += "`n</table>`n`n</body></html>" | |||
#Send via email | |||
$smtp = New-Object Net.Mail.SmtpClient -arg "mailrelay.uk.michaelpage.local" | |||
$msg = New-Object Net.Mail.MailMessage | |||
$msg.From = "VI-Mgmt@michaelpage.local" | |||
$msg.To.Add($EmailTo) | |||
$msg.Subject = "Lab Manager configuration: " + $objConfig.name | |||
$msg.IsBodyHTML = $true | |||
$msg.Body = $msgHTML | |||
try { | |||
$smtp.Send($msg) | |||
} catch { | |||
Write-Host "`n" $_ | |||
Write-Host "`nCopy text above if you want to retain the info." | |||
PressAnyKeyToExit | |||
} | |||
Write-Host "sent !`n" | |||
} | |||
function Test-Access { | |||
Param($objConfVMs) | |||
# Check RDP access to powered-on VM's | |||
Write-Host -ForegroundColor white "`nTesting RDP (TCP port) access to powered on servers..." | |||
foreach ($vm in $objConfVMs) { | |||
if ($vm.VM_State -eq 2) { | |||
Write-Host ($vm.VM_Name).PadRight(16) ($vm.VM_ExtIP).PadRight(15) -nonewline | |||
Test-Port $vm.VM_ExtIP | |||
} | |||
} | } | ||
} | } | ||
Line 191: | Line 312: | ||
# Get list of configurations, query user which to do, then get list of VM's and IP's | # Get list of configurations, query user which to do, then get list of VM's and IP's | ||
$ | $option = 1 | ||
$Config = Select-Configuration | |||
$host.UI.RawUI.WindowTitle = $ScriptName + " - " + $Config.name | |||
$ | |||
While (1) { | |||
While ( | Write-Host -ForegroundColor white ("`n" + $Config.name + " (" + $Config.description + ")`n") | ||
$ | Write-Host "1 - Email IP addresses" | ||
if ($ | Write-Host "2 - Check RDP access" | ||
Write-Host "3 - Change config" | |||
Write-Host "0 - Exit" | |||
$tmp = Read-Host "`nSelect an option [$option]" | |||
if ($tmp) {$option = $tmp} | |||
if ($option -notmatch "0|1|2|3") { | |||
Write-Host "$option is invalid" | |||
$option = 0 | |||
} elseif ($option -eq 0) { | |||
# Dump the Lab Manager connection | # Dump the Lab Manager connection | ||
$labManager.Dispose() | $labManager.Dispose() | ||
exit | exit | ||
} elseif | } elseif ($option -eq 1) { | ||
$ConfVMs = Get-ConfigVMs $Config | |||
Send-ConfigIPs $Config $ConfVMs | |||
$ | |||
$ | } elseif ($option -eq 2) { | ||
$ConfVMs = Get-ConfigVMs $Config | |||
$ | Test-Access $ConfVMs | ||
$ | |||
} elseif ($option -eq 3) { | |||
$Config = Select-Configuration | |||
$host.UI.RawUI.WindowTitle = $ScriptName + " - " + $Config.name | |||
} else { | |||
Write-Host "Unexpected error...!" | |||
$ | |||
} | } | ||
} | } | ||
PressAnyKeyToExit | PressAnyKeyToExit | ||
</source> | </source> |