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
