VM affinity when using vCloud Director and vApps

I made a blog post some time ago, where i created a powerCLI function for creating a VM affinity rule that would keep my VM´s in a vApp together in the cluster for better VM-VM performance. I then checked for MoRef to get the VM correlated between vCloud Director and vCenter.

The answer to my problem was much closer than I thought, the vCloud director creates a folder in the “VM´s and Template view” for each vApp and guess what, all VM´s are in there.. so I do not need to ask the vCloud Director anymore (well if I want the Cloud vApp name I do need to ask it..) but ironically I had not looked there and found out that the folders actually where created for each deployed vCloud vApp :-/

So the code is a bit simpler and to get all VM´s from a folder I just type this, the asterisk is because the folder also gets a unique id

Get-Folder | where {$_.Name -like "ProdvCPod*"} | Get-VM

And then I just need to put those VM´s into an array and create a DRS rule, and here is the function that does this

<#
.Synopsis
   Add an affinity rule for a vCloud Director vAPP
.DESCRIPTION
   This function takes a vApp name (string) as parameter and creates an affinity rule for them to keep them together
.EXAMPLE
   Add-vAppAffinity -CIVApp ProdCust01
.NOTES
Author: Niklas Akerlund
Date: 2012-06-21
#>
function Add-vAppAffinity
{
    Param
    (
        # Parameter for the vAPP
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $CIVApp,

        # If the rule should apply on a different cluster
        $Cluster = "Cluster01"
    	)

	if ($CIVApp -ne " " -or $CIVApp -ne $null -or $CIVApp.length -ne 0 ){
		$Cluster = Get-Cluster $Cluster
		$VAppname = $CIVApp + "*"
		$VMs =  Get-Folder | where {$_.Name -like $VAppname} | Get-VM	
		
		if (!(Get-DrsRule -Cluster $Cluster -Name $CIVApp -ErrorAction SilentlyContinue)){ 
			New-DrsRule -Name $CIVApp -Cluster $Cluster -KeepTogether $true -VM $VMs
		}else{
			Remove-DrsRule -Rule (Get-DrsRule -Cluster $Cluster -Name $CIVApp) -Confirm:$false
			New-DrsRule -Name $CIVApp -Cluster $Cluster -KeepTogether $true -VM $VMs
		}
	}
}

To run it:

and it looks something like this if I check the cluster settings

preferably you have set your DRS cluster to fully automated or not so much will happen 🙂

Clone VM on Win 2012 Hyper-V v3 when it is running using Powershell

I wanted to create a function that could help an IT Pro Admin with the task to create a clone of a running VM, Yes you can use the Export-VM cmdlet but then your VM must be turned off and in some cases, for example when you want to test a new release or patch on a production system but first test it in a safe environment and you are not allowed to stop the original VM.

The function exists in System Center Virtual Machine Manager but there it also must be turned off

So how do I do? I have made a PowerShell function that take a snapshot, copies the vhd files and creates a new VM, connect the vhd´s and network cards, configures the VM with number of processors, dynamic memory etc.

As the Snapshot merges the vhdx files when the VM is running in this new Hyper-V version I thought it was an sufficient way to solve that the VM actually was not writing to the .vhdx files when I copy them but into the .avhdx diff file.

This is a version 0.1 and yes it need some development but right now I do not have the time to make it supernice,

Here you can see it in action

And when It is finished It looks like this in the gui

And here is the PowerShell function

<#
.Synopsis
   Function to clone a running VM
.DESCRIPTION
   This function can be used to clone a running vm and connect the copied vhd´s and network
.EXAMPLE
   Clone-VM -VMName vmdisktest -VMCloneName vmdisktest-clone -Path c:\VMs
.EXAMPLE
   Clone-VM -VMName vmdisktest -VMCloneName vmdisktest-clone -Path c:\VMs -Switch Private
.Notes
Author: Niklas Akerlund
Date: 2012-06-19
#>
function Clone-VM
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Name of the VM to be cloned
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $VMName,
        [string] $VMCloneName,
        # Where to store the VM-clone 
        [string] $Path,
        # Set to another network switch
        [string] $Switch = " "    
    )

    # Get VM
    $VM = Get-VM $VMName
    $VHDs = Get-VMHardDiskDrive -VM $VM
    #$VHDPath = (Get-VMHardDiskDrive -VM $VM).Path
    $VHDClonePath = "$Path\$VMCloneName\Virtual Hard Disks\"
    
    # Create a new array of VHD info
    $VHDOrg = @()
    foreach ($VHD in $VHDs){
        $data = New-Object PSObject -property @{
            VHDName = (Get-Item $VHD.Path).Name
            VHDPath = $VHD.Path
            ControllerType = $VHD.ControllerType
            ControllerNumber = $VHD.ControllerNumber
            ControllerLocation = $VHD.ControllerLocation

        }
        $VHDOrg +=$data
    }

    $VMNics = Get-VMNetworkAdapter -VM $VM

    # Take snapshot 
    $VM | Checkpoint-VM 

    New-Item -ItemType directory -Path $VHDClonePath

    New-VM -Name $VMCloneName -Path $Path -NoVHD -MemoryStartupBytes $VM.MemoryStartup -BootDevice IDE
    $VMClone = Get-VM -Name $VMCloneName
    Get-VMNetworkAdapter -VMName $VMCloneName | Remove-VMNetworkAdapter
   
    # Configure VM-Clon
    if ($VM.DynamicMemoryEnabled){
        Set-VM -VMName $VMCloneName -ProcessorCount $VM.ProcessorCount -DynamicMemory -MemoryMinimumBytes $VM.MemoryMinimum -MemoryMaximumBytes $VM.MemoryMaximum
    }else{
        Set-VM -VMName $VMCloneName -ProcessorCount $VM.ProcessorCount -StaticMemory
    }

    # Add all network cards
    foreach ($VMNic in $VMNics){
        if ($Switch -eq " "){
            Add-VMNetworkAdapter -VMName $VMCloneName -SwitchName $VMNic.SwitchName -IsLegacy $VMNic.IsLegacy
        }else{
            Add-VMNetworkAdapter -VMName $VMCloneName -SwitchName $Switch -IsLegacy $VMNic.IsLegacy
        }
    }

    # Copy all VHDs
    foreach ($VHDcopy in $VHDOrg){
        
        $Dest = $VHDClonePath + $VHDcopy.VHDName
        Copy-Item -Path $VHDcopy.VHDPath -Destination $Dest
        Add-VMHardDiskDrive -VMName $VMCloneName -ControllerType $VHDCopy.ControllerType -ControllerLocation $VHDCopy.ControllerLocation  -ControllerNumber $VHDCopy.ControllerNumber -Path $Dest   
    }

    # Remove snapshot 
    $VM | Remove-VMSnapshot

}

Good luck in testing, but do take in consideration that this is an copy of the running VM so do not start it at the same time on the same network or you will get IP collision etc, as you can see above I have added an -Switch parameter that you can use to set the network cards of the VM to be connected on another switch to avoid any problems.. 🙂

Using Powershell v3 scheduling for off hours Hyper-V VM maintenance/configuring

A colleague wanted a script for a reconfiguration of a VM´s settings. Once the VM is powered off that is not a big issue. The smart thing is when using Powershell version 3 I can configure a scheduled job that I want to execute off hours.

Imagine that you have the task to add a vCPU to a VM but you can only do it during the service window that happens to be around midnight, I don´t know about you but I rather sleep then if I can automate it. Yes I could in earlier versions use scheduled task but now I am using Win 2012 and Hyper-V 3 and the latest powershell.

So what do I need to do then, Jan Egil Ring has made a blog post about scheduled jobs and I want to show an example regarding Virtual Machine management..

In Powershell version 3 there are 16 cmdlets regarding scheduled jobs

First I need a Trigger

$once = New-JobTrigger -Once -At 11:59PM

Then I need a job with the trigger, I could use a parameter -FilePath to use a script file instead of -ScriptBlock

Register-ScheduledJob -ScriptBlock {$vm = Get-VM TestVM2 ; Stop-VM -VM $vm ; Set-VM -VM $vm -ProcessorCount 2 ; Start-VM -VM $vm} -Trigger $once

And then just wait or sleep 😛

And here you can see a screendump of the changing, Now I changed the trigger time to not have to wait to midnight for the blog post but I think you can imagine…

To check that the Job went ok, you run the cmdlet Get-Job ( and if you do not get any job you might need to run import-module PSScheduledJob to get the right Get-Job in that session)

God luck in scheduling your VM tasks 🙂

Lets remove some VM´s with PowerShell on Hyper-V 3 in Windows 2012

In the PowerCLI world there is a kind of evil oneliner to remove all VM´s from a datacenter, I was searching for something alike in the Hyper-V v3 world

The PowerCLI command is

Get-VM | %{Stop-VM $_ -Confirm:$false; Remove-VM $_ -DeletePermanently -Confirm:$false

And of course that is pure evil because the parameter -deletepermanently will remove not only the VM but also it´s files from the datastore, The % is to take care of each VM and in case the VM is running I will shut it down (otherwise if I only run like Get-VM | Stop-VM -Confirm:$false | Remove-VM -deletePermanently -Confirm:$false I will only remove the ones running, cause I will get an error on the others because I cannot change state to what it is already)

In Hyper-V and the powershell v3 It is not quite as easy but of course it can be done and don´t you dare use the gui 😉

This approach requires that you have put your VM in a separate folder for each of them (or of course it will be clean in your default Hyper-V VM folder..)

The Remove-VM cmdlet does not allow you to actually remove the VM´s virtual hard disk and the folders (which in some times can become a bit messy after a while)

Here is my VM´s

And here is my folder

So if I remove one VM now with the Remove-VM TestVM1, this also requires that the VM is off.

And lets see in the folders what do we have

So to clean both VM´s and folder data I run all at once and this looks like this

Get-VM testvm* | %{ Stop-VM -VM $_ -Force; Remove-VM -vm $_ -Force ; Remove-Item -Path $_.Path -Recurse -Force}

And see in my VM´s folder how tidy and neat it is 😛

And as I started to describe in the top .. If you just use Get-VM | …  you will clean your Hyper-V host quick 🙂

Using Powershell v3 Workflow with HyperV deployment

Now that the Windows 2012 RC has arrived with the Powershell v3 I wanted to explore the functionality a bit more, I have seen some posts about how to use the Workflows but none when deploying VM´s in Hyper-V.

I have loaned some code from Mikael Nyström (Deployment Bunny) but had to rewrite a little to make it work with the workflow -parallel.

Also when I downloaded Mikael´s scripts they where automatically  blocked ( I had set my demo system to -Unrestricted) but as you see in the screenshot they are still blocked. but luckily I can use the Unblock-File cmdlet in the Powershell v3, when using the v2 there was a utility tool streams.exe from SysInternals that could help.

and here is it in the properties dialog

But if I have like 5-10 or 100 files I would like to use Powershell (Anyone out there unblocking like 100 scriptfiles by clicking in a dialog?)

To create the “Master” vhdx I used the Convert-WindowsImage.ps1 script, I am creating the master to be deployed with win 2012 RC datacenter core version (Updated:I got a tip in the comments to look at the Convert-WindowsImage instead of the Wim2VHD.ps1)

Now to my workflow, I am testing to create 5 VM´s at the same time with differential disks connected to the master. the workflows also set all VM´s to dynamic memory and starts them.

# Inparallel.ps1
#
# Niklas Akerlund
# 2012-06-03

workflow create-VMs
{
    $VMRefDisk = "C:\VMs\master.vhdx"
    $VMNetwork = "Intern"
    $VMBaseLocation = "C:\VMs"
    $VMMemory = 384MB
    foreach -parallel ($item in 1..5) {
          
        $VMName = "TestVM$item"    
        $VMLocation = New-Item -Path "$VMBaseLocation\$VMName" -ItemType Directory -Force
        $VMDiskLocation = New-Item -Path "$VMLocation\Virtual Hard Disks" -ItemType Directory -Force
        $VMDisk01 = New-VHD –Path $VMDiskLocation\$VMName-OSDisk.vhdx -Differencing –ParentPath $VMRefDisk 
  
        New-VM –Name $VMname –MemoryStartupBytes $VMMemory –VHDPath $VMDisk01.path -SwitchName $VMNetwork -Path $VMBaseLocation
   
        Set-VM -VMName $VMName -DynamicMemory

        Start-VM -VMName $VMName
    }
    Get-VM TestVM*
}

It is quite simple but still powerfull, If I for example had like more hosts i can deploy many VM´s at the same time and the thing that would stop me is the hardware and the storage 😛

PowerShell to check Automatic Windows services after patch Tuesday

I did not get this idea all by my self, a customer I was at for a couple of weeks ago showed me a small script he had to check the services set to automatic after patching to see that they actually was running again.

I thought this could be somewhat more sophisticated done, so I created a script function that can handle single servers or an array of servers and in the script you can either start all services that are stopped or select just the ones you want, you can also in the function add services that you know are not important.

As you can see, I can get the servers from the Active Directory, In this example I have created a OU that contains the servers I want to check. I can also check for example in VMware with PowerCLI and getting an array of running VM´s. This can also be done on my Hyper-V environment with SCVMM. The different arrays I collect is just to get the server names and then I do an Get-wmiObject on the servers.

Here in this screen dump I check the computers in my AD, As you can see I select (N)o and that mean that I want all services that are not running and set to automatic to be started

In this next example I check the VM´s in my VMware vSphere environment

In this last example where i query a SCVMM server for the VM´s on the Hyper-V cluster I use the -AutoRestart parameter to start up all the services that the function finds.

And here is the powershell function, the tricky part here was to get the starting of services remote working and I had to use the -InputObject command as I did not get the pipeline of Get-Service -Computer xyz -Name Spooler | Start-Service to work.

function Check-Service{
<#
.SYNOPSIS
Check if Autostart Services has not started after patching

.DESCRIPTION
Use this function to check that all autostart services have started on the servers after patching

.PARAMETER  xyz 

.NOTES
Author: Niklas Akerlund / RTS
Date: 2012-05-28
#>
    param (
   [Parameter(Position=0,Mandatory=$true,HelpMessage="A server",
    ValueFromPipeline=$True)]
    $Servers,
	[switch]$AutoRestart
    )
	
	$report = @()
	$IgnoreServices = "ShellHWDetection","clr_optimization_v4.0.30319_32","clr_optimization_v4.0.30319_64","sppsvc"
	
	if ($Servers.GetType().Name -eq "String"){
		$Services = Get-WmiObject -ComputerName $Servers -Class win32_service -ErrorAction SilentlyContinue | where {$_.Startmode -EQ "Auto" -and $_.State -NE "Running"}
		if ($Services -ne $null) {
			foreach ($Service in $Services){
				if(!($IgnoreServices -contains $Service.Name)){
					$data = New-Object PSObject -property @{
						Server = $Servers
						Name = $Service.Name
						StartMode = $Service.StartMode
						State = $Service.State
					}
				
				$report +=$data
				}
			}
		}
	}else{
		foreach ($Server in $Servers){	
			$Services = Get-WmiObject -ComputerName $Server.Name -Class win32_service -ErrorAction SilentlyContinue | where {$_.Startmode -EQ "Auto" -and $_.State -NE "Running"}
			if ($Services -ne $null) {
				foreach ($Service in $Services){
					if(!($IgnoreServices -contains $Service.Name)){
						$data = New-Object PSObject -property @{
							Server = $Server.Name
							Name = $Service.Name
							StartMode = $Service.StartMode
							State = $Service.State
						}
					
					$report +=$data
					}
				}	
			} else {
				Write-Host "Could not query server: $Server"
			}
		}
	}
	
	$report 
	if($AutoRestart){
			$report |%{ Start-Service -InputObject (Get-Service -ComputerName $_.Server -Name $_.Name)}
	}else{
		if($report -ne $null){
			Write-Host "Do you want to select which services to start? (Y) (N) (Q):"
			$select = Read-Host
			if ($select -eq "Y"){
				foreach ($item in $report){
					Write-Host "Start service " $item.Name " on server" $item.Server " (Y) (N)"
					$restart = Read-Host
					if ($restart -eq "Y"){
						Start-Service -InputObject (Get-Service -ComputerName $item.Server -Name $item.Name)
					}
				}
			}elseif($select -eq "N"){
				$report |%{ Start-Service -InputObject (Get-Service -ComputerName $_.Server -Name $_.Name)}
			}
		}
	}
}

Good luck in finding services that should be running in your environment 🙂
 

VMware vSwitch ports massage with PowerCLI

This week I have been teaching my first vSphere 5 ICM, I have been trying to show the students the wonderful world of PowerCLI and all good you can do with it.

When we got to the virtual networking I tested if I could add a switch with any number of ports

get-vmhost esxi01* | New-VirtualSwitch -Name TestNumPorts -NumPorts 12 -Nic vmnic0

When I try to add 12 ports with the -NumPorts parameter, powerCLI tells me that my switch has been created with 16 ports, but look here in my vSphere Client screen dump

hmm smells like a bug? or ?

Well now I wanted to test if I could add more than 4 VM´s and connect them and then start them, first I have to create a Portgroup that i can connect and then a nice for loop to create 10 VM´s

get-vmhost esxi01* | Get-VirtualSwitch -Name TestNumPorts | New-VirtualPortGroup -Name Test

for ($i=1;$i -le 10;$i++){New-VM -VMHost (Get-VMHost esxi01*) -Name ("VM01" + $i) -Datastore (Get-Datastore delad02) -NetworkName Test}

The last  oneliner creates a standard VM with 256 MB ram and a 4 GB vmdk disk, and then I want to start them to actually connect them to the network I created to see if anyone of them does not get connected.

So to get them connected I start them

And check the last command where we can see that my number of available ports has not decremented?

Here you can see in the vSphere client that all my new VM´s are connected to the Test portgroup

In this screenshot you can see why my vSwitch got 16 ports (as 8 are allocated by the vmkernel) and the options are 8/24/56/120/248/504/1016 and so on, but still it does not explan why the powercli does not show that i have used 10 of the ports, or?

To see maybe what causing this I changed the number of ports to one of the “allowed” and when I changed the number to 16 ports, directly my free ports changed to 4, but I have 10 machines + 8 reserved?

The following powerCLI command shows the VM´s and if they are connected if you do not believe the gui 😉

Get-VM VM01* | Get-NetworkAdapter | select parent, connectionstate

 

The conclusion is that I have no answer if this is as it should be or if it is a bug, maybe someone out there could clarify 🙂

Now time to prepare for tomorrow and the modules in the course 🙂

Shrink HyperV virtual hard disk Win8 / Win2012 with PowerShell

I read this post by Ben (Virtual PC Guy) Armstrong about shrinking a virtual hard disk and thought this should be done with powershell.

So first I have to shrink the partition inside the VM, for this I use the New-CIMSession (instead of powershell remoting and such) and then I shut down the VM and shrink the VHDx file, As someone correctly noted, this shrink of the virtual disk file is only possible when using the new VHDx format.

The VM is part of the same domain as the hyper-V server from which I am running the script, this made it easier to make a cim connection. The VM is also a Win 8 beta cause I need the latest win framework with powershell 3.

As you can see, the disk in my VM is 60 GB and I want to shrink it to the minimum size * 20% (so i get at least some space left 😉

Now I will run my little script and here is what that looks like

# vNiklas Masterblaster shrink
#
# Niklas Akerlund / 2012-05-13

$VM = "Shrink8"

$cim = New-CimSession $VM

$partitionC = Get-Partition -CimSession $cim -DriveLetter C

$newSize = ($partitionC | Get-PartitionSupportedSize).SizeMin*1.20

Resize-Partition -PartitionNumber $partitionC.PartitionNumber -DiskNumber $partitionC.DiskNumber -Size $newSize -CimSession $cim

Get-Volume C -CimSession $cim

$VM = Get-VM $VM

Stop-VM $VM

$hdd = $VM | Get-VMHardDiskDrive

Resize-VHD -Path $hdd.Path -ToMinimumSize

Start-VM $VM

And here you can see the result, a quite simple solution..

Good luck in your shrinking 🙂

Update:  I totally missed that Ben had done a post about doing it with powershell, just after his other post when i was making this post… At least my solution show how to connect to a VM with CIM 🙂

 

PowerCLI report on datastores, overprovision and number of powered on VM´s

Today I had an reason for running PowerCLI again, the case was to get an quick report on the datastores at a customer, I have made a post about the one-liner that get the number of running VM´s on a datastore. As i described in that post, if your SAN does not support VAAI then you do not want to many VM´s on each datastore because of the SCSI-locking that can occur. This is just an extension of that because we also wanted to check how overprovisioned the datastores was (when using thin provisioning there is a risk that you will fill your datastores as the VM´s fill their vmdk disk´s)

The PowerCLI code looks like this

Get-Datastore | Select Name,@{N="TotalSpaceGB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity)/1GB,0)}},@{N="UsedSpaceGB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace)/1GB,0)}}, @{N="ProvisionedSpaceGB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/1GB,0)}},@{N="NumVM";E={@($_ | Get-VM | where {$_.PowerState -eq "PoweredOn"}).Count}} | Sort Name | Export-Csv -Path C:\temp\vmds-datastore.csv -NoTypeInformation -UseCulture

And this is when i have run it on my test system, the difference here is that i removed the Export-CSV to get the output in the console

And here is a simple excel report

 

VMware vCloud Director vApp and DRS cluster affinity with PowerCLI

To fully utilize the performance in a vApp in vCloud Director I got the task to create an affinity rule based on the VMs in the vApp. This can be the case for example when you have VMs in an vApp that exchange high loads of data. In our case we have virtual ESXi Servers that have a vsa for shared storage and need good performance when deploying VM´s and storage vMotion etc.

When deploying several vApps from the same template it is not just to run Get-CIVM and then use the VM name returned to run Get-VM for the correlation between the VM in VCD and in vCenter, as you can see in the screendumps these two have the same name but followed with an identifier from vCD in the vSphere Client. This is also described in the vSphere blog.

The fine part is that the MoRef is unique (in the relation one vcd <-> one vcenter) so I can check on that which VMs in the vCenter belongs to the same vCloud Director vApp.

I got the code for the MoRef in this community post.

And here is my script

<#
.Synopsis
   Add an affinity rule for a vCloud Director vAPP
.DESCRIPTION
   This function takes a vApp as parameter and creates an affinity rule for them to keep them together
.EXAMPLE
   Add-CIVAppAffinity -CIVApp Green01
.NOTES
Author: Niklas Akerlund / RTS
Date: 2012-05-03
#>
function Add-CIVAppAffinity
{
    Param
    (
        # Parameter for the vAPP
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $CIVApp,

        # If the rule should apply on a different cluster
        $Cluster = "vCD-Cluster"
    )

	$pod = Get-CIVapp $CIVApp
	if ($pod){
		$PodVMs = Get-CIVM -VApp $pod
		$VMs = @()
		$Cluster = Get-Cluster $Cluster
		Foreach ($PodVM in $PodVMs) { 
			$VMname = $PodVM.Name + "*"
			$VM =  Get-VM $VMname | where {$_.ExtensionData.MoRef.Value -eq $PodVM.ExtensionData.VCloudExtension[0].Any[0].VmVimObjectRef.MoRef}
			$VMs +=$VM
		}

		if (!(Get-DrsRule -Cluster $Cluster -Name $pod.Name -ErrorAction SilentlyContinue)){ 
			New-DrsRule -Name $pod.Name -Cluster $Cluster -KeepTogether $true -VM $VMs
		}else{
			Remove-DrsRule -Rule (Get-DrsRule -Cluster $Cluster -Name $pod.Name) -Confirm:$false
			New-DrsRule -Name $pod.Name -Cluster $Cluster -KeepTogether $true -VM $VMs
		}
	}
}

And when checking the cluster i can see that my DRS affinity rule has been created and the VMs have been migrated to the same host

It is not so extensive but it helps us with the case to create DRS affinity on the VMs in a particular vApp 🙂 , The Rule is deleted when the vApp is removed, I have an extra check in the script and remove it if it still exist of some reason.