Skip to content

Commit

Permalink
Introduce auto-start option of deployed VMs
Browse files Browse the repository at this point in the history
  • Loading branch information
machv committed Aug 21, 2020
1 parent 08e2770 commit ad6c61f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
46 changes: 42 additions & 4 deletions Scripts/3_Deploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ If (-not $isAdmin) {
}

# return info
@{
[PSCustomObject]@{
OSDiskPath = $vhdpath
VM = $VMTemp
}
Expand Down Expand Up @@ -1348,7 +1348,7 @@ If (-not $isAdmin) {

#process $labconfig.VMs and create VMs (skip if machine already exists)
WriteInfoHighlighted 'Processing $LabConfig.VMs, creating VMs'
$provisionedVMsCount = 0
$provisionedVMs = @()
foreach ($VMConfig in $LABConfig.VMs.GetEnumerator()){
if (!(Get-VM -Name "$($labconfig.prefix)$($VMConfig.vmname)" -ErrorAction SilentlyContinue)){
$vmProvisioningStartTime = Get-Date
Expand Down Expand Up @@ -1488,7 +1488,7 @@ If (-not $isAdmin) {
$vmDeploymentEvents += $vmInfo
}

$provisionedVMsCount += 1
$provisionedVMs += $createdVm.VM
}
}

Expand Down Expand Up @@ -1530,14 +1530,52 @@ If (-not $isAdmin) {
WriteInfo "`t Enabling VMNics device naming"
Get-VM -VMName "$($labconfig.Prefix)*" | Where-Object Generation -eq 2 | Set-VMNetworkAdapter -DeviceNaming On

#Autostart VMs
$startVMs = 0
if($LabConfig.AutoStartAfterDeploy -eq $true -or $LabConfig.AutoStartAfterDeploy -eq "All") {
$startVMs = 1
} elseif($LabConfig.AutoStartAfterDeploy -eq "DeployedOnly") {
$startVMs = 2
}

if(-not $LabConfig.ContainsKey("AutoStartAfterDeploy") -and $AllVMs.Count -gt 0) {
$options = [System.Management.Automation.Host.ChoiceDescription[]] @(
<# 0 #> New-Object System.Management.Automation.Host.ChoiceDescription "&No", "No VM will be started."
<# 1 #> New-Object System.Management.Automation.Host.ChoiceDescription "&All", "All VMs in the lab will be started."
)

if($provisionedVMs.Count -gt 0) {
<# 2 #> $options += New-Object System.Management.Automation.Host.ChoiceDescription "&Deployed only", "Only newly deployed VMs will be started."
}
$startVMs = $host.UI.PromptForChoice("Start VMs", "Would you like to start lab virtual machines?", $options, 0 <#default option#>)
}
#Starting VMs
$toStart = @()
switch($startVMs) {
1 {
$toStart = $AllVMs
}
2 {
$toStart = $provisionedVMs
}
}

if(($toStart | Measure-Object).Count -gt 0) {
WriteInfoHighlighted "Starting VMs"
$toStart | ForEach-Object {
WriteInfo "`t $($_.Name)"
Start-VM -VM $_ -WarningAction SilentlyContinue
}
}

# Telemetry Event
if((Get-TelemetryLevel) -in $TelemetryEnabledLevels) {
WriteInfo "`t Sending telemetry info"
$metrics = @{
'script.duration' = [Math]::Round(((Get-Date) - $StartDateTime).TotalSeconds, 2)
'memory.available' = [Math]::Round($MemoryAvailableMB, 0)
'lab.vmsCount.active' = ($AllVMs | Measure-Object).Count # how many VMs are running
'lab.vmsCount.provisioned' = $provisionedVMsCount # how many VMs were created by this script run
'lab.vmsCount.provisioned' = ($provisionedVMs | Measure-Object).Count # how many VMs were created by this script run
}
$properties = @{
'lab.timezone' = $TimeZone
Expand Down
1 change: 1 addition & 0 deletions Scripts/LabConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ $LabConfig=@{ DomainAdminName='LabAdmin'; AdminPassword='LS1setup!'; Prefix = 'W
DCVMVersion="9.0"; # (Optional) Latest is used if nothing is specified. Make sure you use values like "8.0","8.3","9.0"
TelemetryLevel=""; # (Optional) If configured, script will stop prompting you for telemetry. Values are "None","Basic","Full"
TelemetryNickname=""; # (Optional) If configured, telemetry will be sent with NickName to correlate data to specified NickName. So when leaderboards will be published, WSLab users will be able to see their own stats
AutoStartAfterDeploy=$false; # (Optional) If $false, no VM will be started; if $true or 'All' all lab VMs will be started after Deploy script; if 'DeployedOnly' only newly created VMs will be started.
AdditionalNetworksConfig=@(); # Just empty array for config below
VMs=@(); # Just empty array for config below
}
Expand Down

0 comments on commit ad6c61f

Please sign in to comment.