Hi,
I am trying to build a configuration script for new hosts. I have multiple vCenters in the environment and I want to add an NFS datastore dependent on the vCenter. For example if my host is connected to VC1, add NFS1, if host is connected to VC2 add NFS2, etc.
Thanks for the help!
If that ESXi node is already added to a vCenter and your DNS is set up correctly, then you can find out the vCenter, while only being connected to that ESXi node.
$esx = Get-VMHost
$vc = (Resolve-DnsName -Name $esx.ExtensionData.Summary.ManagementServerIp).NameHost
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could do something like this
$esxName = 'MyEsx'
$esx = Get-VMHost -Name $esxName
$vc = ([uri]$esx.ExtensionData.Client.ServiceUrl).Host
switch ($vc.Split('.')[0]) {
'vCenter1' {
New-Datastore -VMHost $esx -Nfs -Name 'name' -NfsHost $nfs1 -Path $path1 -Confirm:$false -Server $vc
}
'vCenter2' {
New-Datastore -VMHost $esx -Nfs -Name 'name' -NfsHost $nfs2 -Path $path2 -Confirm:$false -Server $vc
}
Default {
Write-Host "Unknown vCenter $vc for ESXi node $esxName"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Thanks for the reply.
I plugged my host name into the script to test. When I typed $vc to see what the variable returned, it did not return the vCenter name, it returned the esx host name. I need the vC.
Am I doing something wrong?
Let me add something: I'm connecting ONLY to the host, not to the vCenter when I run the script. Do I need to be connected to the vCenter in order to run the script?
Yes, the snippet assumes you are connected to one or more vCenters
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If that ESXi node is already added to a vCenter and your DNS is set up correctly, then you can find out the vCenter, while only being connected to that ESXi node.
$esx = Get-VMHost
$vc = (Resolve-DnsName -Name $esx.ExtensionData.Summary.ManagementServerIp).NameHost
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That's it! While I do not have the setup script in front of me at the moment, I remember there was a part of it where I had to log directly into the host and not vCenter in order to run a command. This made me think the best way to set up these hosts was individually -- just provide a hostname and let it do the rest. The missing component was the NFS share setup which is based on whatever vCenter managed the host.
You always come through for us. Thank you so much for your knowledge and help!
Hi LucD,
Still having a hard time getting this together in a loop. Why does this work....
$creds = Get-Credential
$esxName = "esx01.domain.com"
connect-viserver $esxName -Credential $creds
$esx = Get-VMHost -Name $esxName
$vc = (Resolve-DnsName -Name $esx.ExtensionData.Summary.ManagementServerIp).NameHost
Write-host "The hostname is $esx"
Write-host "The vCenter name is $vc"
Disconnect-VIServer * -Force -Confirm:$False
Write-host $esx has been disconnected
But this doesn't--?
$creds = Get-Credential
$esxName = @("esx01.domain.com" , "esx02.domain.com")
foreach ($esx in $esxName){
connect-viserver $esx -Credential $creds
Get-VMHost -Name $esx
$vc = (Resolve-DnsName -Name $esx.ExtensionData.Summary.ManagementServerIp).NameHost
Write-host "The hostname is $esx"
Write-host "The vCenter name is $vc"
Disconnect-VIServer * -Force -Confirm:$False
Write-host $esx has been disconnected
}
I get the following error:
Resolve-DnsName : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At line:5 char:30
+ ... e-DnsName -Name $esx.ExtensionData.Summary.ManagementServerIp).NameHo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Resolve-DnsName], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.DnsClient.Commands.ResolveDnsName
Thank you again.
Because in the 1st snippet, the $esx variable contains an object returned by Get-VMHost, and in the 2nd snippet, the $esx variable contains a String.
The property is only available in the object.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I understand the concept, but don't get how we know that one is considered an object and one is considered a string.
Based on your example, I was able to get it to work by changing it to:
$creds = Get-Credential
$esxName = @("esx01.domain.com" , "esx02.domain.com")
foreach ($esx in $esxName){
connect-viserver $esx -Credential $creds
$esxObject = Get-VMHost -Name $esx
$vc = (Resolve-DnsName -Name $esxObject.ExtensionData.Summary.ManagementServerIp).NameHost
Write-host "The hostname is $esx"
Write-host "The vCenter name is $vc"
Disconnect-VIServer * -Force -Confirm:$False
Write-host $esx has been disconnected
}
Are there any simple rules or suggestions which would help me figure this out in the future?
Thank you.
One way to check what is in a variable, is to pipe it to the Get-Member cmdlet.
The first line of the output will tell you what the type is.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference