Automation

 View Only
Expand all | Collapse all

Capture all VMs from multiple vCenters CPU and MEM Shares Level

  • 1.  Capture all VMs from multiple vCenters CPU and MEM Shares Level

    Posted 21 days ago
    Edited by Roger Haines 20 days ago

    I need to determine which VMs have the value set to low, so I want to capture them all.  Below is what have cobbled together from another script I found on here thanks to LucD.  I thought it was working but the output has lots of duplicates.  Somehow it's showing the same VM for all vCenters.  So each VM is duplicated 4 times.  I don't know what's wrong.   The top line is just for my own future reference, and so you can sort of see where I'm going with it.  Also, just a little background note, that this all came about after we discovered a large number of VMs with the CPU and MEM shares values set to low.  We think it may have happened as a result of some templates that were used to create the VMs.

    #Get-VM -Name * | Get-VMResourceConfiguration | Set-VMResourceConfiguration -CpuSharesLevel Normal -MemSharesLevel Normal

    ---------Start of script-------------

    $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
    #$vCenter = Read-Host "Enter the vCenter name you wish to connect to."
    # Connect to vCenter(s)
    $vCenter = @(
        "vc02.domain.com",
        "vc03.domain.com",
        "vc04.domain.com",
        "vc05.domain.com"
    )
    Connect-VIServer -Server $vCenter

    #Get-VM -Name * | Get-VMResourceConfiguration | FT -AutoSize


    foreach ($vc in $global:defaultviservers) {

        Get-VM -PipelineVariable vm |

        Get-VMResourceConfiguration |

        Select-Object @{N = 'vCenter'; E = { $vc.Name } },

             @{N = 'VM'; E = { $_.VM.Name } },

            CPUSharesLevel, MemSharesLevel |
            
            Export-Csv C:\Output\Shares-Level-$timestamp.csv -Append -NoTypeInformation



  • 2.  RE: Capture all VMs from multiple vCenters CPU and MEM Shares Level

    Posted 21 days ago
    Edited by James Dougherty 20 days ago

    Your connection to vCenter isn't in the loop so your are looping through the vCenters but never connecting to the next vCenter.  You will also have to add a disconnect.  So like this.

    $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
    #$vCenter = Read-Host "Enter the vCenter name you wish to connect to."
    # Connect to vCenter(s)
    $vCenter = @(
        "vc02.domain.com",
        "vc03.domain.com",
        "vc04.domain.com",
        "vc05.domain.com"
    )


    foreach ($vc in $global:defaultviservers) {
        Connect-VIServer -Server $vCenter
        Get-VM -PipelineVariable vm |
        Get-VMResourceConfiguration |
        Select-Object @{N = 'vCenter'; E = { $vc.Name } },
            @{N = 'VM'; E = { $_.VM.Name } },
            CPUSharesLevel, MemSharesLevel |
            Export-Csv C:\Output\Shares-Level-$timestamp.csv -Append -NoTypeInformation

            Disconnect-VIServer * -Confirm:$false




  • 3.  RE: Capture all VMs from multiple vCenters CPU and MEM Shares Level

    Posted 20 days ago

    Hi James,  Thank you very much for your response!  Doesn't this line do the loop?

    "foreach ($vc in $global:defaultviservers) {"




  • 4.  RE: Capture all VMs from multiple vCenters CPU and MEM Shares Level

    Posted 20 days ago
    Edited by Roger Haines 20 days ago

    Also, as a comparison, this was the code I got from another thread that does work.  Thanks  @LucD

    $VCenter = @('vc02.domain.com', 'vc03.domain.com', 'vc04.domain.com', 'vc05.domain.com')
    $AdminUser = 'DOMAIN\Administrator'

    $Password = 'XXyyZZ!@#'


    Connect-VIServer -Server $VCenter #-User $AdminUser -Password $Password

    foreach ($vc in $global:defaultviservers) {

        Get-VM -PipelineVariable vm |

        Get-Snapshot |

        Select-Object @{N = 'vCenter'; E = { $vc.Name } },

            @{N = 'vCenter Version'; E = { $vc.Version } },

            @{N = 'vCenter Build'; E = { $vc.Build } },

            @{N = 'VM'; E = { $_.VM.Name } },

            Name, Description, Created, SizeGB | FT -AutoSize

    }




  • 5.  RE: Capture all VMs from multiple vCenters CPU and MEM Shares Level

    Posted 20 days ago
    Edited by Roger Haines 20 days ago

    Disregard....   I was wrong, it's still not looping.  Going back to try your original suggestion.  Sorry for the confusion.

    Well, I got it to work using the code below.  Just not sure why it works this way, but not the way I was trying.  

    $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
    #$vCenter = Read-Host "Enter the vCenter name you wish to connect to."
    # Connect to vCenter(s)

    $VCenter = @('vc02.domain.com', 'vc03.domain.com', 'vc04.domain.com', 'vc05.domain.com')
    #$AdminUser = 'DOMAIN\Administrator'

    #$Password = 'XXyyZZ!@#'


    Connect-VIServer -Server $VCenter #-User $AdminUser -Password $Password

    foreach ($vc in $global:defaultviservers) {

        Get-VM -PipelineVariable vm |

        Get-VMResourceConfiguration |

        Select-Object @{N = 'vCenter'; E = { $vc.Name } },

            @{N = 'vCenter Version'; E = { $vc.Version } },

            @{N = 'vCenter Build'; E = { $vc.Build } },

            @{N = 'VM'; E = { $_.VM.Name } },

            CPUSharesLevel, MemSharesLevel | Export-Csv C:\Output\Shares-Level-$timestamp.csv -NoTypeInformation #FT -AutoSize

    }

    Disconnect-VIServer -Server * -Confirm:$false -Force




  • 6.  RE: Capture all VMs from multiple vCenters CPU and MEM Shares Level

    Posted 20 days ago

    Can you try like this?

    $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
    
    $vCenter = @(
        "vc02.domain.com",
        "vc03.domain.com",
        "vc04.domain.com",
        "vc05.domain.com"
    )
    Connect-VIServer -Server $vCenter
    
    Get-VM |
    Get-VMResourceConfiguration |
    Select-Object @{N='vCenter';E={([System.Uri]$_.ExtensionData.Client.ServiceUrl).Host}},
        @{N = 'VM'; E = { $_.VM.Name } },
        CPUSharesLevel, MemSharesLevel |
    Export-Csv C:\Output\Shares-Level-$timestamp.csv -NoTypeInformation
    
    


    ------------------------------


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


    ------------------------------



  • 7.  RE: Capture all VMs from multiple vCenters CPU and MEM Shares Level

    Posted 20 days ago
    Edited by Roger Haines 20 days ago

    Hi Luc,

    Thanks for chiming in.  The code you provided returns all the VM's, and their CPU and MEM Shares levels, but not the vCenter name.

    I was also playing around with the code below, also yours, from another post.  Found it interesting, but having the same issue with getting the vCenter name.

    From HERE 

    And with $global:defaultviservers you can check to which vCenters you are connected.

    $vCenters = 'vc1','vc2'

    Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false

    Connect-VIServer -Server $vCenters -User xxxxx -Password xxxxx

    $global:DefaultVIServers

    $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
    #$vCenter = Read-Host "Enter the vCenter name you wish to connect to."
    # Connect to vCenter(s)
     
    $vCenters = @('vc02.domain.com', 'vc03.domain.com', 'vc04.domain.com', 'vc05.domain.com')
    #$AdminUser = 'DOMAIN\Administrator'
    Connect-VIServer -Server $vCenters
        
        Get-VM -PipelineVariable vm |
     
        Get-VMResourceConfiguration |
        
        Select-Object @{N = 'vCenter'; E = { $vcenters } },
     
            @{N = 'vCenter Version'; E = { $vc.Version } },
     
            @{N = 'vCenter Build'; E = { $vc.Build } },
     
            @{N = 'VM'; E = { $_.VM.Name } },
     
            CPUSharesLevel, MemSharesLevel | Export-Csv C:\Output\Get-Shares-Level-$timestamp.csv -NoTypeInformation




  • 8.  RE: Capture all VMs from multiple vCenters CPU and MEM Shares Level

    Posted 19 days ago

    My bad, that should have been

    @{N='vCenter';E={([System.Uri]$_.VM.ExtensionData.Client.ServiceUrl).Host}}


    ------------------------------


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


    ------------------------------



  • 9.  RE: Capture all VMs from multiple vCenters CPU and MEM Shares Level

    Posted 19 days ago
    Edited by Roger Haines 19 days ago

    That did it!  Thanks Luc. 

    Side note,  

    Odd issue.  I got the following error on every VM that setting the CPU and MEM Shares Levels to normal.  We had many VMs set to low, we think may have happened due to a template.  Anyway, I changed this line to set the Shares and got the error below.  The odd thing is that even though I got the error, it was changing the setting to the value indicted.  

    Get-VMResourceConfiguration | Set-VMResourceConfiguration -CpuSharesLevel Normal -MemSharesLevel Normal |

    Set-VMResourceConfiguration : 10/16/2024 6:43:45 PM Set-VMResourceConfiguration Operation is not valid due to the current state of the object.
    At line:18 char:35
    + ... iguration | Set-VMResourceConfiguration -CpuSharesLevel Normal -MemSh ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Set-VMResourceConfiguration], ViError
        + FullyQualifiedErrorId : Client20_VmServiceImpl_SetVMResourceConfiguration_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetVMResourceConfiguration

    Thanks @James Dougherty & @LucD!!  Very much appreciate your help.

    Roger




  • 10.  RE: Capture all VMs from multiple vCenters CPU and MEM Shares Level

    Posted 19 days ago

    I assume you are connected to multiple vCenters?
    What is in $global:defaultVIServers.

    Does the error also pop up when you are only connected to 1 vCenter?



    ------------------------------


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


    ------------------------------



  • 11.  RE: Capture all VMs from multiple vCenters CPU and MEM Shares Level

    Posted 19 days ago

    $global:defaultVIServers shows connected to all 4 vCenters.  I didn't try it connected to only one since it actually changed the setting.  I'll try that later and let you know.

    I did find this thread...

    https://powercli.ideas.aha.io/ideas/PCLI-I-208