PowerCLI

 View Only
  • 1.  Script to check VM DRS Groups ?

    Posted Dec 14, 2015 01:59 PM

    Hello Guys,

    I want to know if you could give me some insight on how to Check the VM DRS Groups on my cluster.

    DRS Group are created with a shared ID that is common on the VM also. For example: VM DRS Group R125 should include every VM name in which "R125" is included (like VM-R125-001).

    First of all, I'd like to work on a script that'll be able to detect if certain VM are not in their corresponding VM DRS Groups (by listing them for example), and later add a mail notification if possible.

    I'd like to know if you could give me a few tips on how to start this script, which cmdlet should I use, etc.

    Thank you very much :smileywink:

    Regards,

    Benoît.

    EDIT: VM DRS Group and not DRS rules



  • 2.  RE: Script to check VM DRS Groups ?

    Posted Dec 14, 2015 02:07 PM

    Did you already look at the DRS module ?

    It should simplify working with DRS rules and groups.



  • 3.  RE: Script to check VM DRS Groups ?

    Posted Dec 14, 2015 03:20 PM

    Thanks for your quick answer LucD.

    This is what I've done so far with your help, I'm still a beginner unfortunately.

    #Prerequisites

    # (new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex

    # Update-Module -Verbose -Module PsGet

    # Install-Module DRSRule

    #Set-PowerCLIConfiguration -InvalidCertificateAction ignore -confirm:$false

    #

    # vCenter Server specification

    $vCenter = "***"

    $vCenteruser ="***"

    $vCenterUserPasswd="***"

    #

    # Cluster specification

    $cluster1= "***"

    #

    # DRS Group specification

    $DRSGroupr125= "r125"

    #

    # Connect to vCenter

    add-pssnapin VMware.VimAutomation.Core

    Write-Output "Stand by, connecting to $vcenter…."

    Connect-VIServer -Server $vCenter -user $vCenterUser -password $vCenterUserPasswd

    #VM Included in DRS Group

    $DRSGroupr125VMs = Get-DrsVMGroup -Name "vm-r125-01" | Sort-Object vm | Select-Object vm

    #VM Containing the ID

    $VMa512 = Get-Cluster "****" | Get-vm | where {($_.name -like "*r125*")} | Sort-Object Name | Select-Object Name

    #Compare the 2 results

    Compare-Object -referenceobject $DRSGroupa512VMs -differenceobject $VMa512 -passThru | Sort-Object Name | Select-Object Name

    (How to post it as powershell syntax ?)

    Unfortunately it seems that line 25 and 27 give me no results if I use the variable instead of the name. edit: Special characters, nevermind

    What do you think ?

    Is it possible to launch the prerequisites only if not present ?

    Regards,

    Benoît.



  • 4.  RE: Script to check VM DRS Groups ?

    Posted Dec 14, 2015 06:43 PM

    With the -like operator you try find a partial match of the name.

    To specify a wildcard you use the asterisk.

    So "*r125*" means, that the target should have the string 'r125' anywhere.

    When you do "r125*" you are saying that the target should start with r125 and can have 0 or more other characters at the end.

    So try setting your variable to "*r125*".

    The Get-DrsRule cmdlet needs a value for the Cluster parameter.

    The VM property is an array. If you want to compare these to the array with VM names, you'll have to use the ExpandProperty parameter on the Select-Object.

    The following should set you on the right path

    $vCenter = "***" 

    $vCenteruser ="***" 

    $vCenterUserPasswd="***" 

    # Cluster specification 

    $cluster1= "***" 

    # DRS Group specification 

    $DRSGroupr125= "*r125*" 

    $cluster = Get-Cluster -Name $cluster1

    #VM Included in DRS Group 

    $DRSGroupr125VMs = Get-DrsVMGroup -Cluster $cluster -Name $DRSGroupr125 | Sort-Object vm | Select-Object -ExpandProperty vm 

    #VM Containing the ID 

    $VMa512 = Get-vm -Location $cluster | where {($_.name -like $DRSGroupr125)} | Sort-Object Name | Select-Object Name 

    #Compare the 2 results 

    Compare-Object -referenceobject $DRSGroupa512VMs -differenceobject $VMa512 -passThru | Sort-Object Name | Select-Object Name  



    To get the syntax-coloured text, you can refer to Some ways to enter PowerCLI code under the new forum SW


  • 5.  RE: Script to check VM DRS Groups ?

    Posted Dec 15, 2015 11:00 AM

    Hey guys,

    Many thanks for the good support.

    I rethink the script and start from scratch with a paper and a pen.

    I wanted this script to first scan all ID used and then do the comparison for each ID. The ID can be found by removing the following characters on the DRS group name: "vm-" and "-01".

    This is the code:

    # vCenter Server specification
    $vCenter = "***"
    $vCenteruser = "***"
    $vCenterUserPasswd="***"
    # Connect to vCenter
    add-pssnapin VMware.VimAutomation.Core
    Write-Output "Connecting to $vCenter…."
    Connect-VIServer -Server
    $vCenter -user $vCenterUser -password $vCenterUserPasswd
    # Import required module
    Import-Module DRSRule
    # Get DRSVMGROUP in an array
    $DRSGroup = Get-DrsVMGroup | Select-Object Name
    #
    #
    Cluster specification
    $cluster1= "***"
    $cluster = Get-Cluster -Name $cluster1
    #
    #
    for each customer ID
    foreach ($Group in $DRSGroup)
    {
       
    #VM Included in DRS Group
        $DRSGroupVMs = Get-DrsVMGroup -Cluster $cluster -Name $Group | Sort-Object vm | Select-Object -ExpandProperty vm
       
    #Get VM ID Only
        $ID = $Group  -Replace "vm-", ""
       
    $ID = $ID -Replace "-01", ""
       
    #VM Containing the ID
        $CustomersVM = Get-vm -Location $cluster1 | where {($_.name -like "*$ID*")} | Sort-Object Name | Select-Object Name
         
    #Compare the 2 results
        Compare-Object -referenceobject $DRSGroupVMs -differenceobject $CustomersVM -passThru | Sort-Object Name | Select-Object Name
      }

    The big issue I have is that line 22 doesn't give me any results for some reasons: Compare-Object : Cannot bind argument to parameter 'ReferenceObject' because it is null.

    I tried to enter directly the cluster name but it's the same result.

    EDIT: this has been solved by adding $DRSGroup = Get-DrsVMGroup | Select-Object Name | Select-Object -ExpandProperty Name

    Now I have: Compare-Object : Cannot bind argument to parameter 'DifferenceObject' because it is null.

    I'm doing some tests.

    EDIT2: I added this line:

    $ID = $Group  -Replace "vm-", ""

      $ID = $ID -Replace "-01", ""

      $ID = "*$ID*"

      #VM Containing the ID

      $CustomersVM = Get-vm -Location $cluster | where {($_.name -like $ID)} | Sort-Object Name | Select-Object -ExpandProperty Name

    It seems to work now.

    Regards,

    Benoît.



  • 6.  RE: Script to check VM DRS Groups ?
    Best Answer

    Posted Dec 15, 2015 11:28 AM

    Try like this

    Import-Module DRSRule

    #

    # Cluster specification

    $cluster1= "ClusterName"

    $cluster = Get-Cluster -Name $cluster1

    #

    #for each customer ID

    foreach ($Group in (Get-DrsVMGroup -Cluster $cluster))

    {

       # Get all VMs that match the groupID

       $vms = Get-VM -Name "vm-$($Group.Name)-01" -Location $cluster | Select Name

       # Compare the 2 results

       $result = Compare-Object -referenceobject $Group.VM -DifferenceObject $vms

       # Display result

       Write-Output "Missing from DRS rule $([string]::Join(($result | where{$_.SideIndicator -eq '=>'} | Select -ExpandProperty InputObject),'|'))"

       Write-Output "To be removed from DRS rule $([string]::Join(($result | where{$_.SideIndicator -eq '<='} | Select -ExpandProperty InputObject),'|'))"

    }



  • 7.  RE: Script to check VM DRS Groups ?

    Posted Dec 15, 2015 02:34 PM

    Hey LucD,

    For me it's perfect like this with your last input, we can close the subject.

    Thank you !

    Benoît.



  • 8.  RE: Script to check VM DRS Groups ?

    Posted Dec 14, 2015 06:40 PM

    I believe for those two lines you mention you want a

    select -expand vm

    select -expand name

    Or if we are doing it "correctly"

    select-object -expandproperty vm

    select-object -expandproperty name