A PowerShell function for Snapshot reporting in VMM2012

Hello

I am in the hotel waiting for the MMS 2012 to start so there is time for some automation ;-). Last week when I was teaching a course one of the attendes said after I showed the custom value function, that he also wanted a similar function for easy in the console check how many snapshots every VM had. In the VMM 2012 console I have not found an built-in function to actually show this.

I have modified the function and created the run script, this is scheduled to run once a hour so you have a updated view of your environment. At least in the Hyper-V for Windows 2008 R2 it is not recommended to use in production.

It is quite easy to create a new custom property that we then will populate,

New-SCCustomProperty -Name "Snapshot Count" -Description "" -AddMember @("VM")

In the GUI there is no way to remove a custom property, I can only remove it from assigned properties. During the course I created an property just for showing and want to get rid of it. Of course there is a powershell cmdlet for that:

Get-SCCustomProperty -Name "Muuuu" | Remove-SCCustomProperty

But now to the part of getting the script to show snapshots (yes I know, in VMM it is called Checkpoints but i refuse to call it that)

First I have the PowerShell function

function Set-CVSnapShot{
<#
.SYNOPSIS
Function to Update custom values for VM´s in VMM 2012
 
.DESCRIPTION
Use this function to  set the custom value Snapshot count
 
.PARAMETER  VMs
Use this parameter to update just that VM´s custom snapshot value
 
.NOTES
Author: Niklas Akerlund / RTS
Date: 2012-04-16
#>
    param (
   [Parameter(Position=0,HelpMessage="A virtual machine pleaze",
    ValueFromPipeline=$True)]
    $VMs = " "
    )
   
  	if ($VMs -eq " "){
    	$VMs = Get-SCVirtualMachine
	}else{
		$VMs = Get-SCVirtualMachine  $VMs
	}
	
  	$CustomSnapShot = Get-SCCustomProperty -Name "Snapshot Count"
   
	foreach ($VM in $VMs){
		$count = ($VM | Get-SCVMCheckpoint).Count
		
	    if($count -ne $null){
	        Set-SCCustomPropertyValue -InputObject $VM -CustomProperty $CustomSnapShot -Value $count 
	    }else{
	        Set-SCCustomPropertyValue -InputObject $VM -CustomProperty $CustomSnapShot -Value 0 
	    }
   }
}

then I have a very easy script that I task scheduled in my VMM server

# Script to use for scheduled updates of Custom values
#
# Niklas Akerlund / RTS 2012-04-16

ipmo 'C:\Program Files\Microsoft System Center 2012\Virtual Machine Manager\bin\psModules\VirtualMachineManager' | Out-Null

. .\Set-CVSnapshot.ps1
Set-CVSnapshot

And in the VMM Console it looks like this

I will talk to some VMM responsibles at MMS and see if they find an interest and build the view instead of me running it in a sheduled task (as the information is there in the database)

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 :

Get-VM *HOU-DPM-E* | Get-VMNetworkAdapter | Connect-VMNetworkAdapter -SwitchName Internal

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.

# Import VMs for Course
#
# Niklas Akerlund / RTS 2012-04-04

$VMs = Get-Item -Path "E:\Program Files\Microsoft Learning\50273\Drives\*" | Select-Object Name,FullName

foreach ($VM in $VMs){
    
    if(Get-VM $VM.Name -ErrorAction SilentlyContinue ){
        Write-Host $VM.Name "Already imported"
    }else{
        # Create Symlinks
        $symbat = (Get-Item ($VM.FullName + "\*.bat")).FullName
        & $symbat

        $expfile = (Get-Item ($VM.FullName + "\Virtual Machines\*.exp")).FullName
        
        Import-VM -Path $expfile 

        Get-VM REA* | Rename-VM -NewName $VM.Name
        Get-VM $VM.Name | Get-VMNetworkAdapter | Connect-VMNetworkAdapter -SwitchName Internal
    }
}

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:

A PowerCLI function to manage VMware vSphere Licenses

As always ( or at least in my world) you should do everything in powershell/powerCLI. Now the turn comes to handling licensing in VMware vSphere.

I searched the web and found some information from Luc Dekens post and from Joe Keegan post. I also used the Onyx VMware fling to get some more functionality.

I decided to do some powershell functions and came up with the following, I have created three functions that can do some magic with your licenses

  • Get-vLicense  – query the vCenter for licenses
  • Add-vLicense – add a license or assign an existing license to a host/vCenter
  • Remove-vLicense – remove a license that is not in use

Of course there might be other things regarding licensing i have forgot but see this as a version 0.1 and that you can give me input on what to add or change

Here you can see how the function get the information:

You have to bare with me and my paintbrush skills, but I cannot show my license keys on the Internet 🙂

I can also use a switch to just show the ones that are not used with

Get-vLicense -showUnused

I would like to move my hosts to the new license that i have that has unlimited cores

I can also in the Add function just add without assigning the license right now just use the -AddKey switch

And if I want to remove the unused license I  use the following function, It can utilize a pipeline parameter as you see

 

And here is the whole code for the functions:

function Get-vLicense{
<#
.SYNOPSIS
Function to show all licenses  in vCenter
 
.DESCRIPTION
Use this function to get all licenses in vcenter
 
.PARAMETER  xyz 
 
.NOTES
Author: Niklas Akerlund / RTS
Date: 2012-03-28
#>
	param (
		[Parameter(ValueFromPipeline=$True, HelpMessage="Enter the license key or object")]$LicenseKey = $null,
		[Switch]$showUnused,
		[Switch]$showEval
		)
	$servInst = Get-View ServiceInstance
	$licenceMgr = Get-View $servInst.Content.licenseManager
	if ($showUnused -and $showEval){
		$licenses = $licenceMgr.Licenses | where {$_.EditionKey -eq "eval" -or $_.Used -eq 0}
	}elseif($showUnused){
		$licenses = $licenceMgr.Licenses | where {$_.EditionKey -ne "eval" -and $_.Used -eq 0}
	}elseif($showEval){
		$licenses = $licenceMgr.Licenses | where {$_.EditionKey -eq "eval"}
	}elseif ($LicenseKey -ne $null) {
		if (($LicenseKey.GetType()).Name -eq "String"){
			$licenses = $licenceMgr.Licenses | where {$_.LicenseKey -eq $LicenseKey}
		}else {
			$licenses = $licenceMgr.Licenses | where {$_.LicenseKey -eq $LicenseKey.LicenseKey}
		}
	}
	else {
		$licenses = $licenceMgr.Licenses | where {$_.EditionKey -ne "eval"}
	}
	
	$licenses
}

function Add-vLicense{
<#
.SYNOPSIS
Add New Licenses to the vCenter license manager
 
.DESCRIPTION
Use this function to add licenses  and assing to either the vcenter or the hosts
 
.PARAMETER  xyz 
 	
.NOTES
Author: Niklas Akerlund / RTS
Date: 2012-03-28
#>
param (
	$VMHost ,
	[Parameter(ValueFromPipeline=$True)]$License = $null,
	[string]$LicenseKey = "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX",
	[switch]$AddKey
    )
	$LicenseMgr = Get-View -Id 'LicenseManager-LicenseManager'
	$LicenseAssignMgr = Get-View -Id 'LicenseAssignmentManager-LicenseAssignmentManager'
	if($License){
		$LicenseKey = $License.LicenseKey
		$LicenseType = $LicenseMgr.DecodeLicense($LicenseKey)
	}else{
		$LicenseType = $LicenseMgr.DecodeLicense($LicenseKey)
	}
	
	if ($LicenseType) {
		if ($AddKey){
			$LicenseMgr.AddLicense($LicenseKey, $null)
		}else{
			if ($LicenseType.EditionKey -eq "vc"){
				#$servInst = Get-View ServiceInstance
				$Uuid = (Get-View ServiceInstance).Content.About.InstanceUuid
				$licenseAssignMgr.UpdateAssignedLicense($Uuid, $LicenseKey,$null)
			} else {
				$key = Get-vLicense -LicenseKey $LicenseKey
				if($key  -and ($key.Total-$key.Used) -lt (get-vmhost $VMHost | get-view).Hardware.CpuInfo.NumCpuPackages){
					Write-Host "Not Enough licenses left"
				} else{
					$Uuid = (Get-VMhost $VMHost | Get-View).MoRef.Value
					$licenseAssignMgr.UpdateAssignedLicense($Uuid, $LicenseKey,$null)
				}
			}	
		}
	}	
}


function Remove-vLicense{
<#
.SYNOPSIS
Function to remove a licenses that is not in use in vCenter
 
.DESCRIPTION
Use this function to remove a license
 
.PARAMETER  xyz 
 
.NOTES
Author: Niklas Akerlund / RTS
Date: 2012-03-28
#>
param (
	[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$True, HelpMessage="Enter the key or keyobject to remove")]$License
	)
	$LicObj = Get-vLicense $License 
	if($LicObj.Used -eq 0){
		$LicenseMgr = Get-View -Id 'LicenseManager-LicenseManager'
		$LicenseMgr.RemoveLicense($LicObj.LicenseKey)
	}else{
		Write-Host " The license is assigned and cannot be removed"
	}
}

Update vSphere 5 hosts to U1 with Update manager and PowerCLI

Today I wanted to update my lab environment hosts to U1 of vSphere 5, of course with PowerCLI, there is no other way or is it?

This reference indicates there is not so many cmdlets but is it enough? Yes it is!

As the PowerCLI installation does not include the VUM cmdlets i had to download and install them

I have already upgraded my vCenter and VUM to U1, this as it is always a best practice to first update the vCenter and then the hosts

the following script do the whole process of both making a Patch baseline and attaching it to the cluster and then Scan/Remidate (now i only have two hosts but this is quite powerfull if i have lots of more). The script allows some parameters, for example if you already have a baseline you can use that and attach/remediate your cluster, or you want another patch in the baseline.

<#
 .SYNOPSIS
 Upgrade ESXi Hosts to an Update Version 

 .DESCRIPTION
 Create an baseline and upgrade a Cluster to an vSphere 5 update with Update Manager, If you already have a baseline just use the BaseName parameter

 .Author
 Niklas Akerlund /RTS 2012-03-23 (original script from Jonathan Medd)

 .PARAMETER  Cluster
 The name of the host to upgrade.

 .PARAMETER  PatchName
 Enter the name of the Patch/Update
 
 .PARAMETER Name
 Enter the name of the vCenter server to connect to

 .EXAMPLE
 PS C:\> Update-HostsU1.ps1 -Cluster VMWCL
#>
#requires -pssnapin VMware.VimAutomation.Core
#requires -pssnapin VMware.VumAutomation

[CmdletBinding()]
param(
 [Parameter(Position=0, Mandatory=$true, HelpMessage="Enter the name of Cluster")]
 $Cluster,
 [Parameter(HelpMessage="Enter the name of the patch")]
 [String]
 $PatchName = "VMware ESXi 5.0 Complete Update 1",
 [Parameter(HelpMessage="Enter the baseline name")]
 [String]
 $BaseName = "vSphere ESXi 5 U1"
 )

process{

	# Create a baseline to use
	if(!(Get-Baseline -Name $BaseName -EA 0)){
		# Get the update and add a Patchbaseline
		$Patch = get-patch | where {$_.Name -eq $PatchName}
		New-PatchBaseline -Name $BaseName -Static -IncludePatch $Patch
	}
	# Attach the baseline on the cluster
	$Cluster = Get-Cluster $Cluster
	$Baseline = Get-Baseline -Name $BaseName
	if($Baseline){
		$Baseline | Attach-Baseline -Entity $Cluster
		
		# Scan the Cluster 
		$Cluster | Scan-Inventory
		
		# Remediate the cluster with the baseline
		$Cluster | Remediate-Inventory -Baseline $Baseline -ClusterDisableDistributedPowerManagement:$true -ClusterDisableFaultTolerance:$true -ClusterDisableHighAvailability:$true -Confirm:$false -HostDisableMediaDevices:$true
		
	}
}

And here are some screenshots of it running,

One host successfully updated and the remediate migrates the VM´s and start with the other host.

Finally the hosts in the cluster are updated.

I have used some of the code from Jonathan Medd´s post on upgrading from 4.0->4.1 to create my script.

 

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:

SQLCOLLATION=”SQL_Latin1_General_CP1_CI_AS”

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.

# Install VMM unattended
#
# NIklas Akerlund /RTS

# Install the SQL server
c:\install\sql2008r2\setup /ConfigurationFile=c:\install\ConfigurationFile.ini

# Install the WAIK
msiexec /qb /i C:\install\KB3AIK_EN\wAIKAMD64.msi TRANSFORMS=c:\install\waikamd64.mst

Start-Sleep -s 60
# Install the VMM server and console

C:\install\vmm12\setup.exe /server /i
Start-Sleep -s 30
while (Get-Process | where {$_.ProcessName -eq "SetupVM"}){
Write-Host "Still installing server"
Start-Sleep -s 2
}
Start-Sleep -s 10
C:\install\vmm12\setup.exe /client /i
Start-Sleep -s 30
$Process = Get-Process | where {$_.ProcessName -eq "SetupVM"}
while (Get-Process | where {$_.ProcessName -eq "SetupVM"}){
Write-Host "Still installing Console"
Start-Sleep -s 2
}

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

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,

Get-WindowsFeature WindowsPowerShellWebAccess | Add-WindowsFeature
Install-PswaWebApplication -UseTestCertificate
Add-PswaAuthorizationRule * * *

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!

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.

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

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,

 

Migrate VM´s in SCVMM 2012 from one cluster to a new

I have made a simple powershell function that can move VM´s from one cluster to another and also set the network configuration, this because the old cluster had one way of setup regarding the virtual machine network and the new another more flexible with promiscuous mode set on the HP nic team so i do not need to create one virtual switch for every vlan. The function also checks if the actual VM has snapshots before moving it. (the SCVMM calls it checkpoints, which is stupid to call it one thing in hyper-v and another in SCVMM!)

the powershell function looks like this:

function Migrate-VM {

param (
   [Parameter(Position=0,Mandatory=$true,HelpMessage="A virtual machine pleaze",
    ValueFromPipeline=$True)]
    $VM,
    [Parameter(Mandatory=$true)]$VMHost,
    $VMnic,
    $VMnetwork = "VMs",
    $Datastore = "C:\ClusterStorage\Volume1",
    [switch]$StartVM
    )
    
    if( Get-SCVirtualMachine $VM | Get-SCVMCheckpoint) {
		$VM = Get-SCVirtualMachine $VM
		Write-Host " Please remove snapshots and do a shutdown of the VM:" + $VM.Name
    }else{
	    $JobGroupID = [guid]::NewGuid()
	    
	    $VMHost = Get-SCVMHost $VMHost
	    $VMnic = Get-SCVirtualMachine $VM | Get-SCVirtualNetworkAdapter
	    
	    $LogicalNetwork = (Get-SCLogicalNetworkDefinition -VLanID $VMnic.VlanID).LogicalNetwork

	    Set-SCVirtualNetworkAdapter -VirtualNetworkAdapter $VMnic -RunAsynchronously -VirtualNetwork $VMnetwork -LogicalNetwork $LogicalNetwork -VLanEnabled $true -VLanID $VMnic.VlanID -JobGroup $JobGroupID -EnableVMNetworkOptimization $false -EnableMACAddressSpoofing $false
	    if($StartVM){
	        Move-SCVirtualMachine -VM $VM -VMHost $VMHost -HighlyAvailable $true -Path $Datastore -StartVMOnTarget -RunAsynchronously -UseLAN -JobGroup $JobGroupID
	    }else
	    {
	        Move-SCVirtualMachine -VM $VM -VMHost $VMHost -HighlyAvailable $true -Path $Datastore -RunAsynchronously -UseLAN -JobGroup $JobGroupID
	    }
    }   
    
}

And this screendump shows some different ways of running it,

VM inventory function with Powershell on SCVMM 2012

Had a plan to do a blog post last week but was so busy studying for the VCP 5 test. I wrote the test on friday and i passed 🙂

I have created a little powershell function, this to get the inventory information about the virtual machines running in hosts or clusters that are connected to a System Center Virtual Machine Manager. This function collects number of configured vCPU´s, Memory, information about the connected virtual harddisks. This kind of information could be handy as in the case with one customer that has not yet implemented a host based backup solution and still use agents in their VM´s, so if they need to recover machines it is good to know what size and configuration it had if it is lost by some reason. This report can also be handy for some info on how much your datastores are filled. As you can se i both report the maximum size and the current (as dynamic disks are not allocating the whole size at creation time)

The function both export a csv and a HTML report

After importing it to excel it can look something like this:

And the html output is very simple but can be put on a internal web for IT production

I have used the Get-SCVirtualDisk to get the Bus Type and LUN id as this information is not available in Get-SCVirtualHarddisk, this information can be good if i lost the virtual machine configuration and want to connect the virtual harddisks to the right controller.

the function looks like this: (if you copy this you have to move the first # up to the <, the blog syntax highlighter does something and move it down :-( )

function Get-VMdata{
<#
.SYNOPSIS
Get the configuration data of the VMs in Hyper-V via SCVMM 2012
 
.DESCRIPTION
Use this function to get all VMs configuration in case of disaster or just statistics
 
.PARAMETER  xyz 
 
.NOTES
Author: Niklas Akerlund / RTS
Date: 2012-02-13
#>
param (
	$VMHostGroup = "All Hosts",
	[Parameter(ValueFromPipeline=$True)][Alias('ClusterName')]
	$VMHostCluster = $null,
	$VMHost = $null,
	[string]$CSVFile = "VMdata.csv",
    [string]$HTMLReport = "VMdata.html"
    )
	
	$report = @()
	if ($VMHostCluster -eq $null){
		$VMHosts = (Get-SCVMHostGroup -Name $VMhostGroup).AllChildHosts
	}else{
			$VMHosts = (Get-SCVMHostCluster -Name $VMHostCluster).Nodes
	}
	$VMs = $VMHosts | Get-SCVirtualMachine
	
	foreach ($VM in $VMs) {
		$VHDs = $VM | Get-SCVirtualDiskDrive
		$i = "1"
		foreach ($VHDconf in $VHDs){ 
			if($i -eq "1"){
				$data = New-Object PSObject -property @{
					VMName=$VM.Name
			        vCPUs=$VM.CPUCount
			        MemoryGB= $VM.Memory/1024
					VHDName = $VHDconf.VirtualHardDisk.Name
					VHDSize = $VHDconf.VirtualHardDisk.MaximumSize/1GB
					VHDCurrentSize = [Math]::Round($VHDconf.VirtualHardDisk.Size/1GB)
					VHDType = $VHDconf.VirtualHardDisk.VHDType
					VHDBusType = $VHDconf.BusType
					VHDBus = $VHDconf.Bus
					VHDLUN = $VHDconf.Lun
					VHDDatastore = $VHDconf.VirtualHardDisk.HostVolume
				}
				$i= "2"
			}else{
				$data = New-Object PSObject -property @{
					VMName=""
			        vCPUs=""
			        MemoryGB= ""
					VHDName = $VHDconf.VirtualHardDisk.Name
					VHDSize = $VHDconf.VirtualHardDisk.MaximumSize/1GB
					VHDCurrentSize = [Math]::Round($VHDconf.VirtualHardDisk.Size/1GB)
					VHDType = $VHDconf.VirtualHardDisk.VHDType
					VHDBusType = $VHDconf.BusType
					VHDBus = $VHDconf.Bus
					VHDLUN = $VHDconf.Lun
					VHDDatastore = $VHDconf.VirtualHardDisk.HostVolume
				}
			}
			$report +=$data	
		}
	}
	$report | Select-Object VMName,vCPUs,MemoryGB,VHDName,VHDSize,VHDCurrentSize,VHDType,VHDBusType,VHDBus,VHDLUN,VHDDatastore | Export-Csv -Path $CSVFile -NoTypeInformation -UseCulture
	$report | Select-Object VMName,vCPUs,MemoryGB,VHDName,VHDSize,VHDCurrentSize,VHDType,VHDBusType,VHDBus,VHDLUN,VHDDatastore | ConvertTo-HTML | Out-File $HTMLReport
}

and i can run it in some different ways, as you see in this screenshot, if i do not give any parameters it will use VMHostGroup “All Hosts” as default

Updated powerCLI function for setting HA Admission control policy in percent with number of hosts

I got the question from a colleague if i could add a parameter in my function, no problem, just some dividing..

So you can either set percent CPU/Memory or number of hosts and from that get a percent on the policy. I have not taken care of the case if you have more than 100 hosts in your cluster (as this is not possible, at least not yet 😉 )

function Set-HAAdmissionControlPolicy{
<#
.SYNOPSIS
Set the Percentage HA Admission Control Policy
 
.DESCRIPTION
Percentage of cluster resources reserved as failover spare capacity
 
.PARAMETER  Cluster
The Cluster object that is going to be configurered 

.PARAMETER numberHosts
When this parameter is set the percentage is set based on number of hosts in cluster
 
.PARAMETER percentCPU
The percent reservation of CPU Cluster resources
 
.PARAMETER percentMem
The percent reservation of Memory Cluster resources
 
.EXAMPLE
PS C:\> Set-HAAdmissionControlPolicy -Cluster $CL -percentCPU 50 -percentMem 50
 
.EXAMPLE
PS C:\> Get-Cluster | Set-HAAdmissionControlPolicy -percentCPU 50 -percentMem 50

.EXAMPLE
PS C:\> Set-HAAdmissionControlPolicy -Cluster $CL -numberHosts 5
 
.NOTES
Author: Niklas Akerlund / RTS
Date: 2012-01-25
#>
   param (
   [Parameter(Position=0,Mandatory=$true,HelpMessage="This need to be a clusterobject",
    ValueFromPipeline=$True)]
    $Cluster,
	[int]$numberHosts = 0,
    [int]$percentCPU = 25,
    [int]$percentMem = 25
    )
 
    if(Get-Cluster $Cluster){
 
        $spec = New-Object VMware.Vim.ClusterConfigSpecEx
        $spec.dasConfig = New-Object VMware.Vim.ClusterDasConfigInfo
        $spec.dasConfig.admissionControlPolicy = New-Object VMware.Vim.ClusterFailoverResourcesAdmissionControlPolicy
		if($numberHosts -eq 0){
        	$spec.dasConfig.admissionControlPolicy.cpuFailoverResourcesPercent = $percentCPU
        	$spec.dasConfig.admissionControlPolicy.memoryFailoverResourcesPercent = $percentMem
 		}else{
			$spec.dasConfig.admissionControlPolicy.cpuFailoverResourcesPercent = 100/$numberHosts
        	$spec.dasConfig.admissionControlPolicy.memoryFailoverResourcesPercent = 100/$numberHosts
		}
        $Cluster = Get-View $Cluster
        $Cluster.ReconfigureComputeResource_Task($spec, $true)
    }
}