PowerShell Direct (PSD) is a new feature with Windows Server 2016 (and on Windows 10 Anniversary Update or later). PSD enables you to use PowerShell remoting to access a Hyper-V VM without needing to set up networking and firewall settings inside the VM. With PSD, you use Invoke-Command
, specifying either the VM's name or the VM's VMID (the VMID is a GUID used internally by Hyper-V to identify a VM) rather than the VM's hostname. You can also use the VM name or VMID to enter a remote session using Enter-PSSession
.
In earlier versions of Hyper-V, you needed a networking connection between your Hyper-V host and the guest OS in order to remote into the guest. This meant setting up and establishing network connectivity. With PSD, you can use the VM's name or ID and remote straight in. This is useful when a VM is misconfigured and its network connectivity is unavailable.
For this recipe, you need the Hyper-V host that you set up in the Installing and configuring Hyper-V recipe. You also need the VM setup in the Creating a virtual machine recipe. The VM has a VM name of PSDirect
, but has a hostname of Tiger
.
To ensure security, you need to specify credentials when you call Invoke-Command
or Enter-PSSession
. You can either specify the -Credential
parameter or let either cmdlet prompt for credentials. With Hyper-V and PSDirect
, the VM name and the hostname of the OS running inside the VM do not need to be the same.
This recipe uses the HV1
host you created in the Installing and configuring VM recipe, along with a Windows Server VM running on HV1
. This VM has a VM name of PSDirect
, a hostname of Tiger
, and a local administrator password of Pa$$w0rd
. You can use the Creating a VM recipe, suitably adapted, to create the PSDirect
VM.
PSDirect
:$RKAn = 'Administrator' $PS = 'Pa$$w0rd' $RKP = ConvertTo-SecureString -String $PS -AsPlainText -Force $T = 'System.Management.Automation.PSCredential' $RKCred = New-Object -TypeName $T -ArgumentList $RKAn,$RKP
PSDirect
VM:Get-VM -Name PSDirect
$SBHT = @{ VMName = 'PSDirect' Credential = $RKCred ScriptBlock = {hostname} } Invoke-Command @SBHT
$VMID = (Get-VM -VMName PSDirect).VMId.Guid $ICMHT = @{ VMid = $VMID Credential = $RKCred ScriptBlock = {hostname} } Invoke-Command @ICMHT
PSDirect
VM:Enter-PSSession -VMName PSDirect -Credential $RKCred Get-CimInstance -Class Win32_ComputerSystem Exit-PSSession
In step 1, you create a credential object for the local administrator of the PSDirect
virtual machine. This step creates no output.
In step 2, you retrieve the details of the PSDirect
virtual machine, which looks like this:
In step 3, you use the PSD feature and invoke a command inside the PSDirect
VM using just the virtual machine name, which looks like this:
In step 4, you invoke a command inside the PSDirect
virtual machine but using the VM's VMID value, like this:
In step 5, you enter a PS remoting session with the PSDirect
VM, which looks like this:
3.146.221.144