PowerCLI

 View Only
  • 1.  Check and Disconnect session

    Posted Apr 18, 2018 02:36 PM

    Hi,

    Please help to how to check and disconnect a visession ?

    I would like to check and disconnect from vcenter1 before I connect to vcenter2

    disconnect-viserver -server vcen01 -force -confirm:$false



  • 2.  RE: Check and Disconnect session

    Posted Apr 18, 2018 03:12 PM

    Hi,

    Disconnect-VIServer -Server vcen01 -Force

    works for me.

    vSphere 5.5 Documentation Center



  • 3.  RE: Check and Disconnect session

    Posted Apr 18, 2018 03:51 PM

    Thank you for your reply.

    I would like to check before I disconnect the session, If the session is empty, I get error which I dont want to see.

    So looking for any one liner which can help :smileyhappy:



  • 4.  RE: Check and Disconnect session

    Posted Apr 18, 2018 04:23 PM

    Try with the ErrorAction

    Disconnect-VIServer -server vcen01 -Force -confirm:$false -ErrorAction SilentlyContinue



  • 5.  RE: Check and Disconnect session

    Posted Apr 18, 2018 04:32 PM

    Try with the ErrorAction

    Disconnect-VIServer -server vcen01 -Force -confirm:$false -ErrorAction SilentlyContinue

    Second this.

    PowerCLI Error Handling



  • 6.  RE: Check and Disconnect session

    Posted Apr 19, 2018 06:35 AM

    Hi LucD,

    I want use this with the script before I start, I wanted to disconnect the previous sessions if any, so When I tried the suggested command, I am getting the error as below

    Is there a way to check if we are connected to vcenter without using connect-viserver command and like to disconnect it before I connect to different vcenter from script

    where if the session is connected, it should disconnect, or if the session is not connected, it should exit

    please help

    function dis {

    $vc = connect-viserver "aurvpvcntrapp01"

            if($vc.IsConnected)

            {

            Write-host "Connected To : $vc"

            Disconnect-VIServer $vc -force -Confirm:$false -WarningAction SilentlyContinue -ErrorAction SilentlyContinue

            }

            else

            {

              exit

            }

           }

    dis



  • 7.  RE: Check and Disconnect session
    Best Answer

    Posted Apr 19, 2018 07:13 AM

    You could do

    $vcName = 'vcsa.domain'

    $global:DefaultVIServers | Where{$_.Name -eq $vcName} | %{

        Disconnect-VIServer -Server $_.Name -Confirm:$false

    }

    Connect-VIServer -Server $vcName



  • 8.  RE: Check and Disconnect session

    Posted Apr 23, 2018 11:12 AM

    Thank you very much :smileyhappy: