If you have ever changed the vCenter server certificates, you’ve experienced having all your hosts disconnected from vCenter. I couldn’t imagine reconnecting them one at a time… You could do this all natively in PowerCLI, but that would require you to fully remove and then add the hosts. That is very inconvenient, and almost as much trouble as doing it by hand… In this case it is both faster and easier to just use the native vSphere API.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<span style="color: #006400;"># Get the hostsystem object for every host currently disconnected.</span> <span style="color: #ff4500;">$VMhosts</span> <span style="color: #a9a9a9;">=</span> <span style="color: #0000ff;">Get-View</span> <span style="color: #000080;">-ViewType</span> <span style="color: #8b0000;">'Hostsystem'</span> <span style="color: #000000;">` </span> <span style="color: #000080;">-Property</span> <span style="color: #8b0000;">'name'</span> <span style="color: #000000;">` </span> <span style="color: #000080;">-Filter</span> <span style="color: #000000;">@{</span><span style="color: #8b0000;">"Runtime.ConnectionState"</span><span style="color: #a9a9a9;">=</span><span style="color: #8b0000;">"disconnected"</span><span style="color: #000000;">}</span> <span style="color: #00008b;">Foreach</span> <span style="color: #000000;">(</span><span style="color: #ff4500;">$VMhost</span> <span style="color: #00008b;">in</span> <span style="color: #ff4500;">$VMhosts</span><span style="color: #000000;">)</span> <span style="color: #000000;">{</span> <span style="color: #006400;"># Create a reconnect spec</span> <span style="color: #ff4500;">$HostConnectSpec</span> <span style="color: #a9a9a9;">=</span> <span style="color: #0000ff;">New-Object</span> <span style="color: #8a2be2;">VMware.Vim.HostConnectSpec</span> <span style="color: #ff4500;">$HostConnectSpec</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">hostName</span> <span style="color: #a9a9a9;">=</span> <span style="color: #ff4500;">$VMhost</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">name</span> <span style="color: #ff4500;">$HostConnectSpec</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">userName</span> <span style="color: #a9a9a9;">=</span> <span style="color: #8b0000;">'root'</span> <span style="color: #ff4500;">$HostConnectSpec</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">password</span> <span style="color: #a9a9a9;">=</span> <span style="color: #8b0000;">'PassWord'</span> <span style="color: #006400;"># Reconnect the host</span> <span style="color: #ff4500;">$taskMoRef</span> <span style="color: #a9a9a9;">=</span> <span style="color: #ff4500;">$VMhost</span><span style="color: #a9a9a9;">.</span><span style="color: #000000;">ReconnectHost_Task</span><span style="color: #000000;">(</span><span style="color: #ff4500;">$HostConnectSpec</span><span style="color: #000000;">)</span> <span style="color: #006400;"># optional, but i like to return a task object, that way I can </span> <span style="color: #006400;"># easily integrate this into a pipeline later if need be.</span> <span style="color: #0000ff;">Get-VIObjectByVIView</span> <span style="color: #000080;">-MORef</span> <span style="color: #ff4500;">$taskMoRef</span> <span style="color: #000000;">}</span> |
~Glenn