VMware Cloud Community
shinpad
Contributor
Contributor

Script to list Virtual Machines not in DRS Groups

I'd like all my VMs to be in one of my DRS Groups.

Is there a way to run a script that will email a list of any VM that is not assigned to a Virtual Machine DRS Group?

thanks

Reply
0 Kudos
13 Replies
aerodevil
Hot Shot
Hot Shot

May not be the fastest or most efficient but it was the first way that came to mind.

foreach($vm in $vms){


# Check if VM is not in a cluster at all and report
If(!(Get-Cluster -VM $vm)){
    $vm | Add-Member -MemberType NoteProperty -Name Cluster -Value "None"
    $vm | Add-Member -MemberType NoteProperty -Name DRSEnabled -Value "None"
    $vm | Select Name, Cluster, DRSEnabled, PowerState
}Else{
# Since VM is in a cluster, check if DRS is enabled and report
    $cluster = Get-Cluster -VM $vm
    $vm | Add-Member -MemberType NoteProperty -Name Cluster -Value $cluster.Name
    $vm | Add-Member -MemberType NoteProperty -Name DRSEnabled -Value $cluster.DrsEnabled
    $vm | Select Name, Cluster, DRSEnabled, PowerState
}
}

Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/
Reply
0 Kudos
shinpad
Contributor
Contributor

Thank you for taking the time to reply.

Does this check for VM DRS Groups, or whether the VM is in a DRS enabled cluster?

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership

Josh's script checks if a VM is in a DRS-enabled cluster.

Try this to find VMs that are not in a DRS VM group.

$vmTab = @{}
Get-VM | %{
    $vmTab.Add($_.Name,$null)
}

Get-Cluster | %{
    $_.ExtensionData.ConfigurationEx.Group | where {$_.GetType().Name -eq "ClusterVmGroup"} | %{
        Get-View $_.Vm | %{
            $vmTab.Remove($_.Name)
        }
    }
}

$vmTab.Keys

The script uses a hash table to

1) get all the VMs

2) remove the VMs that are in a DRS VM group

3) display the VM names


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
aerodevil
Hot Shot
Hot Shot

Ahh....It seems I misread/misunderstood the request.  Once again well done Luc!  Learning something new every day Smiley Happy

Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/
Reply
0 Kudos
shinpad
Contributor
Contributor

This looks very good.

To test properly, how would I specify a specific cluster to check?

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership

Something like this

$vmTab = @{}
Get-Cluster -Name MyCLuster | Get-VM | %{
    $vmTab.Add($_.Name,$null)
}

Get-Cluster -Name MyCluster | %{
    $_.ExtensionData.ConfigurationEx.Group | where {$_.GetType().Name -eq "ClusterVmGroup"} | %{
        Get-View $_.Vm | %{
            $vmTab.Remove($_.Name)
        }
    }
}

$vmTab.Keys


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
shinpad
Contributor
Contributor

That’s not returning any results unfortunately.

Reply
0 Kudos
LucD
Leadership
Leadership

That could mean that all the VMs in that cluster are in a DRS group.

Try running this version, it will first display all the VMs that are found on the cluster.

$vmTab = @{}
Get-Cluster -Name MyCLuster | Get-VM | %{
    $vmTab.Add($_.Name,$null)
}

"VMs in cluster" 
$vmTab.Keys

Get-Cluster -Name MyCluster | %{     $_.ExtensionData.ConfigurationEx.Group | where {$_.GetType().Name -eq "ClusterVmGroup"} | %{         Get-View $_.Vm | %{             $vmTab.Remove($_.Name)         }     } } "VMs not in a DRS group"
$vmTab.Keys


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
shinpad
Contributor
Contributor

I removed a server from a DRS group to check, but no luck.

anyway, the first script you did is excellent and is working correclty. is there any way it could email me the results?

thank you

Reply
0 Kudos
LucD
Leadership
Leadership

Try it with the Send-MailMessage cmdlet

Send-MailMessage -SmtpServer mailserver.corp.com -To me@corp.com -From report@corp.com -Subject DRS -Body $vmTab.Keys 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
shinpad
Contributor
Contributor

this works, but just lists the vms one after the other e.g.

server1 server2 server3 .......

can it be formatted like

server1

server2

server3

thanks

Reply
0 Kudos
LucD
Leadership
Leadership

Change the Body parameter from

-Body  $vmTab.Keys

to

-Body  ($vmTab.Keys | Out-String)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
shinpad
Contributor
Contributor

That's excellent, and works very well.

Thank you very much for all your help, and thanks aerodevil for taking the time to reply too.

Reply
0 Kudos