PowerCLI

 View Only
Expand all | Collapse all

powercli script with options menu

  • 1.  powercli script with options menu

    Posted Jun 27, 2018 03:14 PM

    Hi People, im new in powercli!

    Is possible in powercli, create a script where appears a menu with options to choose like a what vcenter to connect, choose cluster in this vCenter.... and with this data, execute a task

    For example, to choose a vCenter, in this vCenter  choose a cluster, in that cluster that shows me the DRS rules..

    It's possible?

    How can I do it?

    Thanks in advanced

    Regards



  • 2.  RE: powercli script with options menu

    Posted Jun 27, 2018 03:35 PM

    You can do something like this

    $data = @'

    Name

    VC1

    VC2

    VC3

    '@

    $selectedVC = $data | ConvertFrom-Csv | select Name |

    Out-GridView -OutputMode Single


    Write-Host "You selected $($selectedVC.Name)"



  • 3.  RE: powercli script with options menu

    Posted Jul 02, 2018 01:10 PM

    Thanks!

    And now... Once I choose a vCenter, how can I connect to that vCenter, take another options menu to choose cluster, and of that cluster, show me the MVs that are not in a DRS group?

    Thanks in advanced!



  • 4.  RE: powercli script with options menu

    Posted Jul 03, 2018 04:48 AM

    Try like this

    $data = @'

    Name

    VC1

    VC2

    VC3

    '@

    $selectedVC = $data | ConvertFrom-Csv | select Name |

    Out-GridView -OutputMode Single


    Connect-VIServer -Server $selectedVC.Name


    $selectedCLuster = Get-Cluster -Server $selectedVC.Name | Select Name | Out-GridView -OutputMode Single

    $vmInRule = Get-View -Id (Get-DrsRule -Cluster $selectedCLuster.Name).VmIds -Property Name | %{$_.Name}

    Get-Cluster -Name $selectedCLuster.Name | Get-VM | where{$vmInRule -notcontains $_.Name} | Select Name



  • 5.  RE: powercli script with options menu

    Posted Jul 03, 2018 12:32 PM

    i try this, but i can´t select a cluster, don´t work :smileysad:

    $data = @'

    Name

    VC1

    VC2

    VC3

    '@

    $selectedVC = $data | ConvertFrom-Csv | select Name |

    Out-GridView -OutputMode Single

    Connect-VIServer -Server $selectedVC

    $data1 = @'

    Cluster

    Cluster1

    Cluster2

    Cluster3

    '@

    $selectedVC = $data | ConvertFrom-Csv | select Clusteer |

    Out-GridView -OutputMode Single

    $selectedCLuster = Get-Cluster -Server $selectedVC | Select Name | Out-GridView -OutputMode Single

    $vmInRule = Get-View -Id (Get-DrsRule -Cluster $selectedCLuster.Name).VmIds -Property Name | %{$_.Name}

    Get-Cluster -Name $selectedCLuster.Name | Get-VM | where{$vmInRule -notcontains $_.Name} | Select Name



  • 6.  RE: powercli script with options menu

    Posted Jul 03, 2018 12:35 PM

    That's not what I had in my last reply?
    And I made a change to the Connect-VIServer in the code above.

    Can you try that gain?
    And show the script you are actually executing.



  • 7.  RE: powercli script with options menu

    Posted Jul 03, 2018 01:32 PM

    Hi!

    Yes, yes sorry! with the last update the script works! but it does not filter well the machines that are not in a rules of DRS groups, it shows all the MVs of the cluster!!

    The rules of the DRS groups are a bit special!!

    Tnaks in advanced

    Regards



  • 8.  RE: powercli script with options menu

    Posted Jul 03, 2018 01:39 PM

    The code relies on the output of the Get-DrsRule cmdlet to find the VMs that are used in each DRS rule.

    Does the following show all the VMs that appear in your DRS rules for a specific cluster?

    $clusterName = 'MyCluster'

    Get-CLuster -Name $clusterName |

    Get-DrsRule | %{

        Get-View -Id $_.VMIDs -Property Name |

        Select -ExpandProperty Name

    }



  • 9.  RE: powercli script with options menu

    Posted Jul 03, 2018 01:52 PM

    with those lines, only shows me some MV, s, in theory all the MVs are in a DRS rule, so I would like you to show me if there was any MV without a rule

    Thanks!



  • 10.  RE: powercli script with options menu

    Posted Jul 03, 2018 01:55 PM

    It looks as if not all VM s that are in DRS rules can be found this way then.

    What kind of DRS rules do you use? Affinity, anti-affinity, VMHost-VM rules, start group order...

    Are there VM Groups defined?



  • 11.  RE: powercli script with options menu

    Posted Jul 03, 2018 09:30 PM

    What kind of DRS rules do you use? Affinity, anti-affinity, VMHost-VM rules, start group order...

    Yes, i have Affinity, anti-affinity, VMHost-VM rules... start group order no

    Are there VM Groups defined?

    VM Groups no, DRS Groups yes!

    Thanks!



  • 12.  RE: powercli script with options menu

    Posted Jul 04, 2018 04:48 AM

    What do you mean by a DRS Group?

    Is that a VM/VMHost group under DRS?

    A screenshot of the location where a VM is defined that is not listed by the snippet would help.



  • 13.  RE: powercli script with options menu

    Posted Jul 04, 2018 07:17 AM

    Yes, i have a virtual machines DRS Groupalia, this DRS Groupalia contains VMs



  • 14.  RE: powercli script with options menu

    Posted Jul 04, 2018 07:19 AM

    But is that group used in a DRS rule?

    A screenshot to clarify would help.



  • 15.  RE: powercli script with options menu

    Posted Jul 04, 2018 08:20 AM

    I can,t upload a  screenshot of my infraestructure :smileysad:

    But in this blog--> vSphere 5 DRS Groups and Rules - blog.shiplett.org You can see screenshots like my infraestructure just other mames!



  • 16.  RE: powercli script with options menu

    Posted Jul 04, 2018 11:01 AM

    I understand that, and I know what a DRS VM group is.

    But the important question, are all these groups used in a DRS rule?

    Or do you have VM groups that are defined but never used in a DRS rule?



  • 17.  RE: powercli script with options menu
    Best Answer

    Posted Jul 04, 2018 11:56 AM

    Try this adapted version which starts from the DRS VM groups.

    Get-DrsClusterGroup -Type VMGroup | %{

       $_.Member | Select -ExpandProperty Name

    }


    $data = @'

    Name

    VC1

    VC2

    VC3

    '@


    $selectedVC = $data | ConvertFrom-Csv | select Name | Out-GridView -OutputMode Single


    Connect-VIServer -Server $selectedVC.Name


    $selectedCluster = Get-Cluster -Server $selectedVC.Name | Select Name | Out-GridView -OutputMode Single


    $vmInGroup = Get-DrsClusterGroup -Type VMGroup | %{

       $_.Member | Select -ExpandProperty Name

    }


    Get-Cluster -Name $selectedCluster.Name | Get-VM | where{$vmInGroup -notcontains $_.Name} | Select Name



  • 18.  RE: powercli script with options menu

    Posted Jul 04, 2018 01:13 PM

    Now works perfectly!

    Any idea to export the final result (VMs in not DRS rules) to CSV?

    Thanks!!



  • 19.  RE: powercli script with options menu

    Posted Jul 04, 2018 01:17 PM

    Change the last line into

    Get-Cluster -Name $selectedCLuster.Name | Get-VM | where{$vmInGroup -notcontains $_.Name} | Select Name |

    Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture



  • 20.  RE: powercli script with options menu

    Posted Jul 04, 2018 08:34 PM

    Perfect!!

    Thanks!!