VMware Cloud Community
KlinikenLB
Contributor
Contributor
Jump to solution

Simultaneously connected to vCenter and a ESX Host

Hi all there,

I have a PowerCLI script which configures my new ESXi hosts completly: from local users (e.g. New-VMHostAccount) to the local networks (e.g. New-VirtualPortGroup). In this script there is a Connect-VIServer at the beginning to connect to the new host I want to configure.

Meanwhile I have virtual distributed switches (vDS) in my vCenter. So I need e.g. the Add-VDSwitchVMHost to configure the networks to the new host. But for this cmdlet I have to be connected to my vCenter.

I have learned that if I'm connected to more hosts or vCenters, my cmdlets run again all actually connected hosts and vCenters. I'm worried about this.

The simple safe way to solve this could be first run all the cmdlets to the host, then disconnect here and connect to vCenter, and then run all cmdlets to vCenter.

Is there a better secure way to solve this? And how do I find out which cmdlet works again vCenter andf which one against a host?

Thx and regards

Dietmar

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Yes, use the Server parameter, which is available on most PowerCLI cmdlets.
It allows you to specify against which specific vSphere server (ESXi or VCSA) the cmdlet should run.


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

View solution in original post

0 Kudos
15 Replies
LucD
Leadership
Leadership
Jump to solution

Yes, use the Server parameter, which is available on most PowerCLI cmdlets.
It allows you to specify against which specific vSphere server (ESXi or VCSA) the cmdlet should run.


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

0 Kudos
KlinikenLB
Contributor
Contributor
Jump to solution

Hi LucD,

thanks for answer. I will check all the cmdlets in my script if there is a -server parameter an will add this.

A bit difficult is this with cmdlets like Get-VMHostNetworkAdapter : there is a -server and a -vmhost parameter. I don't understand the difference. I only see (when I'm connected to my VirtualCenter and to my new host) that with -server I see my vmnic0 once and with -vmhost I see my vmnic0 twice.

It is the same difficulty with the discription of get-vmhost parameters -name and -server : until now I used the -name parameter, where I get my new host two times when I'm connected simultaneously. Because of your hint with -server I found out that with this parameter my new host is only shown once 🙂

Regards, Dietmar

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Server parameter indicates to which vSPhere server the cmdlet should be directed.
You will still need to provide the VMHost parameter.

If if are connected to a vCenter and an ESXi node, the following will return the same object twice.

Get-VMHost -Name MyEsx | Get-NetWorkAdapter

when you add the Server parameter, only 1 object will be returned

Get-VMHost -Name MyEsx -Server MyvCenter | Get-NetworkWorkAdapter

# or

Get-VMHost -Name MyEsx -Server MyvEsx | Get-NetworkWorkAdapter


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

0 Kudos
KlinikenLB
Contributor
Contributor
Jump to solution

Very fast answer 🙂

And yes, you are right:

 

PS C:\> Get-VMHost -server $myhost | Get-VMHostNetWorkAdapter -name vmnic0

Name       Mac               DhcpEnabled IP              SubnetMask      DeviceName
----       ---               ----------- --              ----------      ----------
vmnic0     98:f2:b3:28:9d:5c False                                           vmnic0


PS C:\> Get-VMHost -name $myhost | Get-VMHostNetWorkAdapter -name vmnic0

Name       Mac               DhcpEnabled IP              SubnetMask      DeviceName
----       ---               ----------- --              ----------      ----------
vmnic0     98:f2:b3:28:9d:5c False                                           vmnic0
vmnic0     98:f2:b3:28:9d:5c False                                           vmnic0


PS C:\>

 

0 Kudos
KlinikenLB
Contributor
Contributor
Jump to solution

It's not so easy as it seems.

Add-VmHostNtpServer

Get-VMHostFirewallException

Set-VMHostFirewallException

and so on.

Parameter -VMHost is required. Must this be the host object from Get-VMHost -Server $myHost ?

Parameter -Server is optional. I think this is the string of my new $myHost. But for what do I need this?

Or is this the famous exception where I do not need -Server? 😉

0 Kudos
LucD
Leadership
Leadership
Jump to solution

On those cmdlets you need to provide a VMHost parameter.
The value you pass there should be the one you obtain with Get-VMHost with the Server parameter.

Note that some cmdlets need to be run against an ESXi node connection, others can be run against a vCenter connection.
But that is indicated in the cmdlet reference page or the cmdlet help


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

0 Kudos
KlinikenLB
Contributor
Contributor
Jump to solution

Yes, this is my current problem. I have to be connected to vCenter and my new host simultaneously. But some cmdlets (e.g. local users and storage) must run against the new host. And other cmdlets (e.g. vDS for this new host) must run against vCenter.

 

To your answer: you mean that in this cmdlets I do not need the -Server parameter because it is usually identical to the -VMHost parameter?

0 Kudos
KlinikenLB
Contributor
Contributor
Jump to solution

Another funny behaviour:

Get-EsxCli -VMHost accepts also the string in my $myHost variable.

But if I understand it right it should only accept the object from Get-VMHost -server $myHost .

I will correct this "error" in my script.

Things like this don't make it easy for a newby like me ...

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, several cmdlets accept the name of an object.
That concept is named OBN, Object By Name.

I understand this might be confusing in the beginning.
No need to correct your script, it is a feature


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

0 Kudos
KlinikenLB
Contributor
Contributor
Jump to solution

I see I learn a lot today 🙂 Thx for your support and patience.

And if the (e.g.)

 

Get-VMHostNetworkAdapter -Physical -Name vmnic0, vmnic1 -server $myhost

 

and

 

Get-VMHostNetworkAdapter -Physical -Name vmnic0, vmnic1 -vmhost $hostobj

 

have the same result, what should I prefer?

Or are these the typical many ways to Rome?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

In result those 2 might show the same results, but behind the covers the Server parameter is more specific.
If you often connect to multiple vSphere servers (vCenter and ESXi), the Server parameter allows you to specify against which of the connected vSphere servers the cmdlet should run.
Without the Server parameter, the result will depend on how you have configured the DefaultVIServerMode (possible values Single of Multiple).
Check the Description of the DefaultVIServerMode parameter on the Set-PowerCLIConfiguration cmdlet.


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

0 Kudos
KlinikenLB
Contributor
Contributor
Jump to solution

 

Then I will prefer -Server where available 🙂

 

 

PS C:\> Get-PowerCLIConfiguration

Scope    ProxyPolicy     DefaultVIServerMode InvalidCertificateAction  DisplayDeprecationWarnings WebOperationTimeout
                                                                                                  Seconds
-----    -----------     ------------------- ------------------------  -------------------------- -------------------
Session  UseSystemProxy  Multiple            Ignore                    True                       300
User                     Multiple            Ignore
AllUsers

 

 

 

0 Kudos
KlinikenLB
Contributor
Contributor
Jump to solution

So this code should be fine?

 

# Host an RKH-Backup anschließen und Uplinks verbinden
Add-VDSwitchVMHost -VDSwitch "RKH-Backup" -VMHost $myHost -Server $myVC
$HostNICs = Get-VMHostNetworkAdapter -Name vmnic6, vmnic7 -Physical -Server $myHost
Add-VDSwitchPhysicalNetworkAdapter -DistributedSwitch "RKH-Backup" -VMHostPhysicalNic $HostNICs -Server $myHost -Confirm:$False
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That looks ok


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

0 Kudos
KlinikenLB
Contributor
Contributor
Jump to solution

And it works with two connected hosts and a vCenter 🙂

Great, thank you.

0 Kudos