Skip to content

Instantly share code, notes, and snippets.

@crohr
Created March 23, 2026 16:56
Show Gist options
  • Select an option

  • Save crohr/b8d6d3a2bd187494bf3636896707d571 to your computer and use it in GitHub Desktop.

Select an option

Save crohr/b8d6d3a2bd187494bf3636896707d571 to your computer and use it in GitHub Desktop.
Windows Nested Virtualization
=== Hyper-V isolated container smoke ===
Hyper-V isolated container image=mcr.microsoft.com/windows/servercore:ltsc2025
Hyper-V isolated container output begin
container> Unable to find image 'mcr.microsoft.com/windows/servercore:ltsc2025' locally
container> ltsc2025: Pulling from windows/servercore
container> 97e96e9cbeb1: Pulling fs layer
container> 193f32f0b02a: Pulling fs layer
container> 193f32f0b02a: Verifying Checksum
container> 193f32f0b02a: Download complete
container> 97e96e9cbeb1: Verifying Checksum
container> 97e96e9cbeb1: Download complete
container> 97e96e9cbeb1: Pull complete
container> 193f32f0b02a: Pull complete
container> Digest: sha256:a4d6cb8427e90fd15e39bef29e0e800465989793d2a0826d3b7ceba78af0fe34
container> Status: Downloaded newer image for mcr.microsoft.com/windows/servercore:ltsc2025
container>
container> Microsoft Windows [Version 10.0.26100.32522]
Hyper-V isolated container output end
Hyper-V isolated container exit code=0
Hyper-V isolated container result=passed
=== Nested VM smoke ===
Nested VM state=Running
Nested VM smoke result=passed
@crohr
Copy link
Copy Markdown
Author

crohr commented Mar 23, 2026

Script:

  Write-Host "=== Hyper-V isolated container smoke ==="
  $containerImage = "mcr.microsoft.com/windows/servercore:ltsc2025"
  Write-Host "Hyper-V isolated container image=$containerImage"
  try {
    $previousNativeCommandUseErrorActionPreference = $PSNativeCommandUseErrorActionPreference
    $PSNativeCommandUseErrorActionPreference = $false
    try {
      $containerOutput = & docker run --rm --isolation=hyperv $containerImage cmd /c ver 2>&1
      $containerExitCode = $LASTEXITCODE
    }
    finally {
      $PSNativeCommandUseErrorActionPreference = $previousNativeCommandUseErrorActionPreference
    }
  
    if ($null -eq $containerOutput -or @($containerOutput).Count -eq 0) {
      Write-Host "Hyper-V isolated container output=<none>"
    }
    else {
      Write-Host "Hyper-V isolated container output begin"
      @($containerOutput) | ForEach-Object {
        Write-Host "container> $_"
      }
      Write-Host "Hyper-V isolated container output end"
    }
  
    Write-Host "Hyper-V isolated container exit code=$containerExitCode"
    if ($containerExitCode -ne 0) {
      Write-Host "Hyper-V isolated container result=failed"
      Add-Failure "Hyper-V isolated container smoke failed for ${containerImage} with exit code $containerExitCode"
    }
    else {
      Write-Host "Hyper-V isolated container result=passed"
    }
  }
  catch {
    Write-Host "Hyper-V isolated container result=failed"
    Write-Host "Hyper-V isolated container exception=$($_.Exception.Message)"
    Add-Failure "Hyper-V isolated container smoke failed for ${containerImage}: $($_.Exception.Message)"
  }
  
  Write-Host "=== Nested VM smoke ==="
  $hasNewVMCommand = $null -ne (Get-Command New-VM -ErrorAction SilentlyContinue)
  if (-not $hasNewVMCommand) {
    Write-Host "Nested VM smoke result=skipped"
    Write-Host "Nested VM smoke skip_reason=New-VM cmdlet unavailable"
  }
  else {
    $vmName = "runs-on-nested-virt-smoke-$([guid]::NewGuid().ToString('N').Substring(0,8))"
    $switch = Get-VMSwitch | Select-Object -First 1
  
    try {
      $vm = New-VM -Name $vmName -Generation 2 -MemoryStartupBytes 512MB -NoVHD
      if ($switch) {
        Connect-VMNetworkAdapter -VMName $vmName -SwitchName $switch.Name
      }
  
      Start-VM -Name $vmName -ErrorAction Stop | Out-Null
      Start-Sleep -Seconds 5
  
      $state = (Get-VM -Name $vmName -ErrorAction Stop).State
      Write-Host "Nested VM state=$state"
      if ($state -ne "Running") {
        throw "Expected nested VM to reach Running state, got $state"
      }
  
      Write-Host "Nested VM smoke result=passed"
    }
    catch {
      Write-Host "Nested VM smoke result=failed"
      Add-Failure "Nested VM smoke failed: $($_.Exception.Message)"
    }
    finally {
      if (Get-VM -Name $vmName -ErrorAction SilentlyContinue) {
        Stop-VM -Name $vmName -TurnOff -Force -ErrorAction SilentlyContinue | Out-Null
        Remove-VM -Name $vmName -Force -ErrorAction SilentlyContinue
      }
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment