Skip to content

Commit

Permalink
Add scripts for creating cluster groups and affinity rules.
Browse files Browse the repository at this point in the history
These scripts create DRS groups for virtual machines and hosts, and host
affinity rules.
  • Loading branch information
pjorg committed Dec 3, 2016
1 parent 2e2f027 commit 6967b1b
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Scripts/New-ClusterHostGroup.ps1
@@ -0,0 +1,43 @@
<#
.NOTES
===========================================================================
Script name: New-ClusterHostGroup.ps1
Created on: 2016-10-25
Author: Peter D. Jorgensen (@pjorg, pjorg.com)
Dependencies: None known
===Tested Against Environment====
vSphere Version: 5.5, 6.0
PowerCLI Version: PowerCLI 6.5R1
PowerShell Version: 5.0
OS Version: Windows 10, Windows 7
===========================================================================
.DESCRIPTION
Creates a DRS Host Group in a vSphere cluster.
.Example
$ProdCluster = Get-Cluster *prod*
$OddHosts = $ProdCluster | Get-VMHost | ?{ $_.Name -match 'esxi-\d*[13579]+.\lab\.local' }
.\New-ClusterHostGroup.ps1 -Name 'OddProdHosts' -Cluster $ProdCluster -VMHost $OddHosts
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[String]$Name,
[Parameter(Mandatory=$True,ValueFromPipeline=$True,Position=2)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.Cluster]$Cluster,
[Parameter(Mandatory=$False,Position=3)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost[]]$VMHost
)

$NewGroup = New-Object VMware.Vim.ClusterHostGroup -Property @{
'Name'=$Name
'Host'=$VMHost.Id
}

$spec = New-Object VMware.Vim.ClusterConfigSpecEx -Property @{
'GroupSpec'=(New-Object VMware.Vim.ClusterGroupSpec -Property @{
'Info'=$NewGroup
})
}

$ClusterToReconfig = Get-View -VIObject $Cluster -Property Name
$ClusterToReconfig.ReconfigureComputeResource($spec, $true)
43 changes: 43 additions & 0 deletions Scripts/New-ClusterVmGroup.ps1
@@ -0,0 +1,43 @@
<#
.NOTES
===========================================================================
Script name: New-ClusterVmGroup.ps1
Created on: 2016-10-25
Author: Peter D. Jorgensen (@pjorg, pjorg.com)
Dependencies: None known
===Tested Against Environment====
vSphere Version: 5.5, 6.0
PowerCLI Version: PowerCLI 6.5R1
PowerShell Version: 5.0
OS Version: Windows 10, Windows 7
===========================================================================
.DESCRIPTION
Creates a DRS VM Group in a vSphere cluster.
.Example
$ProdCluster = Get-Cluster *prod*
$EvenVMs = $ProdCluster | Get-VM | ?{ $_.Name -match 'MyVM-\d*[02468]+' }
.\New-ClusterVmGroup.ps1 -Name 'EvenVMs' -Cluster $ProdCluster -VM $EvenVMs
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[String]$Name,
[Parameter(Mandatory=$True,ValueFromPipeline=$True,Position=2)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.Cluster]$Cluster,
[Parameter(Mandatory=$False,Position=3)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]]$VM
)

$NewGroup = New-Object VMware.Vim.ClusterVmGroup -Property @{
'Name'=$Name
'VM'=$VM.Id
}

$spec = New-Object VMware.Vim.ClusterConfigSpecEx -Property @{
'GroupSpec'=(New-Object VMware.Vim.ClusterGroupSpec -Property @{
'Info'=$NewGroup
})
}

$ClusterToReconfig = Get-View -VIObject $Cluster -Property Name
$ClusterToReconfig.ReconfigureComputeResource($spec, $true)
48 changes: 48 additions & 0 deletions Scripts/New-ClusterVmHostRule.ps1
@@ -0,0 +1,48 @@
<#
.NOTES
===========================================================================
Script name: New-ClusterVmHostRule.ps1
Created on: 2016-10-25
Author: Peter D. Jorgensen (@pjorg, pjorg.com)
Dependencies: None known
===Tested Against Environment====
vSphere Version: 5.5, 6.0
PowerCLI Version: PowerCLI 6.5R1
PowerShell Version: 5.0
OS Version: Windows 10, Windows 7
===========================================================================
.DESCRIPTION
Creates a VM to Host affinity rule in a vSphere cluster.
.Example
$ProdCluster = Get-Cluster *prod*
.\New-ClusterVmHostRule.ps1 -Name 'Even VMs to Odd Hosts' -AffineHostGroupName 'OddHosts' -VMGroupName 'EvenVMs' -Enabled:$true -Cluster $ProdCluster
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[String]$Name,
[Parameter(Mandatory=$True,Position=2)]
[String]$AffineHostGroupName,
[Parameter(Mandatory=$True,Position=3)]
[String]$VMGroupName,
[Parameter(Mandatory=$False,Position=4)]
[Switch]$Enabled=$True,
[Parameter(Mandatory=$True,ValueFromPipeline=$True,Position=5)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.Cluster]$Cluster
)

$NewRule = New-Object VMware.Vim.ClusterVmHostRuleInfo -Property @{
'AffineHostGroupName'=$AffineHostGroupName
'VmGroupName'=$VMGroupName
'Enabled'=$Enabled
'Name'=$Name
}

$spec = New-Object VMware.Vim.ClusterConfigSpecEx -Property @{
'RulesSpec'=(New-Object VMware.Vim.ClusterRuleSpec -Property @{
'Info'=$NewRule
})
}

$ClusterToReconfig = Get-View -VIObject $Cluster -Property Name
$ClusterToReconfig.ReconfigureComputeResource($spec, $true)

0 comments on commit 6967b1b

Please sign in to comment.