Original Message:
Sent: Oct 17, 2024 03:13 PM
From: Roger Haines
Subject: Capture all VMs from multiple vCenters CPU and MEM Shares Level
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 as I wanted.
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
Original Message:
Sent: Oct 17, 2024 04:11 AM
From: LucD
Subject: Capture all VMs from multiple vCenters CPU and MEM Shares Level
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
Original Message:
Sent: Oct 16, 2024 06:42 PM
From: Roger Haines
Subject: Capture all VMs from multiple vCenters CPU and MEM Shares Level
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
Original Message:
Sent: Oct 16, 2024 06:20 PM
From: LucD
Subject: Capture all VMs from multiple vCenters CPU and MEM Shares Level
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 $vCenterGet-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
Original Message:
Sent: Oct 16, 2024 02:44 PM
From: Roger Haines
Subject: Capture all VMs from multiple vCenters CPU and MEM Shares Level
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
Original Message:
Sent: Oct 16, 2024 10:57 AM
From: Roger Haines
Subject: Capture all VMs from multiple vCenters CPU and MEM Shares Level
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
}
Original Message:
Sent: Oct 16, 2024 10:11 AM
From: Roger Haines
Subject: Capture all VMs from multiple vCenters CPU and MEM Shares Level
Hi James, Thank you very much for your response! Doesn't this line do the loop?
"foreach ($vc in $global:defaultviservers) {"
Original Message:
Sent: Oct 15, 2024 11:39 PM
From: James Dougherty
Subject: Capture all VMs from multiple vCenters CPU and MEM Shares Level
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
}
Original Message:
Sent: Oct 15, 2024 04:48 PM
From: Roger Haines
Subject: Capture all VMs from multiple vCenters CPU and MEM Shares Level
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
}