Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 741
  • Last Modified:

VMWAre: Adding a new disk to VM with affinity rules off

I have this script:

$ourVm = Get-VM $vmname  
$ourDisk = $ourVm | Get-HardDisk  
$antiAffinityRule = New-Object 'VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.SdrsVMDiskAntiAffinityRule' $ourDisk
New-HardDisk -VM $ourVm -AdvancedOption $antiAffinityRule -CapacityGB 300 -Datastore $datastorecluster

Open in new window

This is to be able to add a disk to a VM in a datastore cluster with enough space, but the VM itself is on a datastore in that cluster that does not have enough space for the disk. By turning the affinity rule settings off I am hoping to add the new disk on a datastore in the cluster where there is enough space, even if that is not together with the VM.

The script is the same as what can be found in Example 5 here:

I get this error:

New-HardDisk : 20.02.2014 09:55:54    New-HardDisk        A specified parameter
 was not correct.
At line:1 char:1
+ New-HardDisk -VM $ourVm -AdvancedOption $antiAffinityRule -CapacityGB 300 -Da
tas ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-HardDisk], InvalidArgumen
    + FullyQualifiedErrorId : Client20_VirtualDeviceServiceImpl_NewVirtualHard
  • 2
1 Solution
In the last parameter which says
-Datastore $datastorecluster
You have not defined $datastorecluster anywhere in the script.
Go to your vCenter and see what is the name of the Datastore cluster. For example you might have named the cluster as Example_Cluster.
So modify the last parameter as
-Datastore Example_Cluster

See if it works.
itniflAuthor Commented:
You are right in my initial example I forgot to initiate the variable datastorecluster. However, in my full script the datastorecluster variable gets initiated like this:

(get-datastorecluster -datastore (get-datastore -VMHost (Get-Cluster -VM $vmname | get-vmhost)[0])) | %{
	if($dataStoreClusters.name -notcontains $_.name) {
		$dataStoreClusters += @{"name" = $_.name; "freePercentage" = "{0:P0}" -f ($_.FreeSpaceGB / $_.CapacityGB)}
$selectedDataStore = @{"name" = "NA"; "freePercentage" = "0 %"}

foreach($dataStoreCluster in $dataStoreClusters) {
	$excluded = $exclusionList -contains $datastoreCluster.name
	if(!$excluded) {
		$freePercentage1 = $dataStoreCluster.freePercentage
		$freePercentage2 = $selectedDataStore.freePercentage	
		if(([int]$freePercentage1.substring(0,$freePercentage1.length - 2)) -gt ([int]$freePercentage2.substring(0,$freePercentage2.length - 2))) {
			$lowpri = $lowPriorityList -contains $dataStoreCluster.name
			if((!$lowpri -and [int]$freePercentage1.substring(0,$freePercentage1.length - 2) -gt 9) -or ($lowpri -and $freePercentage2.substring(0,$freePercentage2.length - 2) -lt 10)) {
				$selectedDataStore = $dataStoreCluster
$selectedDataStoreName = $selectedDataStore.name
$datastorecluster = $selectedDataStoreName.toUpper()

Open in new window

This makes for the wrong datastorecluster, even if it is available to the hosts that host the VM. I guess I need to initiate it with the datastore cluster name where the vmx file is, how do I do that?
itniflAuthor Commented:
You got the answert correct, even though it wasn't all i was after . I will start a new question that is more on point.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now