Hello,
I recently wrote a script with the help of LucD to be able to scan a vCenter if it has multiple datacenters or not. If it has more than 1 it prompts to choose, if not then the script moves forward. In this instance I'm having an issue when there's more than 1 datacenter. For whatever reason it works fine on my vCenter (6.5), but when running on a 6.0 vCenter it throws up an error, but still completes. PowerCLI version is 10.1.1 on the machine I test on and the one that the script is being run from. Both are Windows 10 RS4 and have PowerShell 5.1 as well. Below is the error:
Get-Cluster : 11/15/2018 4:29:24 PM Get-Cluster VMHost parameter: Could not find any of the objects specified by
name.
At C:\Users\admin\Desktop\GenSeriallyMigrateFromHostToHost.ps1:44 char:33
+ Get-Datacenter -Name $dc.Name | Get-Cluster | sort Name | Format-Tabl ...
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (VMware.VimAutom...VMHost[] VMHost:RuntimePropertyInfo) [Get-Cluster], O
bnRecordProcessingFailedException
+ FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ObjectNotFoundCritical,VMware.VimAutomation.ViCore
.Cmdlets.Commands.GetCluster
Get-Cluster : 11/15/2018 4:29:25 PM Get-Cluster VMHost parameter: Could not find any of the objects specified by
name.
At C:\Users\admin\Desktop\GenSeriallyMigrateFromHostToHost.ps1:44 char:33
+ Get-Datacenter -Name $dc.Name | Get-Cluster | sort Name | Format-Tabl ...
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (VMware.VimAutom...VMHost[] VMHost:RuntimePropertyInfo) [Get-Cluster], O
bnRecordProcessingFailedException
+ FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ObjectNotFoundCritical,VMware.VimAutomation.ViCore
.Cmdlets.Commands.GetCluster
Get-Cluster : 11/15/2018 4:29:25 PM Get-Cluster VMHost parameter: Could not find any of the objects specified by
name.
At C:\Users\admin\Desktop\GenSeriallyMigrateFromHostToHost.ps1:44 char:33
+ Get-Datacenter -Name $dc.Name | Get-Cluster | sort Name | Format-Tabl ...
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (VMware.VimAutom...VMHost[] VMHost:RuntimePropertyInfo) [Get-Cluster], O
bnRecordProcessingFailedException
+ FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ObjectNotFoundCritical,VMware.VimAutomation.ViCore
.Cmdlets.Commands.GetCluster
Below is the initial code, where the last line is line 44 that's referenced in the error:
$commands = { #This is the top of the loop, if you hit C at the end of the script it will take you back here
$dc = Get-Datacenter #This gets the datacenters, if more than 1 it will ask you which one to choose, if only one it will move forward in the script
if ($dc.Count -gt 1) {
Write-Host "I found $($dc.Count) datacenters"
Write-Host "Choose a datacenter"
1..($dc.Count) | %{
Write-Host "$_ - $($dc[$_ - 1].Name)"
}
$answer = 0
while (1..($dc.Count) -notcontains $answer){
$answer = Read-Host -Prompt "Select a datacenter (1-$($dc.Count))"
}
}
Get-Datacenter -Name $dc.Name | Get-Cluster | sort Name | Format-Table #Gets the clusters in the datacenter
So is it something that needs to be changed somehow to accommodate different vCenter versions? I thought this script would work pretty well between versions of vCenter/ESXi. Is there that much of a difference? I'm tempted to build my own setup that has 6.0, 6.5 and 6.7 just to see how differently the scripts behave.
Does this version give you the same error?
$dc = @(Get-Datacenter)
if ($dc.Count -gt 1) {
Write-Host "I found $($dc.Count) datacenters"
Write-Host "Choose a datacenter"
$answer = 0
1..($dc.Count) | %{
Write-Host "$_ - $($dc[$_ - 1].Name)"
}
while (1..($dc.Count) -notcontains $answer){
$answer = Read-Host -Prompt "Select a datacenter (1-$($dc.Count))"
}
}
else{
$answer = 1
}
Get-Datacenter -Name $dc[$answer - 1].Name | Get-Cluster | sort Name | Format-Table #Gets the clusters in the datacenter
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Does this version give you the same error?
$dc = @(Get-Datacenter)
if ($dc.Count -gt 1) {
Write-Host "I found $($dc.Count) datacenters"
Write-Host "Choose a datacenter"
$answer = 0
1..($dc.Count) | %{
Write-Host "$_ - $($dc[$_ - 1].Name)"
}
while (1..($dc.Count) -notcontains $answer){
$answer = Read-Host -Prompt "Select a datacenter (1-$($dc.Count))"
}
}
else{
$answer = 1
}
Get-Datacenter -Name $dc[$answer - 1].Name | Get-Cluster | sort Name | Format-Table #Gets the clusters in the datacenter
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
That seems to be working on vCenter 6.0 and 6.5. Thanks for the help. Can you explain what the changes are exactly and why they're needed? I'd just like to understand a little better.
The major difference is that I initialised the $answer variable outside the loop where the script prints all the datacenters (in case there is more than 1)
The script first lists all the datacenters (with a number before them), sets the $answer to 0 and then waits in the While loop until a number that falls between 1 and the number of found datacenters, is entered.
The 2nd difference is that I capture the datacenters in an array, even if there is only 1 (that's what the @() does).
This allows me to address the selected datacenter with $dc[$answer - 1], even if there is only 1 datacenter.
In your script the expression $dc.Name only works when there is 1 datacenter in $dc, if there are more the expression $dc.Name will return the names of all the datacenters in $dc.
I hope that clarified a bit.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference