diff --git a/Scripts/3_Deploy.ps1 b/Scripts/3_Deploy.ps1 index e70a6e9a..ce0979a9 100644 --- a/Scripts/3_Deploy.ps1 +++ b/Scripts/3_Deploy.ps1 @@ -653,7 +653,7 @@ If (-not $isAdmin) { } # return info - @{ + [PSCustomObject]@{ OSDiskPath = $vhdpath VM = $VMTemp } @@ -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 @@ -1488,7 +1488,7 @@ If (-not $isAdmin) { $vmDeploymentEvents += $vmInfo } - $provisionedVMsCount += 1 + $provisionedVMs += $createdVm.VM } } @@ -1530,6 +1530,44 @@ 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" @@ -1537,7 +1575,7 @@ If (-not $isAdmin) { '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 diff --git a/Scripts/LabConfig.ps1 b/Scripts/LabConfig.ps1 index 65d405e1..91e3f834 100644 --- a/Scripts/LabConfig.ps1 +++ b/Scripts/LabConfig.ps1 @@ -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 }