VMware to Hyper-V Migration with Netapp shift and MAT

I have been doing some seminars and presentations on the subject of migration from VMware. My latest seminars was with my colleague Kristian Nese and we were in Stockholm, Oslo and Trondheim talking about the cloud os, the new features in Hyper-V 2012 R2 and System Center VMM 2012 R2 and also migration and the different tools. After the summer camp at Microsoft I did a blog post about my customisations of the MAT that was running with MVMC and my way to solve the IP addresses and after my blog post there have come an updated version of the MAT that do handle the NIC.

If you plan to do a migration and just happens to be one of the lucky guys that have a Netapp box with Data ONTAP 8.2 in Cluster Mode in your VMware environment you can use the MAT for shift that can be found on the gallery. It utilizes the NetApp Data ONTAP PowerShell toolkit and the features in the Netapp storage that converts the vmdk files to vhdx right in the storage and boy do that save migration time as you do not have to move loads of data for your large VM´s over the network and then convert them at the target. As the Netapp software is so intelligent it does not write over the old data and you can quite easily go back if you find any issues after the migration.

Building Clouds have a blog post and Migration Mark has done a funny movie that can be watched on Youtube that explains the procedure and how fast it is!

Hope to start a project really soon with a Netapp customer 🙂

VMware to Hyper-V Migration with MAT and my addons

Today I was on Sommarkollo at Microsoft Sweden and speaking on the event and this was the last of two sessions about the MAT (Migration Automation Toolkit).

This automation tool that I first got to know about at MMS 2013 in Las Vegas (sadly it looks like there will be no more MMS 🙁 but that is another story ) in the session held by “Migration Mark” and Matt McSpirit WS-B325 and there in the video about 41 minutes into I ask about how the MAT take care of IP addresses on the migrated VM´s and they answered me that is not part of the MAT yet! And well I would say that this would be quite painful to migrate 100-200 VM´s with this automation tool and still have to enter IP settings for each migrated vm manually!!

But I have an example on how that can be done, I have not integrated it into the MAT scripts yet but it automates the collection of not just the IP addresses but also dns,subnet and gateway.

The information is available via the PowerCLI from the vCenter and here I show you how you can collect all that info after you have got the migration list

<br />
# Collect Networking info from VM´s<br />
#<br />
# Niklas Akerlund / 2013-07-01<br />
if(!(Get-PSSnapin "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue)){<br />
    Add-PSSnapin "VMware.VimAutomation.Core"<br />
}<br />
$MigVMs = Get-Content "d:\mat\vmlist.txt"<br />
$VMNetConf = @()<br />
Connect-VIServer vCenter.vniklas.com<br />
remove-module Hyper-V -ErrorAction SilentlyContinue</p>
<p>foreach ($MigVM in $MigVMs){<br />
     $VM = Get-VM $MigVM.Split(".")[0]<br />
     #write-host $VM<br />
     #$VM = Get-VM $VM<br />
     $VMNics = Get-VMGuestNetworkInterface $VM | where {$_.IP -ne $null -and $_.IPPolicy -eq "Static"}<br />
     foreach ($VMNic in $VMNics){<br />
         $VMNetAdapter = Get-NetworkAdapter -VM $VMNic.VM -Name $VMNic.NetworkAdapter<br />
          $obj = [PSCustomObject]@{<br />
                        VMName = $VM.Name<br />
                        NetworkName = $VMNetAdapter.NetworkName<br />
                        IP = $VMNic.Ip<br />
                        Subnet = $VMNic.SubnetMask<br />
                        DNS = [string]$VMNic.Dns<br />
                        GW = $VMNic.DefaultGateway<br />
                        }<br />
          $VMNetConf +=$obj<br />
     }<br />
}</p>
<p>$VMNetConf | Export-csv -Path d:\mat\vmniclist.txt -NoTypeInformation -NoClobber -UseCulture<br />

Here you can see the csv file after it has run in my test environment, one thing that I will extend with after discussing with some folks is the vlan number the port group the vm is connected to so that can be configured on the other side also.

Screen Shot 2013-08-15 at 23.30.25

And then I also with the possibility that there is in Hyper-V 2012, configure the network on the guest from the host, this requires that the virtual machine has the latest integration components installed. Ravikanth has done a blog post about how his function, I have added the possibility to run it from a remote computer

<br />
# Set-VMNetworkConfiguration<br />
# Orginial by Ravikanth http://www.ravichaganti.com/blog/?p=2766<br />
#<br />
# Added computername to remotly set IP to guest from hosts<br />
# Niklas Akerlund<br />
Function Set-VMNetworkConfiguration {<br />
    [CmdletBinding()]<br />
    Param (</p>
<p>        [Parameter(Mandatory=$true,<br />
                   Position=1,<br />
                   ParameterSetName='DHCP',<br />
                   ValueFromPipeline=$true)]<br />
        [Parameter(Mandatory=$true,<br />
                   Position=0,<br />
                   ParameterSetName='Static',<br />
                   ValueFromPipeline=$true)]<br />
        [Microsoft.HyperV.PowerShell.VMNetworkAdapter]$NetworkAdapter,</p>
<p>        [Parameter(Mandatory=$true,<br />
                   Position=2,<br />
                   ParameterSetName='DHCP',<br />
                   ValueFromPipeline=$true)]<br />
        [Parameter(Mandatory=$true,<br />
                   Position=5,<br />
                   ParameterSetName='Static',<br />
                   ValueFromPipelineByPropertyName=$true)]<br />
        [String[]]$ComputerName=@(),</p>
<p>        [Parameter(Mandatory=$true,<br />
                   Position=1,<br />
                   ParameterSetName='Static')]<br />
        [String[]]$IPAddress=@(),</p>
<p>        [Parameter(Mandatory=$false,<br />
                   Position=2,<br />
                   ParameterSetName='Static')]<br />
        [String[]]$Subnet=@(),</p>
<p>        [Parameter(Mandatory=$false,<br />
                   Position=3,<br />
                   ParameterSetName='Static')]<br />
        [String[]]$DefaultGateway = @(),</p>
<p>        [Parameter(Mandatory=$false,<br />
                   Position=4,<br />
                   ParameterSetName='Static')]<br />
        [String[]]$DNSServer = @(),</p>
<p>        [Parameter(Mandatory=$true,<br />
                   Position=0,<br />
                   ParameterSetName='DHCP')]<br />
        [Switch]$Dhcp</p>
<p>    )</p>
<p>    $VM = Get-WmiObject -Namespace 'root\virtualization\v2' -Class 'Msvm_ComputerSystem' -ComputerName $ComputerName | Where-Object { $_.ElementName -eq $NetworkAdapter.VMName }<br />
    $VMSettings = $vm.GetRelated('Msvm_VirtualSystemSettingData') | Where-Object { $_.VirtualSystemType -eq 'Microsoft:Hyper-V:System:Realized' }<br />
    $VMNetAdapters = $VMSettings.GetRelated('Msvm_SyntheticEthernetPortSettingData') </p>
<p>    $NetworkSettings = @()<br />
    foreach ($NetAdapter in $VMNetAdapters) {<br />
        if ($NetAdapter.Address -eq $NetworkAdapter.MacAddress) {<br />
            $NetworkSettings = $NetworkSettings + $NetAdapter.GetRelated("Msvm_GuestNetworkAdapterConfiguration")<br />
        }<br />
    }</p>
<p>    if ($Dhcp) {<br />
        $NetworkSettings[0].DHCPEnabled = $true<br />
    } else {<br />
        $NetworkSettings[0].DHCPEnabled = $false<br />
        $NetworkSettings[0].IPAddresses = $IPAddress<br />
        $NetworkSettings[0].Subnets = $Subnet<br />
        $NetworkSettings[0].DefaultGateways = $DefaultGateway<br />
        $NetworkSettings[0].DNSServers = $DNSServer<br />
        $NetworkSettings[0].ProtocolIFType = 4096<br />
    }</p>
<p>    $Service = Get-WmiObject -Class "Msvm_VirtualSystemManagementService" -Namespace "root\virtualization\v2" -ComputerName $ComputerName<br />
    $setIP = $Service.SetGuestNetworkAdapterConfiguration($VM, $NetworkSettings[0].GetText(1))</p>
<p>    if ($setip.ReturnValue -eq 4096) {<br />
        $job=[WMI]$setip.job </p>
<p>        while ($job.JobState -eq 3 -or $job.JobState -eq 4) {<br />
            start-sleep 1<br />
            $job=[WMI]$setip.job<br />
        }</p>
<p>        if ($job.JobState -eq 7) {<br />
            write-host "Success"<br />
        }<br />
        else {<br />
            $job.GetError()<br />
        }<br />
    } elseif($setip.ReturnValue -eq 0) {<br />
        Write-Host "Success"<br />
    }<br />
}<br />

And here is the script that I run to configure the VM´s after the MAT has done the conversions, and yes as Mark says about MAT, there is room for improvements here also of course, As you can see I start with injecting the latest integration components into the VM, the sleep cmdlet is used to get the vm fully booted and integration components installed and then an reboot inside the VM.

<br />
# Configure and set VM after Conversion<br />
#<br />
# Niklas AKerlund / 2013-07-01</p>
<p># Function import<br />
. .\Set-VMNetworkConfiguration.ps1</p>
<p>$VMNICs = Import-CSV -Path D:\mat\vmniclist.txt -Delimiter ";"<br />
$ConvertedVMs = Get-Content -Path D:\mat\VMlist.txt</p>
<p># Configure each vm before starting<br />
foreach ($ConvertedVM in $ConvertedVMs){<br />
    $VM = Get-VM $ConvertedVM.Split(".")[0]<br />
    write-host $VM.Name<br />
    #patch each VM with latest Integration Tools<br />
    $virtualHardDiskToUpdate =($VM | Get-VMHardDiskDrive).path<br />
    $integrationServicesCabPath ="C:\Windows\vmguest\support\amd64\Windows6.x-HyperVIntegrationServices-x64.cab"</p>
<p>    #Mount the VHD<br />
    $diskNo=(Mount-VHD -Path $virtualHardDiskToUpdate –Passthru).DiskNumber</p>
<p>    #Get the driver letter associated with the mounted VHD, note this assumes it only has one partition if there are more use the one with OS bits<br />
    $driveLetter=(Get-Disk $diskNo | Get-Partition | where Size -GT 100MB).DriveLetter</p>
<p>    #Check to see if the disk is online if it is not online it<br />
    if ((Get-Disk $diskNo).OperationalStatus -ne 'Online'){Set-Disk $MountedVHD.Number -IsOffline:$false -IsReadOnly:$false}</p>
<p>    #Install the patch<br />
    Add-WindowsPackage -PackagePath $integrationServicesCabPath -Path ($driveLetter + ":\")</p>
<p>    #Dismount the VHD<br />
    Dismount-VHD -Path $virtualHardDiskToUpdate</p>
<p>    Start-VM -VM $VM</p>
<p>    Start-Sleep -Seconds 300</p>
<p>    # Wait for the Integration components being installed and the server reboot (reboot requires interaction or automatic script inside VM or you will have to do an unclean poweroff</p>
<p>    Start-VM -VM $VM</p>
<p>    # check that the migrated VM actually has ic that responds<br />
    do {<br />
        $notok = Get-VMIntegrationService -VM $VM | Select -First 1 | where PrimaryStatusDescription -eq "OK"<br />
    } while( $notok -eq $null)</p>
<p>    # COnfigure NICs<br />
    foreach ($VMNic in $VMNICs){<br />
        write-host "Configuring"<br />
        if($VMNIC.VMName -eq $VM.Name){<br />
           if(!(Get-VMNetworkAdapter -VM $VM)){<br />
               $VMNetAdapter = Add-VMNetworkAdapter -VM $VM -SwitchName $VMNic.NetworkName -Passthru<br />
               $VMNetAdapter | Set-VMNetworkConfiguration -IPAddress $VMNic.IP -DefaultGateway $VMNic.GW -Subnet $VMNic.SubnetMask -DNSServer $VMNic.Dns -ComputerName HV03</p>
<p>           }else {</p>
<p>               Connect-VMNetworkAdapter -VMName $VM.Name -SwitchName $VMNic.NetworkName<br />
               $VMNetAdapter = Get-VMNetworkAdapter -VM $VM<br />
               write-host "connecting $VMNic.NetworkName "<br />
               $VMNetAdapter | Set-VMNetworkConfiguration -IPAddress $VMNic.IP -DefaultGateway $VMNic.GW -Subnet $VMNic.Subnet -DNSServer $VMNic.DNS -ComputerName HV03<br />
          }<br />
        }<br />
    }</p>
<p>}<br />

And after running as you can see in the Hyper-V manager I have the right IP on the VM (and if you check inside the VM, you will see that the dns,subnet and gateway also has been set correctly.

Screen Shot 2013-08-15 at 23.38.27

vExpert 2013

vexpert_logo_2013

This year I got the vExpert award again, this is the second year I get the title and feel honored to be part of the program 🙂

I have two former colleagues from Sweden and my time at Real Time Services that also got the title this year and they are serious mega-experts 🙂

Thank you John Troyer and all other involved in the vExpert program!

You can find the rest of the 580 vExperts on this link.

SC Orchestrator 2012 Sp1 and VMware vSphere 5.1

I have been exploring the System Center Orchestrator Integration Pack for vSphere and this is some good stuff! I have several customers that have bought Windows Datacenter Licenses with the System Center bundle and that is quite beneficial from a economical perspective! This means that they can use the System Center all parts with all VM´s and hosts. Several customers also use VMware vSphere as their virtualization platform and they can use System Center Orchestrator to build automation without any extra license cost.

In the System Center family there is only Virtual Machine Manager and Orchestrator that has connections to VMware vSphere without third party software (In Operations Manager you can use SNMP to get alarms from vCenter though).

So what can we do with Orchestrator IP for vSphere? This is the activities that is in the pack with version number 7.1.3010:

Screen Shot 2013-02-17 at 18.47.24

And what do we need to configure, well of course we need connection settings to a vCenter server and that is found under the “Options” and then you add your vCenter or if you have several and want to use them you will have to add one configuration for each.

Screen Shot 2013-02-17 at 19.12.05

And here is an simple example of a runbook that changes all VM´s memory to 384 MB, in the runbook I check if the VM is on or off and do an graceful power off activity depending on that status. For each activity in the runbook we have to add what configured vCenter we want to use or the runbook will fail!

Screen Shot 2013-02-17 at 19.19.42

The possibilites are almost endles as you can combine these activitiy with the others and interact with for example SCCM and deploy VM´s and OS. If this list of activities in the IP is not enough it can be extended by either creating your own IP or calling PowerCLI scripts from your runbook. Together with System Center Service Manager we can build nice self service portals that also does stuff on the VMware VM´s based on what the user order.

New version of the free vKernel Server Explorer

There is a new version of the free Dell vKernel Server Explorer and now the version number is 6.3

What has been added to this valuable tool:

  • Storage Explorer
  • Change Explorer

So with the storage explorer view I can get valuable information about the capacity and also the configuration of the datastores, such as what path selection, number of VM´s and IO etc

With the Change Explorer I get an overview of what has changed in my environment, this is data from the latest 7 days and if I want more I can always activate the vOPS server trial and then buy an license.

The Server Explorer can be installed/imported into VMware vSphere or Microsoft Hyper-V and also Redhat Enterprise Virtualization, and you can connect it to several vCenter servers and also Hyper-V and RHEV at the same time 🙂

I have tested to install it on Microsoft Hyper-V, I tried to use the guide and import the VM on a Windows 2008 R2 Hyper-V, this worked excellent. On windows 2008 r2 it is important that the config.xml file is present in the folder of the VM you try to import or it will fail, this is not a problem on windows 2012 hyper-v where you can import VM´s without exporting them first.

I changed the settings for the imported VM to

  • 4096 MB (I have also been able to start it with 2048 MB)
  • removed the legacy Network Adapter and replaced it with a Network Adapter (the synthetic)

Updated: The synthetic Network Adapter cannot be used when deploying on windows 2012 Hyper-V because the IC for linux on the vKernel Server Explorer appliance is version 3.1 and the OS is OpenSuse 11.4, and the IC 3.4 which is needed for 2012 cannot be installed on that OS. You can find out the version on your Linux VM by typing modinfo hv_vmbus or modinfo hv_netvsc for the network adapter. The recommendation from Microsoft is that you should use the Network Adapter when possible.

Screen Shot 2013-01-16 at 12.43.08

Then I started it and although the VM/VHD´s says 6.0 the VM after start says 6.3 So I know it is the latest and it starts successfully.

Screen Shot 2013-01-14 at 14.21.06

Next step is to configure the vKernel Hyper-V Collector, as you can see this is kind of mandatory before adding the System Center in the vOPS Server Explorer.

Screen Shot 2013-01-14 at 14.46.20

There are two different folders on the download and if you have System Center 2012 you should install the Hyper-V Collector 2012. If I had chosen the internal vOPS database I will use the SQL Server type PostgreSQL, the ip adress of the vOPS Server Explorer and the username and password postgres / postgres

Screen Shot 2013-01-14 at 16.05.27

Next step is to configure the connections tab where we add a connection to the SCOM server and when that is done we can after a while check the data in the web, the address is http://ip_of_the_vOPS , the name of my SCOM server is VMM12 so it is not the VMM server I connect to, although this server do have vmm and scom on the same OS.

Screen Shot 2013-01-14 at 21.57.44

Free tools are always nice and with these new change and storage explorers you get more insight into your environment and this for free!!

Good luck in your deployment of this updated tool!

Internet Explorer 6.0 in my Win8 Enterprise version

Yesterday I too the challenge from VMware Sweden to be the first presenting an image of an working Thinapp 4.7.3 packaged Internet Explorer 6.0 on a Windows 8.

This function within ThinApp is quite powerfull. Think of the enterprise applications that have an legacy webapp and that does not allow you to upgrade your web browser cause then it stops working. So how do you do when your users also are connected to the internet and the vulnerabilities out there when you do not have an updated web browser.

Here the ThinApped Internet Explorer comes in to play. You can use the ThinApp ThinDirect for those url containing the corporate web sites that require an older web browser and without the user needing to do anything they get the virtualized IE6 connecting to that site.

Here is a screendump on the configuration dialog for the Internet Explorer, and if I want the Internet Explorer 6 extracted from the OS ( in this case I am getting mine from a win 2003 Sp1)

And here you can see when I am running the IE6

I think that it is really easy to create ThinApped packages and also so powerful as you can see!

Installing vSphere vCenter 5.1 on Windows 2012

I have tested to install the new VMware ESXi 5.1 in a VM on my Macbook pro, next step was to get the vCenter 5.1 working on Windows 2012.

I downloaded the evaluation ISO from technet and when using the Easy Install I got an error trying to install the OS on the VM, as you can see on the screendump I get a licensing error, my first thought was that it was an error with my iso download and I then downloaded it again but the error persisted.

When I try to set up a new VM without the Easy Install I get the license agreement without error and could deploy the VM without any errors. Did I say that I am using the latest fusion 5.0.1

So to continue with the install, when the OS is upp and running I also need to download/install .net 3.5 (this is not by default available in win 2012 so I need the Media or an Internet connection). This as the .Net 3.5 is a requirement when installing vCenter.

Then I run the simple install from the media that will install the vCenter server, Single sign-on and Inventory service together.

When the installation is finished everything seems to be installed but the vCenter service will not start. It complained about a service that it depended on that was not existing.

I investigated a bit and the Protected Storage Service was deprecated in windows 2008 and only in read-mode and I could not find it for Windows 2012. So I wanted to remove the dependent services.

So how to do this, I type sc config vpxd depend= “” to remove all dependencies and then I typed sc start vpxd and the service took a bit to start but eventually it got to a running state.

Then I installed the vSphere Client and tried to log in and when the client opened I added the vSphere host without any issues.

Now when I got this running I will continue to install PowerCLI 5.1 and test it with PowerShell 3.0

 

 

New free tool vOPS Server Explorer 5.1 from vKernel

There is a new version of the vKernel Server Explorer that has a Environment Explorer view, this can handle multiple hypervisors (current support is Hyper-V, VMware vSphere and RHEV). The tool gives you as an administrator or consultant the insight in the environment and can give you answers if there are some issues and if you are running out of capacity.

The software is packed as an OVF and imported into your vSphere environment, as the vOPS server Readme text file describes there will also be an Hyper-V appliance that will be importable into an Hyper-V host and that with an collector for Hyper-V, do not know when that will be released though.

I have imported it with PowerCLI and then edited the vCPU to 2 instead of 4 and memory to 4 GB instead of 8 because my lab machines has limited resourses, It is really easy to deploy as you can see.

After import and configure I start it and then use a browser to the IP adress and configure, with this current version I can only add vSphere vCenter and RHEV

To add a vCenter I have to set the vCenter statistic level to at least 2 on all intervals

When that is done I successfully add the vCenter to the Server Explorer

With this you get three free tools in one, Environment Explorer, vScope Explorer and SearchMyVM Explorer that all can help you get the initial info you need. and If you want you can activate a free vOPS trial to extend the functionality.

In my small test/lab environment it looks like this

Download the Server Explorer and test yourself -> vOPS Server Explorer 5.1

Using PowerCLI to set CPU/Memory reservation on VM´s in vSphere

I have made a function to set reservations on VM´s in PowerCLI. The function takes a VM as parameter and also percentage of CPU and Memory you want to reserve.

The function checks the Mhz of the physical CPU from the Host the VM is running on and makes the reservation from this.

In the function I can set both memory and cpu reservation or just one of them

here is a screendump of the function being used

and here is the PowerCLI

<br />
function Set-VMReservation{<br />
<#
.SYNOPSIS
Set VM Reservation on Memory and/or CPU

.DESCRIPTION
Use this function set VM reservations 

.PARAMETER MemoryMB
Use this parameter to set the percent of memory reserved for the VM

.PARAMETER CpuMhz
Use this parameter to set the percent of CPU Mhz from the host CPU to be reserved

.NOTES
Author: Niklas Akerlund
Date: 2012-07-19
#><br />
    param (<br />
   [Parameter(Position=0,Mandatory=$true,HelpMessage="A VM",<br />
    ValueFromPipeline=$True)]<br />
    $VM,<br />
	$MemoryMB = 0,<br />
	$CpuMhz = 0<br />
    )<br />
	$VM = Get-VM $VM<br />
	if ($MemoryMB -ne 0 -and $CpuMhz -ne 0){<br />
		$MemoryMB = $MemoryMB/100<br />
		[int]$MemoryMB = $VM.MemoryMB*$MemoryMB<br />
		$CpuMhz = $CpuMhz/100<br />
		[int]$CpuMhz = ([math]::truncate(((Get-VMHost -id $VM.extensiondata.summary.runtime.host).ExtensionData.Hardware.CpuInfo.hz)/1000000))*$VM.NumCpu*$CpuMhz<br />
		$VM | Get-VMResourceConfiguration |Set-VMResourceConfiguration -CpuReservationMhz $CpuMhz -MemReservationMB $MemoryMB<br />
	}elseif ($MemoryMB -ne 0 -and $CpuMhz -eq 0){<br />
		$MemoryMB = $MemoryMB/100<br />
		[int]$MemoryMB = $VM.MemoryMB*$MemoryMB<br />
		$VM | Get-VMResourceConfiguration |Set-VMResourceConfiguration -MemReservationMB $MemoryMB<br />
	}elseif ($MemoryMB -eq 0 -and $CpuMhz -ne 0){<br />
		$CpuMhz = $CpuMhz/100<br />
		[int]$CpuMhz = ([math]::truncate(((Get-VMHost -id $VM.extensiondata.summary.runtime.host).ExtensionData.Hardware.CpuInfo.hz)/1000000))*$VM.NumCpu*$CpuMhz<br />
		$VM | Get-VMResourceConfiguration |Set-VMResourceConfiguration -CpuReservationMhz $CpuMhz<br />
	}elseif ($MemoryMB -eq 0 -and $CpuMhz -eq 0){<br />
		$VM | Get-VMResourceConfiguration |Set-VMResourceConfiguration -CpuReservationMhz 0 -MemReservationMB 0<br />
	}<br />
}<br />

In this example I have set the CPU reservation to 60 % and Memory to 40 %

vSphere Cluster Host-VM rule affinity with PowerCLI

A colleague wanted a script that could make a Host-VM affinity rule so that certain VM´s that resided on one storage at one site would run on the hosts on that site, the cluster spanns over two sites so the VM´s can run at both sites but one datastore is primary at the first site and the other at the second site.  So He wanted to get the VM´s that have their storage on datastoreLocation1 to be in an host-vm affinity rule “Should Run” on the hosts that is in that site. In this community post I found most of the code, I made som adjustments but mostly the cred goes to @lucd22 and GotMoo.

Here you can see the screendump from the cluster settings

Here is the powerCLI that has been run

One thing here was also that this VMgroup should be updated once a day if there was new VM´s that needed to be included. So this lines should be run in a scheduled task, this will make the DRSVMGroup VMLocationDC1 to be updated with the VM´s currently located at that datastore:

<br />
$VM = get-datastore delad02 | get-vm<br />
Update-DrsVMGroup -cluster vmw* -VMs $VM -groupVMName VMLocationDC1<br />

Here are the PowerCLI functions for doing this. As you can see I have an switch parameter for -MustRun, that will create the rule as a Must Run on these hosts.

<br />
function New-DRSGroupRule{<br />
<#
.SYNOPSIS
Create a new DRSGroupRule for VMs to reside on some hosts in a cluster

.DESCRIPTION
Use this function to create vms in a group and hosts in a group and a host-vm affinity

.PARAMETER  MustRun
A switch that will create the rule with Must Run on these host, if not set it will create the rule with should run.

.NOTES
Author: Niklas Akerlund / RTS (most of the code came from http://communities.vmware.com/message/1667279 @LucD22 and GotMoo)
Date: 2012-06-28
#><br />
	param (<br />
	[Parameter(Position=0,Mandatory=$true,HelpMessage="A Cluster",<br />
	ValueFromPipeline=$True)]<br />
	$cluster,<br />
	$VMHosts,<br />
	$VMs,<br />
	[string]$Name,<br />
	[switch]$MustRun<br />
	)</p>
<p>	$cluster = Get-Cluster $cluster</p>
<p>	$spec = New-Object VMware.Vim.ClusterConfigSpecEx<br />
	$groupVM = New-Object VMware.Vim.ClusterGroupSpec<br />
	$groupVM.operation = "add"<br />
	$groupVM.Info = New-Object VMware.Vim.ClusterVmGroup<br />
	$groupVM.Info.Name = "VM$Name"</p>
<p>	Get-VM $VMs | %{<br />
	$groupVM.Info.VM += $_.Extensiondata.MoRef<br />
	}<br />
	$spec.GroupSpec += $groupVM</p>
<p>	$groupESX = New-Object VMware.Vim.ClusterGroupSpec<br />
	$groupESX.operation = "add"<br />
	$groupESX.Info = New-Object VMware.Vim.ClusterHostGroup<br />
	$groupESX.Info.Name = "Host$Name"</p>
<p>	Get-VMHost $VMHosts | %{<br />
	$groupESX.Info.Host += $_.Extensiondata.MoRef<br />
	}<br />
	$spec.GroupSpec += $groupESX</p>
<p>	$rule = New-Object VMware.Vim.ClusterRuleSpec<br />
	$rule.operation = "add"<br />
	$rule.info = New-Object VMware.Vim.ClusterVmHostRuleInfo<br />
	$rule.info.enabled = $true<br />
	$rule.info.name = $Name<br />
	if($MustRun){<br />
		$rule.info.mandatory = $true<br />
	}else{<br />
		$rule.info.mandatory = $false<br />
	}<br />
	$rule.info.vmGroupName = "VM$Name"<br />
	$rule.info.affineHostGroupName = "Host$Name"<br />
	$spec.RulesSpec += $rule</p>
<p>	$cluster.ExtensionData.ReconfigureComputeResource($spec,$true)<br />
}</p>
<p>function Update-DrsVMGroup {<br />
<#
.SYNOPSIS
Update DRS VM group with a new collection of VM´s

.DESCRIPTION
Use this function to update the ClusterVMgroup with VMs that are sent in by parameters

.PARAMETER  xyz 

.NOTES
Author: Niklas Akerlund / RTS (most of the code came from http://communities.vmware.com/message/1667279 @LucD22 and GotMoo)
Date: 2012-06-28
#><br />
	param (<br />
	$cluster,<br />
	$VMs,<br />
	$groupVMName)</p>
<p>    $cluster = Get-Cluster $cluster<br />
    $spec = New-Object VMware.Vim.ClusterConfigSpecEx<br />
    $groupVM = New-Object VMware.Vim.ClusterGroupSpec<br />
    #Operation edit will replace the contents of the GroupVMName with the new contents seleced below.<br />
    $groupVM.operation = "edit" </p>
<p>    $groupVM.Info = New-Object VMware.Vim.ClusterVmGroup<br />
    $groupVM.Info.Name = $groupVMName </p>
<p>    Get-VM $VMs | %{<br />
        $groupVM.Info.VM += $_.Extensiondata.MoRef<br />
    }<br />
    $spec.GroupSpec += $groupVM</p>
<p>    #Apply the settings to the cluster<br />
    $cluster.ExtensionData.ReconfigureComputeResource($spec,$true)<br />
}<br />

When I get the time I might add some more functionality to also remove rules and groups.