PoshCode Archive  Artifact [4788722289]

Artifact 4788722289b7e7131c801582bd4436c4653da45742854706d7fa2b3874db93c8:

  • File Connect-VMHost.ps1 — part of check-in [58aaeb2a3c] at 2018-06-10 14:26:19 on branch trunk — Reconnect a VMHost that has been disconnected from vCenter… Example using the VI API real world use Set-VMHost -State (user: glnsize size: 2070)

# encoding: ascii
# api: powershell
# title: Connect-VMHost
# description: Reconnect a VMHost that has been disconnected from vCenter… Example using the VI API real world use Set-VMHost -State
# version: 0.1
# type: function
# author: glnsize
# license: CC0
# function: Connect-VMHost
# x-poshcode-id: 902
# x-archived: 2009-03-03T09:33:53
#
#
#requires -version 2 
#requires -pssnapin VMware.VimAutomation.Core 
Function Connect-VMHost {
    <#
    .Summary
        Used to Connect a disconnected host to vCenter.
    .Parameter VMHost
        VMHost to reconnect to virtual center
    .Example
        Get-VMHost | Where-Object {$_.state -eq "Disconnected"} | Connect-VMHost
        
        Will Attempt to reconnect any host that are currently disconnected.
    .Example
        Connect-VMHost -Name ESX1.get-admin.local
        
        Will reconnect ESX1 to vCenter
    #>
    [CmdletBinding(
        SupportsShouldProcess=$True,
	    SupportsTransactions=$False,
	    ConfirmImpact="low",
	    DefaultParameterSetName="ByString"
	)]
    Param(
        [Parameter(
            Mandatory=$True,
            Valuefrompipeline=$true,
            ParameterSetName="ByObj"
        )]
        [VMware.VimAutomation.Client20.VMHostImpl[]]
        $VMHost,
        
        [Parameter(
            Mandatory=$True,
            Position=0,
            ParameterSetName="ByString"
        )]
        [string[]]
        $Name
    )
    Begin {
        IF ($Name) {
            $VMHost = $Name|%{ Get-VMHost -Name $_ }
        }
    }
    process {
        Foreach ($VMHostImpl in ($VMHost|Get-View)) {
            $ReconnectSpec = New-Object VMware.Vim.HostConnectSpec
            $ReconnectSpec.HostName = $VMHostImpl.Summary.Config.Name
            $ReconnectSpec.Port = $VMHostImpl.Summary.Config.Port
            $ReconnectSpec.Force = $true
            if ($pscmdlet.ShouldProcess($VMHostImpl.name)) {
                $VMHostImpl.ReconnectHost_Task($ReconnectSpec)
            }
        }
    }
}