Saturday, February 18, 2017

Changing Nano back to DHCP

I've been playing around some more with Nano server, but I didn't want to create yet another VM, so I decided to use one of my existing Nano servers. But it had an IP set already, and was set to an Internal switch (so no internet connectivity possible). So let's PowerShell-fix this!

First let's change the adapter of my VM called nano1:

Get-VM nano1 |Get-VMNetworkAdapter|Connect-VMNetworkAdapter -SwitchName external

There, that didn't hurt so much! Good, now let's change the settings inside the nano1 VM, to set the IP's back to DHCP. First, this very cool thing in Hyper-V called PowerShell Direct, allows me to enter the VM without needing any IP connectivity. All you need is the credentials of the VM, and to select the "-VMName" option with Enter-PSSession:

$cred = Get-Credential
Enter-PSSession -VMName nano1 -Credential $cred

This is what it looks like:

PS C:\> $cred = Get-Credential
Enter-PSSession -VMName nano1 -Credential $cred
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
[nano1]: PS C:\Users\administrator\Documents>

As Montgomery Burns says: "Excellent...": Did you notice the [nano1] in front of the last prompt? We're inside the VM! BTW, you don't need to add the credential, it will ask you for the credentials if you don't add them, but if you go in and out more than once, this is nicer.

Now that we're in a session, let's change the IP address and DNS back to DHCP enabled:

Get-NetIPAddress -InterfaceAlias "Ethernet" | Set-NetIPInterface -Dhcp Enabled
Set-DnsClientServerAddress -ResetServerAddresses -InterfaceAlias Ethernet

Done!

Sunday, February 12, 2017

Linked clones in Hyper-V


I've allways been a fan of VMware Workstation, but I've been spending a bit of time with Hyper-V on Windows 10 now too to see how the cookies are on the other side. One thing I thought I missed was creating what is called linked clones in VMware Workstation.



Linked clones allow a user to create multiple VM's that are referenced off of only one VM. The changes compared to the first VM are stored in a so called delta file under the covers. This way, you can build a large lab while needing a lot less space. You will however need access to the original VM files.


It turns out Hyper-V has the same functionality, but the steps to create a linked clone are a bit more hidden, and the operations are a bit more manual. The steps are as follows:
  • You need a template VM to reference your linked clones from. No different from VMware Workstation. The difference though, is that with VMware Workstation, you can keep working with this VM. With Hyper-V, this template is going to be left as an image on your harddrive, but you can't use it.
  • Sysprep the template VM. Theoretically you should do that in VMware Workstation too, but if you have some quick test you want to do and don't require domain functionality this could be omitted.
  • Turn off the VM, and mark the vhdx file of the VM as read-only. You can even remove that VM out of Hyper-V, so you don't accidentally turn it back on again
  • In Hyper-V, go to Action->New->Hard Disk. Select "Differencing Disk" as harddisk type, set the destination location and select the vhdx from the VM you created earlier.
  • Finally, create a new VM and select the newly created differenced disk instead of a new harddisk.
A lot of steps for what is relatively simple wizard in VMware Workstation. I think we can do better than that ;-) Powershell to the rescue! In the case below I've been lazy, and created a function that has paths and memory and such hardcoded inside, but the idea is what counts:


  • You create a differencing disk with New-VHD
  • You create the VM
  • You add the harddisk

$Template = "C:\VM\2012 Template.vhdx"


function New-CloneVM
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([int])]
    Param
    (
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $Template,
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $NewVM
    )

    $destdisk = “C:\VM\$NewVM.vhdx”
    
    New-VHD –ParentPath $Template -Path $destdisk  –Differencing
    New-VM  $NewVM -MemoryStartupBytes 2048MB -SwitchName VLAN2 -Generation 2 -BootDevice CD
    Add-VMHardDiskDrive -VMName $NewVM -Path $destdisk -ControllerType SCSI
}


New-CloneVM -template $Template -NewVM Test1



If you quickly want to create a bunch of VM's for your testlab, Powershell and Hyper-V are a good alternative to VMware workstation, and in fact, since VMware Workstation doesn't support Powershell, Hyper-V might even be better.. I know, I know. blasphemy.

Wednesday, February 8, 2017

Turning on nested virtualization Hyper-V

I've been playing around with Hyper-V these past few weeks, and it seems to work rather well (VMware needs to start worrying).

One of the features I've been playing with is nested virtualization. Unlike VMware Workstation where you can select Hyper-V as one of  the install options, in Hyper-V on Windows 10 and Windows 2016, you can turn it on on a per VM basis, with the following command:

Set-VMProcessor -VMName NestHostVM -ExposeVirtualizationExtensions $true

Now you can have a Hyper-V inside your Hyper-V. I'm so hyped about this! <drumroll>

To get a list of which VM's have it turned on, do:

Get-VMProcessor -VMName * |select VMName, ExposeVirtualizationExtensions