Importing VM´s with PowerShell into Win8 Hyper-V 3.0

I am working on setting up my lab environment for the M50273 Design course that I am going to teach next week.

The import process is a bit heavy without powershell, cause it is 18 VMs that has to be imported. In a MOC (Microsoft Official Course) the lab setup consists of several VM´s that have base vhd disks and then several tiers of differential disks that need to be connected before import, in every directory there is a .bat file that creates the symlinks, I decided to keep them but run them in a script.

And here you can see the result of running the .bat file

The setup guide for the course says that I should install a Windows 2008 R2 host and then import the VM´s but i want to use Win8 instead, this to be able to run the built-in hyper-v cmdlets and also test if I could run the course VM´s on it.

I did not have this problem (Matthew Dowst) when importing my VM´s to the host but another networking issue occured, when checking the VM settings as you can see in the screen dump that the virtual network adapter is there but not working correctly, a little powershell again helps 😛

And this powershell command that takes the network adapter on the vm and connects it to the Internal switch :

<br />
Get-VM *HOU-DPM-E* | Get-VMNetworkAdapter | Connect-VMNetworkAdapter -SwitchName Internal<br />

So then it looks like this in VM settings:

So to automate the import process I created a small script that looks in a directory for VM´s in folders to import, then starts each .bat script to create the symlinks and imports the machine and lastly renames it (cause in the import process it is named REARMEDxxx) and connects the vm to the right network

As you can see, in this script i have not created a function or fancy parameters, that might come in a version 0.2.

<br />
# Import VMs for Course<br />
#<br />
# Niklas Akerlund / RTS 2012-04-04</p>
<p>$VMs = Get-Item -Path "E:\Program Files\Microsoft Learning\50273\Drives\*" | Select-Object Name,FullName</p>
<p>foreach ($VM in $VMs){</p>
<p>    if(Get-VM $VM.Name -ErrorAction SilentlyContinue ){<br />
        Write-Host $VM.Name "Already imported"<br />
    }else{<br />
        # Create Symlinks<br />
        $symbat = (Get-Item ($VM.FullName + "\*.bat")).FullName<br />
        & $symbat</p>
<p>        $expfile = (Get-Item ($VM.FullName + "\Virtual Machines\*.exp")).FullName</p>
<p>        Import-VM -Path $expfile </p>
<p>        Get-VM REA* | Rename-VM -NewName $VM.Name<br />
        Get-VM $VM.Name | Get-VMNetworkAdapter | Connect-VMNetworkAdapter -SwitchName Internal<br />
    }<br />
}<br />

It looks something like this when running

And when it is done I have all my 18 VMs imported and with the right network connected:

Virtual Machine Snapshots in Win8 Hyper-V massage with PowerShell

In the new win8 there is a function to merge the snapshot taken on a VM without putting it into saved state/turn off. In the Windows 2008 and 2008 R2 there was a big no-no to taking snapshots on production machines, how about now in the new version? Well their replication function uses snapshots so it must be good enough. Then as always you should consider the consequences of taking snapshots and rolling back on different systems as the data in a database may be lost when jumping around.

The Cmdlets that are relevant for Snapshots :

  • Checkpoint-VM
  • Get-VMSnapshot
  • Remove-VMSnapshot
  • Export-VMSnapshot
  • Restore-VMSnapshot
  • Rename-VMSnapshot

If you search among the cmdlets the first CheckPoint-VM is not very easy to find, I am woundering why they(MS) has decided that name?! (I have before complained about checkpoint in SCVMM, maybe it is from there it has the roots)

Here is a PowerShell oneliner that makes 50 Snapshots on a VM (the limit of 50 snapshots is still valid as i tested to make 60 snapshots and got errors from 50+)

<br />
for($i=1 ;$i -le 50 ; $i++){Checkpoint-vm -SnapshotName $i -VMName lajs}<br />

Here is the error.

How do I know how many snapshots i have on a VM?

Or if you must, check the GUI..

When I now have 50 snapshots I have to remove one before taking a new, the Remove-VMSnapshot has a parameter -IncludeAllChildSnapshots that is quite handy if you have like me 50 snapshots that you want to remove. I use the Select-Object -First 1 to get the first snapshot so i can remove all at once.

<br />
Get-VM lajs | Get-VMSnapshot | Select-Object -First 1 | Remove-VMSnapshot -IncludeAllChildSnapshots<br />

How about the cmdlet Export-VMSnapshot, it cannot be run when the VM is running, but it creates an exported VM that is from the snapshot. This can be very useful if you want to have a machine from a specific snapshot built.

How to go to a snapshot with Restore-VMSnapshot then, when I restore I have to confirm but not when deleting?

If you haven´t found the reference page on technet for WIN 8 Hyper-V powershell cmdlets it is here.

Automatic installation of SCVMM 2012 RC & SQL 2008 R2 and WAIK 3

Today I have been researching how to make a script that deploys a working installation of a VMM 2012 RC with a local SQL server without needing to manually type in anything.

What is strange is that the WAIK itself does not give you an option to do a quiet install! I had to use msiexec Transform to massage it to allow for an unattended install.

I used this MyITforum page to get a SQL config file, and this post by Christoffer got me the waik installation bit.

One thing i changed in the SQL config was this line:


because when i install on a OS with Swedish it will try to install with another Collation, this is no worries until you want to use this database server for SCOM 2012, because it requires the above Collation! 🙂

I am using the Start-Sleep cmdlet to get some time between the installations, also I have a check that the Setup process is still running and delay until it has finished,

You will have to download the WAIK 3.0 and the SCVMM 2012 RC, also you will have to copy all files from the SQL 2008 R2 ISO into a folder, as my script refers. Copy the content to subfolders like my screen dump and you should get it working. The .Net feature is installed during the SQL installation so i do not need to add it on its own.

<br />
# Install VMM unattended<br />
#<br />
# NIklas Akerlund /RTS</p>
<p># Install the SQL server<br />
c:\install\sql2008r2\setup /ConfigurationFile=c:\install\ConfigurationFile.ini</p>
<p># Install the WAIK<br />
msiexec /qb /i C:\install\KB3AIK_EN\wAIKAMD64.msi TRANSFORMS=c:\install\waikamd64.mst</p>
<p>Start-Sleep -s 60<br />
# Install the VMM server and console</p>
<p>C:\install\vmm12\setup.exe /server /i<br />
Start-Sleep -s 30<br />
while (Get-Process | where {$_.ProcessName -eq "SetupVM"}){<br />
Write-Host "Still installing server"<br />
Start-Sleep -s 2<br />
}<br />
Start-Sleep -s 10<br />
C:\install\vmm12\setup.exe /client /i<br />
Start-Sleep -s 30<br />
$Process = Get-Process | where {$_.ProcessName -eq "SetupVM"}<br />
while (Get-Process | where {$_.ProcessName -eq "SetupVM"}){<br />
Write-Host "Still installing Console"<br />
Start-Sleep -s 2<br />
}<br />

When i run it, it looks like this

There are some configuration files that could be used to set up other parameters for ports/license keys etc and they reside in the VMM setup directory and their names are VMServer.ini and VMClient.ini, for my demo/test setup i decided to not change anything. If you want to know more parameters in the VMM setup, just run in powershell console or CMD, setup /? and you will get a dialog with options

System Center Online Seminars: Deep Dive VMM 2012

Today Me self and Joachim Nasslander did our last Live Meeting about the System Center VMM 2012 in swedish for Microsoft. We did not have time to show all our demos so i thought that it would be an idea that i show them here in a blog post. There is no sound on the demo-videos, hopefully the LM that was recorded will show up on the Swedish MS technet soon…

The first thing that i showed was the Bare Metal Deployment of hosts, this is done with the help of a prepared VHD and a WDS (Windows deployment Server) that is registered to the VMM server. I have cut a bit of the waiting when the server was finalizing the configuration.

The next demo was after deployment of the host I used the built in functions in VMM 2012 to add hosts to a cluster

After this I show how to use the integration between the WSUS and the VMM which gives the opportunity to update a cluster and set the cluster nodes into maintenance mode and then update rolling through all hosts and the VM´s live migrating to the hosts.

I also made a demo on how to v2v migrate a VM from vSphere host to a hyper-v host when they are all connected to the same VMM 2012

Lastly I have made a demo on how to use Powershell within the VMM 2012 and how to use it to both do reporting and changing things, I have already made some blog posts about how to use the VMM 2012 powershell cmdlets.

Using SCVMM 2012 SP1 CTP to administer my Win 8 Hyper-V hosts

This night I saw that MS, listened to my tweet 😉  where I asked for possibility to manage Hyper-V 3 with the SC VMM2012. I have children that let me wake early and so I could start immediately to investigate the new SP1 and see how it works to handle Win8/HyperV3 hosts.

In my test environment I have two hosts  hyp31 and hyp32, I have also a virtual machine for the VMM (vmm3) and a virtual file server all of them running the Win8 Server Beta.

The installation was pretty straight forward, The only problem was that I had to have a internet connection to be able to get the .Net 3.5 feature installed.

When the VMM server and console was Installed I could login and add some hosts

As I already had set up the host and had VM´s on the fileshare it was easy to start testing Live Migration between the hosts (they are not in a cluster but both can access the fileshare).

<br />
Get-SCVirtualMachine MAJS | Move-SCVirtualMachine -VMHost HYP31 -Path "c:\VMs"<br />

I have to use the -Path parameter but only a xml config file resides on the host and my vhd harddisk remains on the fileshare \\win8fs\vmds , will do some research building a cluster and see how that works with the files later..


Using Windows PowerShell Web access for VMM2012 Administration

Today i had the pleasure to install and configure the Windows PowerShell Web Access in Windows 8. This gives you an opportunity to administer your internal servers with a Web Powershell Console. The really nice part here is that when I have enabled this I can connect to an Windows Server 2008 R2 with Powershell v2.

I have configured it with Authorization to everything, now this is a test environment but in your maybe you should be more restrictive,

<br />
Get-WindowsFeature WindowsPowerShellWebAccess | Add-WindowsFeature<br />
Install-PswaWebApplication -UseTestCertificate<br />
Add-PswaAuthorizationRule * * *<br />

So I can connect with for example a Ipad and administer my Hyper-V Cluster in SCVMM2012 😛

In this link you can see how to configure this on your Windows 8 Server Beta.

Here is the login prompt…

And here you can see how i run commands in the console, As you can see I must use Import-Module to import the VMM 2012 cmdlets before I can use them. This is a flash embedded video from youtube (where i uploaded it to) If you do not see it you can go to this link

I still have to find out why I cannot use the PowerCLI PSSnapin, I get an OutOfMemory exception when trying to connect-viserver 🙁 will have to dig a bit deeper to check that one out. Maybe there is a bug in the PSSnapin, an excellent time for VMware to make a Module instead!

Enable/Disable Core mode on Windows 8 Server Beta

Today I have been researching about how to enable and disable the GUI on the Windows 8 Server beta. As it is a best practice to run your Hyper-V hosts in Core I wanted to know how to do this, you might want to configure some things in full GUI mode and then run the server in core after you are done. Going from the full gui to Core was quite easy, i just ran the following commands in powershell and rebooted, then i was in the cmd console world of Core.

When i then wanted to get back to the GUI world i just ran the command Add-WindowsFeature instead of remove

Then i had to test to install a Core and try to get it to full. The Beta does only have two editions to choose from.

When the machine was finished i tried to use the Sconfig tool that is a configuration helper in Core, and in that you have a selection 12 that are supposed to enable GUI, but despite that it said 100% nothing happend, so this was a bit trickier because when i choose the Core in the installation, not all files are deployed in the installation, this to save space and unnecessary files/functions. This does however create some extra work when you realize that you want the GUI, at least nowadays you do not have to reinstall the whole machine :-).

How do i solve it then, I found a presentation from Jeffrey Snover and Andrew Mason at the Build conference which led me to the imageX binary that i can mount the install.wim file. one would think that just mounting the win8 ISO would be enough but no, i had to do some imageX magic, i used the one from the Win7 WAIK and copied it into the core machine, as i am a ps-fan i copied it with powershell ,then i had to mount the wim file with the imagex and then run the cmdlets to enable the GUI. At least it feels easier using powershell than DISM /Online bla bla..

<br />
copy-item \\win8fs\share\imagex.exe c:\temp</p>
<p>mkdir c:\win8wim</p>
<p>.\imagex.exe /mount d:\sources\install.wim 2 c:\win8wim<br />

I ran the following, and as you can see i had to specify the windows folder on the wim where the winSxS (this is where the missing files are) is a direct subfolder,

<br />
Get-WindowsFeature Server-G* | Add-WindowsFeature -Source C:\win8wim\windows<br />
Restart-Computer<br />

When going from core -> full or the other way around it is some waiting when the configuration is done.. it is not quite as easy as the Linux and the run-levels (init 3, init 5), maybe MS should have been looking a bit there when implementing their solution.

Update: As Snover commented below, it actually works to use sconfig and “Restore GUI”, but this is when your server is connected to the Internet and can download the files needed, I tried it and it took over 1 hour and then it asked for a restart to finish. Not every organisation do connect their servers directly to the Internet, maybe it could work then with a local WSUS server?!

Managing Hyper-V 3.0 Host and VM´s with powershell

So now i have started digging into the powershell cmdlets that are available for the Hyper-V in Windows 8 Server beta. As you can see on my screendump and also on this link there are some, i would love to see some extensions when the product becomes RTM, I am spoiled with the functionality and extensibility of the PowerCLI 😛

One thing that i like is that with powershell it is easy to actually find out what NIC you want to use in your virtual switches, why Microsoft insists of showing the description instead of name in the Hyper-V virtual switch admin gui? i have no idea which nic is which,

I do have a good name for them in the Network Connections

in the wonderful world of powershell it is now easy to create a virtual switch and actually get the right nic, also with the parameter -AllowManagementOS i make sure that my Host will not share this nic.

<br />
New-VMswitch -Name Intern -NetAdapterName VMSwitch -AllowManagementOS $false<br />

Then i can deploy my VMs, I am woundering why the virtualization team is using bytes in their parameters ? -MemoryStartupBytes -NewVHDSizeBytes, fortunately you can use -MemoryStartupBytes 512MB or 1GB and powershell calculates it to bytes. Another thing that I am wondering about is that when i create a VM i cannot set number of vCPU, i have to change that after the VM has been created?!

<br />
New-VM -Name vNiklasDC -NewVHDSizeBytes 20GB -SwitchName Intern -MemoryStartupBytes 512MB -NewVHDPath C:\VMs\vNiklasDC\vNiklasDC.vhdx<br />
Set-VM -Name vNiklasDC -ProcessorCount 2 -DynamicMemory -MemoryStartupBytes 512MB -MemoryMaximumBytes 1GB<br />

More updates will come as i discover more about the new Windows 8 Server beta..


Enable Hyper-V 3.0 with Powershell in Windows 8 Server beta

How do you enable Hyper-V with powershell in the new win 8 Server beta, as you can see when running the Get-WindowsFeature Hyper-V i only see the Hyper-V role, but i also want the administration tools, best practice says that you should run the Hyper-V server as clean as possible and then you might want to enable the management tools on the administration server, but now i only have one server and want to be able to create VMs and configure etc.. In the screenshot i had already enabled the Hyper-V role.

To enable all Hyper-V Role/features i just type like this,

<br />
Get-WindowsFeature Hyper-V* | Add-WindowsFeature<br />

I will test some Hyper-V powershell cmdlets after this and that might result in another post 😛

Add my own custom properties and populate them with powershell in VMM2012

I have made a powershell script function to update information about what VLAN and IP the VM´s have. This for the IT Admin to easily see in the VMM 2012 Console. Of course the function can be customized to populate any other information available for the VM´s, maybe you want to display create date or something else that is not in the standard properties and you do not want to use the pre-made custom# .

I am using the Get-IPAddress from Ben Wilkinson, This requires that the VMM server can resolve the IP of the VM´s from a DNS.

If I do not have an DNS or the VLAN info is not stored, i can via the parameters update the properties by -IP or -VLANid.

As you can see on this screen dump, by default i only have custom#, to add my own i have to click on “Manage Custom Properties”

Here i press “Create” and add a name and optionally a description

Then when i have created the custom properties i want i add them,

Then it looks like this,

Now the powershell function comes into play when i am going to populate these fields with information.

<br />
function Set-CustomValue{<br />
# Function to add custom data on VMs<br />
#<br />
# Niklas Akerlund /RTS 2012-02-18<br />
    param (<br />
   [Parameter(Position=0,Mandatory=$true,HelpMessage="A virtual machine pleaze",<br />
    ValueFromPipeline=$True)]<br />
    $VM,<br />
	$VLANid = "",<br />
	$IP = ""<br />
    )<br />
    . .\Get-IPAddress.ps1</p>
<p>    $VM = Get-SCVirtualMachine $VM</p>
<p>  	$CustomIP = Get-SCCustomProperty -Name "IP"<br />
    $CustomVLAN = Get-SCCustomProperty -Name "VLAN"</p>
<p>	if ($IP -eq ""){<br />
		$IP = Get-IPAddress -HostName $VM.Name<br />
<p>	if ($VLANid -eq ""){<br />
	    $VMnics = $VM | Get-SCVirtualNetworkAdapter<br />
	    if($VMnics.Count -ne $null){<br />
	        foreach ($VMnic in $VMnics){<br />
	            $VLANid = $VLANid + $VMnic.VlanID + " "<br />
	        }<br />
	        Set-SCCustomPropertyValue -InputObject $VM -CustomProperty $CustomVLAN -Value $VLANid<br />
	    }else{<br />
	        Set-SCCustomPropertyValue -InputObject $VM -CustomProperty $CustomVLAN -Value $VMnics.VlanID<br />
	    }<br />
    }else {<br />
		Set-SCCustomPropertyValue -InputObject $VM -CustomProperty $CustomVLAN -Value $VLANid<br />
	}<br />
    if($IP -ne ""){<br />
        Set-SCCustomPropertyValue -InputObject $VM -CustomProperty $CustomIP -Value $IP<br />
    }<br />
}<br />

a screendump of my powershell console, as you can see I am using the (Get-SCVMHostCluster).Nodes to get all virtual machines on these, also i am using a foreach (%) to run the population on each VM.

And this is how it looks in the VMM console,