Add & Remove VM from DRS Groups based on datastore. Using PowerShell Ubuntu 19.10 Budgie. Add-DrsVMtoDrsVMGroup and Remove-DrsVMFromDrsVMGroup

Thanks to

Author: Tim Carman
Twitter: @tpcarman
Github: tpcarman

https://www.timcarman.net/2017/10/27/add-remove-virtual-machines-based-datastore-location-powercli/

Functions:
Add-DrsVMtoDrsVMGroup
Remove-DrsVMFromDrsVMGroup

  1.  install Powershell
  2. Get PowerShell to work with ubuntu 19.10 Budgie
  3. Create Powershell Profile
  4. Run it!
sudo snap install powershell --classic

export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1

vi /home/USERNAME/config/powershell/Microsoft.PowerShell_profile.ps1

Paste the following in the file:


$Hour = (Get-Date).Hour
If ($Hour -lt 12) {"Morning Master"}
ElseIf ($Hour -gt 17) {"Good Evening Master"}
Else {"Have a nice day Roger"}

#************************
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false

#*************************
function Add-DrsVMToDrsVMGroup{
#Requires -Modules VMware.VimAutomation.Core, DRSRule
#region script help
<#
.SYNOPSIS  
    Adds virtual machines to a DRS VM group based on datastore location
.DESCRIPTION
    Adds virtual machines to a DRS VM group based on datastore location
.NOTES
    Version:        1.0
    Author:         Tim Carman
    Twitter:        @tpcarman
    Github:         tpcarman
.LINK
    https://github.com/tpcarman/PowerCLI-Scripts	
.PARAMETER DrsVMGroup
    Specifies the DRS VM Group
    This parameter is mandatory but does not have a default value.
.PARAMETER Cluster
    Specifies the cluster which contains the DRS VM Group
    This parameter is mandatory but does not have a default value.
.PARAMETER Prefix
    Specifies a prefix string for the datastore name
    This parameter is optional and does not have a default value.
.PARAMETER Suffix
    Specifies a suffix string for the datastore name
    This parameter is optional and does not have a default value.
.PARAMETER Datastore
    Specifies a datastore name
    This parameter is optional and does not have a default value.
.EXAMPLE
    Add-DrsVMtoDrsVMGroup -DRSVMGroup 'SiteA-VMs' -Cluster 'Production' -Prefix 'SiteA-'
.EXAMPLE
    Add-DrsVMtoDrsVMGroup -DRSVMGroup 'SiteA-VMs' -Cluster 'Production' -Suffix '-02'
.EXAMPLE
    Add-DrsVMtoDrsVMGroup -DRSVMGroup 'SiteB-VMs' -Cluster 'Production' -Datastore 'VMFS-01'
#>
#endregion script help
#region script parameters
[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True,HelpMessage='Specify the name of the DRS VM Group')]
    [ValidateNotNullOrEmpty()]
    [String]$DrsVMGroup='',
    [Parameter(Mandatory=$True,HelpMessage='Specify the cluster name')]
    [ValidateNotNullOrEmpty()] 
    [String]$Cluster='',  
    [Parameter(Mandatory=$False,ParameterSetName=’Prefix’,HelpMessage='Specify the prefix string for the datastore name')]
    [ValidateNotNullOrEmpty()]
    [String]$Prefix='',
    [Parameter(Mandatory=$False,ParameterSetName=’Suffix’,HelpMessage='Specify the suffix string for the datastore name')]
    [ValidateNotNullOrEmpty()]
    [String]$Suffix='',      
    [Parameter(Mandatory=$False,ParameterSetName=’Datastore’,HelpMessage='Specify the datastore name')]
    [ValidateNotNullOrEmpty()]
    [String]$Datastore=''
	)
#endregion script parameters
#region script body
if($Prefix){
    $VMs = Get-Datastore | where{($_.name).StartsWith($Prefix)} | Get-VM
    }
if($Datastore){
    $VMs = Get-Datastore | where{($_.name) -eq $Datastore} | Get-VM
    }
if($Suffix){
    $VMs = Get-Datastore | where{($_.name).EndsWith($Suffix)} | Get-VM
    }
$objDrsVMGroup = Get-DrsVMGroup -Name $DrsVMGroup -Cluster $Cluster
foreach($VM in $VMs){
    if(($objDrsVMGroup).VM -notcontains $VM){
    	Write-Host "Adding virtual machine $VM to DRS VM Group $DrsVMGroup"
        try{
            Set-DrsVMGroup -Name $DrsVMGroup -Cluster $Cluster -Append -VM $VM
        }
        catch{
            Write-Error "Error adding virtual machine $VM to DRS VM Group $DrsVMGroup"
        } 
    }
}
#endregion script body
}

#**************************
function Remove-DrsVMFromDrsVMGroup{
#Requires -Modules VMware.VimAutomation.Core, DRSRule
#region script help
<#
.SYNOPSIS  
    Removes virtual machines from a DRS VM group based on datastore location
.DESCRIPTION
    Removes virtual machines from a DRS VM group based on datastore location
.NOTES
    Version:        1.0
    Author:         Tim Carman
    Twitter:        @tpcarman
    Github:         tpcarman
.LINK
    https://github.com/tpcarman/PowerCLI-Scripts	
.PARAMETER DrsVMGroup
    Specifies the DRS VM Group
    This parameter is mandatory but does not have a default value.
.PARAMETER Cluster
    Specifies the cluster which contains the DRS VM Group
    This parameter is mandatory but does not have a default value.
.PARAMETER Prefix
    Specifies a prefix string for the datastore name
    This parameter is optional and does not have a default value.
.PARAMETER Suffix
    Specifies a suffix string for the datastore name
    This parameter is optional and does not have a default value.
.PARAMETER Datastore
    Specifies a datastore name
    This parameter is optional and does not have a default value.
.EXAMPLE
    Remove-DrsVMFromDrsVMGroup -DRSVMGroup 'SiteA-VMs' -Cluster 'Production' -Prefix 'SiteA-' 
.EXAMPLE
    Remove-DrsVMFromDrsVMGroup -DRSVMGroup 'SiteA-VMs' -Cluster 'Production' -Suffix '-02' 
.EXAMPLE
    Remove-DrsVMFromDrsVMGroup -DRSVMGroup 'SiteB-VMs' -Cluster 'Production' -Datastore 'VMFS-01' 
#>
#endregion script help
#region script parameters
[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True,HelpMessage='Specify the name of the DRS VM Group')]
    [ValidateNotNullOrEmpty()]
    [String]$DrsVMGroup='',
    [Parameter(Mandatory=$True,HelpMessage='Specify the cluster name')]
    [ValidateNotNullOrEmpty()] 
    [String]$Cluster='',  
    [Parameter(Mandatory=$False,ParameterSetName=’Prefix’,HelpMessage='Specify the prefix string for the datastore name')]
    [ValidateNotNullOrEmpty()]
    [String]$Prefix='',
    [Parameter(Mandatory=$False,ParameterSetName=’Suffix’,HelpMessage='Specify the suffix string for the datastore name')]
    [ValidateNotNullOrEmpty()]
    [String]$Suffix='',       
    [Parameter(Mandatory=$False,ParameterSetName=’Datastore’,HelpMessage='Specify the datastore name')]
    [ValidateNotNullOrEmpty()]
    [String]$Datastore=''
	)
#endregion script parameters
#region script body
if($Prefix){
    $VMs = Get-Datastore | where{($_.name).StartsWith($Prefix)} | Get-VM | sort Name
        }
if($Datastore){
    $VMs = Get-Datastore | where{($_.name) -eq $Datastore} | Get-VM | sort Name
    }
if($Suffix){
    $VMs = Get-Datastore | where{($_.name).EndsWith($Suffix)} | Get-VM | sort Name
    }
$objDrsVMGroup = Get-DrsVMGroup -Name $DrsVMGroup -Cluster $Cluster
foreach($VM in $VMs){
    if(($objDrsVMGroup).VM -contains $VM){
    	Write-Host "Removing virtual machine $VM from DRS VM Group $DrsVMGroup"
        try{
            Set-DrsVMGroup -Name $DrsVMGroup -Cluster $Cluster -RemoveVM $VM
        }
        catch{
            Write-Error "Error removing virtual machine $VM from DRS VM Group $DrsVMGroup"
        } 
    }
}
#endregion script body
}

#********************************
#Import script modules
Import-Module VMware.VimAutomation.Core, DrsRule

# Connect to vCenter Server
#Connect-VIServer -Server $vcenter -Credential $creds
# Add DC1 VMs to DC1 DRS VM Group
#Add-DrsVMtoDrsVMGroup -DrsVMGroup 'DC1_VMs' -Cluster 'Production' -Prefix 'DC1-'
# Remove DC2 VMs from DC1 DRS VM Group
#Remove-DrsVMFromDrsVMGroup -DrsVMGroup 'DC2_VMs' -Cluster 'Production' -Prefix 'DC1-'
# Add DC2 VMs to DC2 DRS VM Group
#Add-DrsVMtoDrsVMGroup -DrsVMGroup 'DC2_VMs' -Cluster 'Production' -Prefix 'DC2-'
# Remove DC1 VMs from DC2 DRS VM Group
#Remove-DrsVMFromDrsVMGroup -DrsVMGroup 'DC1_VMs' -Cluster 'Production' -Prefix 'DC2-'
# Disconnect from vCenter Server
#Disconnect-VIServer -Server $vcenter -Confirm:$false

#**************************************
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: