The Deployment Bunny

OS Deployment, Virtualization, Microsoft based Infrastructure…

  • about.me

    about.me/mikaelnystrom

  • Archives

  • Meta

Archive for the ‘Hyper-V’ Category

PowerShell is King – Find VM Configuration files (.vmcx) that have not been imported/registered

Posted by Mikael Nystrom on November 9, 2016

Sometime you need to move a large number of VMs, or you need to recover from a broken hyper-v host, or similar. If you have a few VM’s, that will be easy, but if you have many you will most likely use PowerShell to import the VM’s. That works perfectly fine, but you have a few VM’s that did weren’t imported correctly you will end in a state when you ask your self –Which VM’s are imported and which ones are not?

(You can download the script from GitHub: https://github.com/DeploymentBunny/Files/tree/master/Tools/Get-VIAUnimportedvmcxFiles )

Find the not yet imported VM configurations

That will of course be done using PowerShell, and here is the function that I use

image
Script and output.

Function Get-VIAUnimportedvmcxFiles
{
    <#
    .Synopsis
        Script used find not yet imported Hyper-V Configurations
    .DESCRIPTION
        Created: 2016-11-07
        Version: 1.0
        Author : Mikael Nystrom
        Twitter: @mikael_nystrom
        Blog   : http://deploymentbunny.com
        Disclaimer: This script is provided "AS IS" with no warranties.
    .EXAMPLE
        Get-VIAUnimportedvmcxFiles
    #>    
    [CmdletBinding(SupportsShouldProcess=$true)]
    
    Param(
    [string]$Folder
    )

    if((Test-Path -Path $Folder) -ne $true){
        Write-Warning "I'm sorry, that folder does not exist"
        Break
    }

    $VMsIDs = (Get-VM).VMId
    $VMConfigs = (Get-ChildItem -Path $Folder -Filter *.vmcx -Recurse).BaseName

    $obj = Compare-Object -ReferenceObject $VMsIDs -DifferenceObject $VMConfigs

    $Configs = foreach($Item in ($obj.InputObject)){
        $Items = Get-ChildItem -Path $Folder -Recurse -File -Filter *.vmcx  | Where-Object -Property Basename -Like -Value "*$Item" 
        $Items | Where-Object -Property FullName -NotLike -Value "*Snapshots*"
        }
    Return $Configs.FullName
}

/mike

Posted in Hyper-V, PowerShell | Tagged: , | Leave a Comment »

PowerShell is King – Find VHDs that are not connected to a VM

Posted by Mikael Nystrom on November 8, 2016

Working with VM’s in Hyper-V means that you will eventually have VHD’s that are not connected, not not even in use. That happens when you remove the VM configuration from Hyper-V manager, but you told your self “I will remove the data files later”, later very rarely happens, at least not for my lab and my demo environment (in production we use SCVMM and this issues is much less). In many cases this consumes space and I can use that space for other purposes.

(The script is available at GitHub: https://github.com/DeploymentBunny/Files/tree/master/Tools/Get-VIADisconnectedVHDs)

Get-VM, Get-ChildItems and Compare-Object solves the problem

Here is a function that will return an array of files from a folder you specify that is not directly connected to a VM.

image
In this case one disk is returned and this disk is not in use.

Function Get-VIADisconnectedVHDs
{
    <#
    .Synopsis
        Script used find .VHD files that are not connected to VM's
    .DESCRIPTION
        Created: 2016-11-07
        Version: 1.0
        Author : Mikael Nystrom
        Twitter: @mikael_nystrom
        Blog   : http://deploymentbunny.com
        Disclaimer: This script is provided "AS IS" with no warranties.
    .EXAMPLE
        Get-Get-VIADisconnectedVHDs
    #>    
    [CmdletBinding(SupportsShouldProcess=$true)]
    
    Param(
    [string]$Folder
    )

    if((Test-Path -Path $Folder) -ne $true){
        Write-Warning "I'm sorry, that folder does not exist"
        Break
    }

    #Get the disk used by a VM
    $VMs = (Get-VM | Where-Object -Property ParentSnapshotName -EQ -Value $null).VMId

    if(($VMs.count) -eq '0'){
        Write-Information "Sorry, could not find any VM's"
        Break
    }
    $VHDsActive = foreach($VMsID in $VMs){
        Get-VMHardDiskDrive -VM (Get-VM -Id $VMsID)
    }

    #Get the disk in the folder
    $VHDsAll = Get-ChildItem -Path $Folder -Filter *.vhd* -Recurse
    if(($VHDsAll.count) -eq '0'){
        Write-Information "Sorry, could not find any VHD's in $folder"
        Break
    }

    $obj = Compare-Object -ReferenceObject $VHDsActive.Path -DifferenceObject $VHDsAll.FullName

    #Compare and give back the list of .vhd's that are not connected
    Return ($obj | Where-Object -Property SideIndicator -EQ -Value =>).InputObject
}

/Mike

Posted in Hyper-V, PowerShell | Tagged: , | Leave a Comment »

PowerShell is King – Find out what Differencing disks that are in use

Posted by Mikael Nystrom on November 7, 2016

I use Hyper-V, in both production as well as for lab. One very effective way to spin up systems very fast is to use differencing disks, it also saves space. So I use differencing disks basically. After a while you tend to have some VM’s in your environment, and when it comes to a lab environment, well lets put it this way, I have disks all over the place.

(The function is on GitHub: https://github.com/DeploymentBunny/Files/tree/master/Tools/Get-VIAActiveDiffDisk)

I need to know what Parent hard drives that are active and in use

So, PowerShell to the rescue. First we get all hard disks that are connected to any of my VM’s, then we grab each disk that has a parent disk, an then we present that. That should give me a list of what parent disks are actually used.

   
Function Get-VIAActiveDiffDisk{
    <#
    .Synopsis
        Script used to Deploy and Configure Fabric
    .DESCRIPTION
        Created: 2016-11-07
        Version: 1.0
        Author : Mikael Nystrom
        Twitter: @mikael_nystrom
        Blog   : http://deploymentbunny.com
        Disclaimer: This script is provided "AS IS" with no warranties.
    .EXAMPLE
        Get-VIAActiveDiffDisk
    #>    
    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
    )

    $VMHardDiskDrives = Get-VMHardDiskDrive -VM (Get-VM)
    $ActiveDisks = foreach($VMHardDiskDrive in $VMHardDiskDrives){
        $Diffs = Get-VHD -Path $VMHardDiskDrive.Path | Where-Object -Property VhdType -EQ -Value Differencing
        $Diffs.ParentPath
    }
    $ActiveDisks | Sort-Object | Select-Object -Unique
}

On my laptop that gives the the following return:

image
The output.

Posted in Hyper-V, PowerShell | Tagged: , | Leave a Comment »

PowerShell is King – Simple function to test a VMSwitch is present

Posted by Mikael Nystrom on May 27, 2016

When building VM’s, they are usually connected to a Hyper-V switch, so when working for a customer I suddenly needed to verify that the switch actually exists before building the VM. One way to solve this is of course to use Get-VMSwitch –Name, but if it cant find it, it blows up and turns red and angry, the other way is to use “Count”, that way I can return whatever I need, in this case I Return True or False.

Usage:

Create a .psm1 file, copy the content, paste into the file, save the file and use the Import-Module function. After that you can test it by running commands similar to this.

image
Using the function to test a VMswitch is present on the local machine.

The Function:

Function Test-FAVMSwitchexistence
{
    Param(
        [string]$VMSwitchname
    )
        $Item = (Get-VMSwitch | Where-Object -Property Name -EQ -Value $VMSwitchname).count
        If($Item -eq '1'){Return $true}else{Return $false}
}

Mike

Posted in Hyper-V, PowerShell | Tagged: , | Leave a Comment »

Windows Server 2016 – Create a NAT network using the Hyper-V switch

Posted by Mikael Nystrom on March 3, 2016

In Windows Server 2016 (TP4) and Windows 10 1511 it is possible to create a Hyper-V Switch with NAT functionality, including publishing rules. The purpose is to run containers, but it can of course be used to run normal virtual machines. This means that you can very easy build an infrastructure environment with access to Internet and reverse without having a virtual router or firewall, pretty neat IMHO. Currently, the only way to create the switch is using PowerShell, but hey, I don’t mind…

Create the Switch

The following command will create a VM Switch for NAT usage with a subnet of 192.168.1.0/24. The net result will be a switch and an Internal network adapter with the IP address 192.168.1.1

New-VMSwitch -Name ViaMonstraNAT -SwitchType NAT -NATSubnetAddress 192.168.1.0/24

Create the NAT rule to get out

The following command will create a NAT rule for all machines connected to the Switch that uses the default gateway to get out.

New-NetNat -Name ViaMonstraNAT -InternalIPInterfaceAddressPrefix 192.168.1.0/24

Create a publishing rule

The following rule will open the “firewall” and redirect traffic on the hyper-v host port tcp/80 to the machine on the VM Switch with the IP address 192.168.1.200 to tcp/80

Add-NetNatStaticMapping -NatName ViaMonstraNAT -Protocol TCP -ExternalPort 80 -InternalIPAddress 192.168.1.200 -InternalPort 80 -ExternalIPAddress 0.0.0.0

Check if it works

You can use the following PowerShell commandlets to see the configuration after it has been done.

image
Get-VMSwitch will show you the switch with SwitchType NAT.

image
Get-NetNat will show you the NAT configuration bound to the switch.

image
Get-NetNatStaticMapping will show you the publishing rule.

image
Get-NetNatSession will show you current NAT sessions.

/Mike

Posted in Hyper-V, Windows 10, Windows Server 2016 | Tagged: , , | 3 Comments »

Working in the Datacenter–Enable Virtual TPM in Hyper-V gives you the ability to test bitlocker in a VM

Posted by Mikael Nystrom on January 26, 2016

Last night a friend contaced me and said “-Did you ever post the vTPM thing?”, i did say yes, but i was wrong, so here it is…

Simple, without testing and verfication, a deployment solution will fail. One of the tasks that takes a lot of time to test and verify is BitLocker and that also includes TPM. Windows 10 and Windows Server 2016 gives you the ability to create Virtuial Machines with a Virtual TPM Chip 2.0.

image
A VM running Windows Server 2012 R2 with a vTPM chip, The VM is running on Windows Server 2016.

The How-To Part

You need to run Windows Server 2016 TP4 or Windows 10.

On the host, add Isolated UserMode, Hyper-V and Hostguardian Services, by running the following powershell command(elevated):

Add-WindowsFeature -Name “Isolated-UserMode”,”Hyper-V”,”HostGuardian” -IncludeAllSubFeature –IncludeManagementTools

If needed, restart the host.

Before you can enable the vTPM you need to have a Guardian Service guardian object and with that you can crerate a Key Protector.

New-HgsGuardian -Name ‘Guardian’ -GenerateCertificates
$Owner = Get-HgsGuardian -Name ‘Guardian’
$KeyProtector = New-HgsKeyProtector -Guardian $Owner -Owner $Owner –AllowUntrustedRoot

Great, the last piece is to enable the vTPM

Set-VMKeyProtector -VMName ‘WSUS01’ -KeyProtector $KeyProtector.RawData
Enable-VMTPM -VMName ‘WSUS01’

/Mike

Posted in Deployment, Hyper-V, OSD, Windows 10, Windows Server 2016, Windows Server vNext | Tagged: , , , , , | 4 Comments »

Working in the Datacenter – Add-DVDDrive does not work correctly in Windows Server 2016 TP4 (or in Windows 10)

Posted by Mikael Nystrom on December 17, 2015

Update 2015-12-17 : This is now a confirmed bug, and as soon as I know more I will update this post.

It seems to be a bug, hopefully it will be fixed soon. The issue is very simple. If you try to run Add-VMDvdDrive the –path must be specified, in previous version that could be left alone. This problem is more common when you create VM Gen 2, since it does not have a DVD by default, and yes when we build VM’s they usually have a empty DVD for various reasons. According to help in the command let, there is no differences in the cmdlet between 1.1 and 2.0, but in reality it is.

The Issue

The problem is that when using the command Add-VMDvdDrive -VMName $VMName it fails with Add-VMDvdDrive : Exception of type ‘System.ArgumentException’ was thrown because it does not have a path, so i have seen workarounds when you create a small ISO and mount that and then you can remove that, but that sucks. There are some other issues as well.

image
The issue.

The Workaround

Luckily there is 2 different PowerShell modules, 1.1 for older OS and 2.0 for Windows 10/Windows server 2016 TP4 so the only thing you need to do is unload the new PowerShell module for Hyper-V and load the old one, and when you are done, you can load the new module again.

(if you would like to know why there is 2 versions, here you go: http://blogs.msdn.com/b/virtual_pc_guy/archive/2015/11/16/why-are-there-two-hyper-v-powershell-modules-in-windows-10.aspx)

image
We run this in the beginning of the script to replace the module.

image
We run this in the end of the script to restore the module.

/mike

Here is the code on GHitHub

Posted in Datacenter, Hyper-V, PowerShell, Windows 10, Windows Server 2016 | Tagged: , , , | 1 Comment »

Working in a Datacenter – Nested Hyper-V or Running Hyper-V in Hyper-V

Posted by Mikael Nystrom on November 21, 2015

image

There are many reason where it make sense to run Hyper-V in Hyper-V, one of them being to enable Credential Guard (VSM) in Windows Server 2016 TP 4 and later. For training, demos, test. R&D it is great. For Windows Server 2016 TP4 it needs to be enable and configured to work and that means PowerShell. Currently there are also some limitations.

On the Host:

Device Guard Disable
Credential Guard Disable
Hyper-V Enabled
Hardware Intel VT-x
Windows Version Build 10565 or greater

 

In the VM:

Dynamic Memory No
Change memory while VM is running No
Using any kind of Checkpoint No
Live Migration No
Save/Resume No

 

You can read the fine print here: https://msdn.microsoft.com/en-us/virtualization/hyperv_on_windows/user_guide/nesting

The PowerShell Function/Script:

This script will enable Nested Hyper-V an a VM
Invoke-WebRequest "https://raw.githubusercontent.com/DeploymentBunny/Files/master/Tools/Enable-NestedHyperV/EnableNestedHyperV.ps1" -OutFile ~/EnableNestedHyperV.ps1
Import-Module ~/EnableNestedHyperV.ps1
Enable-NestedHyperV -VMname TEST100
This Script (Provided be Microsoft) will verify configuration
Invoke-WebRequest "https://raw.githubusercontent.com/Microsoft/Virtualization-Documentation/master/hyperv-tools/Nested/Get-NestedVirtStatus.ps1" -OutFile ~/Get-NestedVirtStatus.ps1
~/Get-NestedVirtStatus.ps1

/mike

Posted in Datacenter, Hyper-V, Windows 10, Windows Server, Windows Server 2016, Windows Server vNext | Tagged: , , , , | 1 Comment »

PowerShell is King – Compress, Compact, Optimize VHD and VHDX files

Posted by Mikael Nystrom on June 15, 2015

I was reading a thread at Facebook in our user group, someone was trying to compress/compact/optimize a VHD(VHDX files but the result was depressing, the amount of reclaimed space was zero and that is kind of low… Now there is a good explanation for that, when using the UI there is no way to configure it and the UI also don’t inform you that the ONLY way to get it small is to mount it in Read Only mode. It really has to be mounted, otherwise there is now way to see “inside” the disk and to get rid of all the Zero blocks. It does not hurt if you also run defrag and cleanup inside the VM before, but the most important thing is that it needs to be mounted.

The documentation for the PowerShell command says:

image

The Script:

So, to make things easy for my friends in the User Group I created a simple script that will find the disks for the VM, check if the VM is running and if not mount each file in read only mode with no drive letter, optimize the VDH and dismount it. So the output of the script looks like this when running using –verbose

image

and here is how the script looks like:

image

and you can download the script here…http://1drv.ms/1IES3pB

/mike

Posted in Hyper-V, PowerShell | Tagged: , , | 13 Comments »

Windows Server vNext – Hyper-V in Hyper-V

Posted by Mikael Nystrom on May 1, 2015

Yes, it is true

image

Continue reading the story at the source for much more information!!!

http://www.hyper-v.nu/archives/hvredevoort/2015/05/nested-hypervisor-in-windows-server-vnext/

Posted in Hyper-V, Windows Server vNext | Tagged: , | Leave a Comment »