March 13, 2020

Typing text on a virtual machine keyboard via PowerShell CIM Cmdlets

Retail Windows media contains a boot loader that requires keyboard input before setup begins. In automation scenarios, you can use CIM Cmdlets to type keys over WinRM. (This is preferred over deprecated WMI Cmdlet use.)

// ...

$CimSession = New-CimSession `
  -ComputerName $VmHost `
  -Credential $VmHostCredentials
        
$CimInstance = Get-CimInstance `
  -CimSession $CimSession `
  -Namespace "root\virtualization\v2" `
  -ClassName Msvm_ComputerSystem `
  -Filter "ElementName = '$VmName'"
  
$Keyboard = Get-CimAssociatedInstance $CimInstance `
  -CimSession $CimSession `
  -ResultClassName Msvm_Keyboard

Invoke-CimMethod $Keyboard `
  -CimSession $CimSession `
  -MethodName TypeText `
  -Arguments @{ asciiText = " "}