PoshCode Archive  Artifact [5167cbcf72]

Artifact 5167cbcf72b4e6d165aab28ea21b8610481eab67fe9af4f2d40e20f4c63c8fcf:

  • File Disconnect-VMHost.ps1 — part of check-in [b1e9bba63b] at 2018-06-10 14:26:17 on branch trunk — Disconnect a VMHost from vCenter Example using the VI API real world use Set-VMHost -State (user: glnsize size: 1799)

# encoding: ascii
# api: powershell
# title: Disconnect-VMHost
# description: Disconnect a VMHost from vCenter
  Example using the VI API real world use Set-VMHost -State
# version: 0.1
# type: function
# author: glnsize
# license: CC0
# function: Disconnect-VMHost
# x-poshcode-id: 901
# x-archived: 2009-03-03T09:33:37
#
#
#requires -version 2 
#requires -pssnapin VMware.VimAutomation.Core 
Function Disconnect-VMHost {
    <#
    .Summary
        Used to Disconnect a Connected host from vCenter.
    .Parameter VMHost
        VMHost to Disconnect to virtual center
    .Example
        Get-VMHost | Where-Object {$_.state -eq "Connected"} | Disconnect-VMHost
        
        Will Attempt to Disconnect any host that are currently Connected.
    .Example
        Disconnect-VMHost -Name ESX1.get-admin.local
        
        Will Disconnect ESX1 From 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)) {
            if ($pscmdlet.ShouldProcess($VMHostImpl.name)) {
                $VMHostImpl.DisconnectHost_Task()
            }
        }
    }
}