Hi,
i'm trying to create a script that configures the DiskSharesLevel per VM and per VM HardDisk.
Say i need to configure the value DiskSharesLevel to High for VM11 and VM1 has two hard disks, one with DiskSharesLevel value Low and one with DiskSharesLevel High, i can only touch the configuration for the hard disk with DiskSharesLevel Low.
Running the below gives me the HardDisk per VM that needs to be configured with the value specified in the $Disk1Shares:
#
#
$Disk1Shares="High"
#
$Tier1=get-vm | where {$_.CustomFields.item("Tier") -eq 1}
foreach ($vm in $Tier1) {
# Get the current DiskSharesLevel values for each VM
$vm_valuehash = Get-VM $vm | select HardDisks
$hd_confs = Get-VMResourceConfiguration -VM $vm | Select -ExpandProperty DiskResourceConfiguration
#Initialize the string to set resources
$set_vm_resource_string = ""
foreach($hd in $hd_confs){
$disk = @{"Name"=($vm_valuehash.HardDisks | where {$_.ExtensionData.Key -eq $hd.Key} | Select -ExpandProperty Name);"DiskSharesLevel"=$hd.DiskSharesLevel}
if ($disk.DiskSharesLevel -ne $Disk1Shares) {
$set_vm_resource_string += " -Disk " + $disk.Name + " -DiskSharesLevel $Disk1Shares"
}
}
}
The output for two VMs (VM1 has 1 HardDisk and VM2 has three HardDisks but only two needs to be reconfigured) looks like this:
-Disk Hard disk 1 -DiskSharesLevel High
-Disk Hard disk 1 -DiskSharesLevel High -Disk Hard disk 3 -DiskSharesLevel High
How can i use the values in $set_vm_resource_string to actually configure the DiskSharesLevel per HardDisk and VM?
i have, without any luck, tried:
get-vm $vm |Get-VMResourceConfiguration |Set-VMResourceConfiguration $set_vm_resource_string
¨Thanks in advanced
There is only one CPU and memory share per VM, so no need to extract that in a separate variable.
Something like this perhaps.
# # $Disk1Shares = "High"
$CpuShare = "High"
$MemShare = "High"
# $Tier1 = get-vm | where {$_.CustomFields.item("Tier") -eq 1} foreach ($vm in $Tier1) { # Get the current DiskSharesLevel values for each VM $vm_valuehash = Get-VM $vm | select HardDisks
$res = Get-VMResourceConfiguration -VM $vm
$hd_confs = $res | Select -ExpandProperty DiskResourceConfiguration #Initialize the string to set resources $set_vm_resource_string = @() foreach($hd in $hd_confs){ $vm_valuehash.HardDisks | where {$_.ExtensionData.Key -eq $hd.Key} | %{ if ($hd.DiskSharesLevel -ne $Disk1Shares) { $set_vm_resource_string += $_
} } } if($set_vm_resource_string){ Set-VMResourceConfiguration -Disk $set_vm_resource_string -DiskSharesLevel $Disk1Shares
} if ($res.CpuSharesLevel -ne $CpuShare){ Set-VMResourceConfiguration -Configuration $res -CpuSharesLevel $CpuShare
} if ($res.MemSharesLevel -ne $MemShare){ Set-VMResourceConfiguration -Configuration $res -MemSharesLevel $MemShare
} }
Btw I added a test to check if any harddisks need changing.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try it like this.
The Disk parameter expects an array of Harddisk objects, it doesn't allow multiple Disk parameters.
# # $Disk1Shares = "High"
# $Tier1 = get-vm | where {$_.CustomFields.item("Tier") -eq 1} foreach ($vm in $Tier1) { # Get the current DiskSharesLevel values for each VM $vm_valuehash = Get-VM $vm | select HardDisks
$hd_confs = Get-VMResourceConfiguration -VM $vm | Select -ExpandProperty DiskResourceConfiguration #Initialize the string to set resources $set_vm_resource_string = @() foreach($hd in $hd_confs){ $vm_valuehash.HardDisks | where {$_.ExtensionData.Key -eq $hd.Key} | %{ if ($hd.DiskSharesLevel -ne $Disk1Shares) { $set_vm_resource_string += $_
} } } Get-VMResourceConfiguration -VM $vm |
Set-VMResourceConfiguration -Disk $set_vm_resource_string -DiskSharesLevel $Disk1Shares
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I used the Get-Harddisk instead
# SetVMhddresource.ps1
#
# Niklas Akerlund
$tierHddshares = "High"
$VMs = Get-VM | where {$_.CustomFields.item("Tier") -eq 1}
foreach ($VM in $VMs){
$hdds = @()
$vmrescfhdds = $VM | Get-VMResourceConfiguration | select -ExpandProperty DiskResourceConfiguration
foreach ($vmrescfhdd in $vmrescfhdds){
if ($vmrescfhdd.DiskSharesLevel -ne $tierHddshares){
$hdds += $VM | Get-HardDisk | where {$_.Extensiondata.key -eq $vmrescfhdd.key}
}
}
$VM | Get-VMResourceConfiguration | Set-VMResourceConfiguration -Disk $hdds -DiskSharesLevel $tierHddshares
}
excellent this works perfect. thanks for your reply.
do you have any suggestion how to add e.g. CpuSharesLevel and MemorySharesLevel to the script?
first, increase the $vm_valuehash to include:
$vm_valuehash = Get-VM $vm | select @{N="CpuSharesLevel";E={$_.VMResourceConfiguration.CpuSharesLevel}}, @{N="MemSharesLevel";E={$_.VMResourceConfiguration.MemSharesLevel}}, HardDisks
then, do i need to initialize one string per object (CpuShares, MemoryShares, HardDisksShares)?
thanks
There is only one CPU and memory share per VM, so no need to extract that in a separate variable.
Something like this perhaps.
# # $Disk1Shares = "High"
$CpuShare = "High"
$MemShare = "High"
# $Tier1 = get-vm | where {$_.CustomFields.item("Tier") -eq 1} foreach ($vm in $Tier1) { # Get the current DiskSharesLevel values for each VM $vm_valuehash = Get-VM $vm | select HardDisks
$res = Get-VMResourceConfiguration -VM $vm
$hd_confs = $res | Select -ExpandProperty DiskResourceConfiguration #Initialize the string to set resources $set_vm_resource_string = @() foreach($hd in $hd_confs){ $vm_valuehash.HardDisks | where {$_.ExtensionData.Key -eq $hd.Key} | %{ if ($hd.DiskSharesLevel -ne $Disk1Shares) { $set_vm_resource_string += $_
} } } if($set_vm_resource_string){ Set-VMResourceConfiguration -Disk $set_vm_resource_string -DiskSharesLevel $Disk1Shares
} if ($res.CpuSharesLevel -ne $CpuShare){ Set-VMResourceConfiguration -Configuration $res -CpuSharesLevel $CpuShare
} if ($res.MemSharesLevel -ne $MemShare){ Set-VMResourceConfiguration -Configuration $res -MemSharesLevel $MemShare
} }
Btw I added a test to check if any harddisks need changing.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thanks for you effort.
when i add Get-VM $vm | Get-VMResourceConfiguration | before each Set-VMResourceConfiguration it works great
thanks again
Strange, that shouldn't be needed, the -Configuration $res should be enough.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference