liveBook · Manning

Hypervisor-based remoting

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

Hypervisor-based remoting is remote command execution that uses a hypervisor or virtualization-management intermediary instead of direct network communication with the guest machine.

Hypervisor-based remoting executes PowerShell on a virtual machine through a hypervisor or virtualization-management intermediary rather than through direct network communication with the guest. A control script communicates with the host, while the hypervisor integration tools create and run the remote session. section 5.5

This approach is useful when guest access is unavailable or undesirable, including initial machine configuration, DMZ systems, and cloud virtual machines. Host access can be sufficient even when the guest has no reachable network path. section 5.5

A practical implementation should account for these design points:

  • Select the provider integration: Hyper-V uses PowerShell Direct, VMware uses Invoke-VMScript from PowerCLI, and cloud platforms provide comparable virtual-machine command features. section 5.5
  • Pass virtual-machine names rather than assuming they match guest operating-system computer names. section 5.5
  • Optionally start stopped virtual machines before execution and shut them down afterward, while handling host capacity limits by processing machines individually. section 5.5

Scanning virtual machines for VS Code extensions

A control script can enumerate Hyper-V virtual machines, start each stopped machine, run a script that searches user-level VS Code extensions for vsixmanifest files, record failures, and optionally power machines off after completion. section 5.0 section 5.5

Listing 5.4 Connecting to all virtual machines from a Hyper-V host
$Credential = Get-Credential                               #1
$CsvFile = 'P:\Scripts\VSCodeExtensions.csv'               #2
$ScriptFile = 'P:\Scripts\Get-VSCodeExtensions.ps1'        #3
$ConnectionErrors = "P:\Scripts\VSCodeErrors.csv"          #4
 
$servers = Get-VM                                          #5
foreach ($VM in $servers) {
    $TurnOff = $false
    if ($VM.State -ne 'Running') {                         #6
        try {
            $VM | Start-VM -ErrorAction Stop               #7
        }
        catch {
            [pscustomobject]@{
                ComputerName = $s
                Date         = Get-Date
                ErrorMsg     = $_
            } | Export-Csv -Path $ConnectionErrors -Append
            continue                                       #8
        }
        $TurnOff = $true
        $timer = [system.diagnostics.stopwatch]::StartNew()
        while ($VM.Heartbeat -notmatch '^OK') {            #9
            if ($timer.Elapsed.TotalSeconds -gt 5) {
                break                                      #10
            }
        }
    }
 
    $Command = @{                                          #11
        VMId        = $Vm.Id
        FilePath    = $ScriptFile
        Credential  = $Credential
        ErrorAction = 'Stop'
    }
    try {
        $Results = Invoke-Command @Command                 #12
        $Results | Export-Csv -Path $CsvFile -Append
    }
    catch {
        [pscustomobject]@{                                 #13
            ComputerName = $s
            Date         = Get-Date
            ErrorMsg     = $_
        } | Export-Csv -Path $ConnectionErrors -Append
    }
 
    if ($TurnOff -eq $true) {                              #14
        $VM | Stop-VM
    }
 
}                                                          #15

Inspect the Hyper-V control-script pattern for credential handling, VM enumeration, conditional startup, error logging, and reusable remote execution structure. section 5.5

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Explains intermediary-based execution, provider choices, naming caveats, VM lifecycle, capacity limits, and control-script handling.
Examples
Uses Hyper-V VM enumeration, VS Code extension discovery, conditional startup, CSV error logging, and optional shutdown.
What it assumes
Assumes PowerShell control scripts and virtual-machine management knowledge, while distinguishing guest names from virtualization-platform names.
What you can do after
Operate remote PowerShell tasks through Hyper-V, VMware, or cloud integrations while managing VM state and recording failures.
How it connects
Connects PowerShell remoting with Hyper-V, PowerShell Direct, VMware PowerCLI, Invoke-VMScript, and cloud virtual-machine command features.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.