Using PowerShell Direct

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.

Getting ready

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.

Getting ready

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.

How to do it...

  1. Create a credential object for the local administrator on 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
  2. Retrieve and display the details of the PSDirect VM:
    Get-VM -Name PSDirect
  3. Invoke a command on the VM, specifying the VM name:
    $SBHT = @{
      VMName      = 'PSDirect'
      Credential  = $RKCred
      ScriptBlock = {hostname}
    }
    Invoke-Command @SBHT
  4. Invoke a command based on VMId:
    $VMID = (Get-VM -VMName PSDirect).VMId.Guid
    $ICMHT = @{
      VMid        = $VMID 
      Credential  = $RKCred  
      ScriptBlock = {hostname}
    }
    Invoke-Command @ICMHT
  5. Enter a PS remoting session with the PSDirect VM:
    Enter-PSSession -VMName PSDirect -Credential $RKCred
    Get-CimInstance -Class Win32_ComputerSystem
    Exit-PSSession 

How it works...

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:

How it works...

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:

How it works...

In step 4, you invoke a command inside the PSDirect virtual machine but using the VM's VMID value, like this:

How it works...

In step 5, you enter a PS remoting session with the PSDirect VM, which looks like this:

How it works...

There's more...

In step 3 and step 4, you make use of PSD by invoking a command using either the VM's name or the VM's VMID value. In step 5, you use Enter-PSSession and enter a PowerShell session in the PSDirect VM, and as you can see, the hostname of that VM is Tiger.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.221.144