Connecting and disconnecting servers
Before you can do useful things with PowerCLI, you have to connect to a vCenter Server or an ESXi server. And if you are finished, it is a good practice to disconnect your session. We will discuss how to do this in the following sections Connecting to a server, Connecting to multiple servers, Suppressing certificate warnings , and Disconnecting from a server.
Connecting to a server
If you are not connected to a vCenter or an ESXi server, you will get an error message if you try to run a PowerCLI cmdlet. Let's try to retrieve a list of all of your data centers using the following command:
PowerCLI C:\> Get-Datacenter
The output of the preceding command is as follows:
Get-Datacenter : 1/7/2017 1:37:17 PM Get-Datacenter You are not currently connected to any servers. Please connect first using a Connect cmdlet. At line:1 char:1 + Get-Datacenter + ~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (:) [Get- Datacenter], ViServerConnectionException + FullyQualifiedErrorId : Core_BaseCmdlet_NotConnectedError, VMware.VimAutomation.ViCore.Cmdlets. Commands.GetDatacenter
You can see that this gives an error message. You first have to connect to a vCenter Server or an ESXi server using the Connect-VIServer
cmdlet. If you have a vCenter Server, you only need to connect to the vCenter Server and not to the individual ESXi servers. It is possible to connect to multiple vCenter Servers or ESXi servers at once. The Connect-VIServer
cmdlet has the following syntax. The syntax contains two parameter sets. The first parameter set is the default:
Connect-VIServer [-Server] <String[]> [-Port <Int32>] [-Protocol
<String>] [-Credential <PSCredential>] [-User <String>] [-Password
<String>] [-Session <String>] [-NotDefault] [-SaveCredentials]
[-AllLinked] [-Force] [<CommonParameters>]
In the Default
parameter set, the -Server
parameter is required. The second parameter set can be used to select a server from a list of recently connected servers:
Connect-VIServer -Menu [<CommonParameters>]
In the Menu
parameter set, the -Menu
parameter is required. You cannot combine parameters from the Default
parameter set with the Menu
parameter set.
Let's first try to connect to a vCenter Server with the following command:
PowerCLI C:\> Connect-VIServer -Server 192.168.0.132
192.168.0.132
is the IP address of the vCenter Server in my home lab. Replace this IP address with the IP address or DNS name of your vCenter or ESXi server.
The preceding command will pop up a window in which you have to specify server credentials to connect to your server if your Windows session credentials don't have rights on your server. Enter values for User name and Password and click on OK.
If you specified valid credentials, you would get output similar to the following:
Name Port User ---- ---- ---- 192.168.0.132 443 root
You can also specify a username and password on the command line as follows:
PowerCLI C:\> Connect-VIServer -Server 192.168.0.132 -User admin
-Password pass
You can also save the credentials in a variable with the following command:
PowerCLI C:\> $Credential = Get-Credential
The preceding command will pop up a window in which you can type the username and password.
You can now use the $Credential
variable to connect to a server using the -Credential
parameter, as follows:
PowerCLI C:\> Connect-VIServer -Server 192.168.0.132 -Credential
$Credential
You can also use the PowerCLI credential store. This will be discussed in the Using the credential store section, later in this chapter.
The default protocol that the Connect-VIServer
cmdlet uses is HTTPS. If you want to make a connection with the HTTP protocol, you can do that with the following command:
PowerCLI C:\> Connect-VIServer -Server 192.168.0.132 -Protocol HTTP
If you have multiple vCenter Servers in Linked Mode, you can use the Connect-VIServer -AllLinked
parameter to connect all of these vCenter Servers at once, as follows:
PowerCLI C:\> Connect-VIserver -Server 192.168.0.132 -Credential
$Credential -AllLinked
The Connect-VIServer -Menu
command gives you a list of previously connected servers from which you can pick one, as shown in the following command line:
PowerCLI C:\> Connect-VIServer -Menu Select a server from the list (by typing its number and pressing Enter): [1] 192.168.0.132 [2] 192.168.0.133
Type the number of the server you want to connect to.
Connecting to multiple servers
It is possible in PowerCLI to connect to multiple servers at once. You can do this by specifying more than one server, as follows:
PowerCLI C:\> Connect-VIServer -Server vCenter1,vCenter2
The first time you try to do this, you will get the following message:
Working with multiple default servers? Select [Y] if you want to work with more than one default servers. In this case, every time when you connect to a different server using Connect-VIServer, the new server connection is stored in an array variable together with the previously connected servers. When you run a cmdlet and the target servers cannot be determined from the specified parameters, the cmdlet runs against all servers stored in the array variable. Select [N] if you want to work with a single default server. In this case, when you run a cmdlet and the target servers cannot be determined from the specified parameters, the cmdlet runs against the last connected server. WARNING: WORKING WITH MULTIPLE DEFAULT SERVERS WILL BE ENABLED BY DEFAULT IN A FUTURE RELEASE. You can explicitly set your own preference at any time by using the DefaultServerMode parameter of Set-PowerCLIConfiguration. [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):
Press Enter or type Y to work with multiple default servers.
As the message says, you can always connect to multiple servers, but your commands will only work against the last server you connected to unless you have enabled working with multiple servers.
You can see the current value of DefaultVIServerMode
with the Get-PowerCLIConfiguration
cmdlet:
PowerCLI C:\> Get-PowerCLIConfiguration Scope ProxyPolicy DefaultVIServerMode InvalidCertificateAction ----- ----------- ------------------- ------------------------ Session UseSystemProxy Multiple Unset User Multiple AllUsers
If you want to change DefaultVIServerMode
from single to multiple, you can do that with the Set-PowerCLIConfiguration
cmdlet. This cmdlet has the following syntax:
Set-PowerCLIConfiguration [-ProxyPolicy <ProxyPolicy>]
[-DefaultVIServerMode <DefaultVIServerMode>] [-InvalidCertificateAction
<BadCertificateAction>] [-ParticipateInCeip <Boolean>]
[-CEIPDataTransferProxyPolicy <ProxyPolicy>] [-
DisplayDeprecationWarnings <Boolean>] [-WebOperationTimeoutSeconds
<Int32>] [-VMConsoleWindowBrowser <String>] [-Scope
<ConfigurationScope>] [-WhatIf] [-Confirm] [<CommonParameters>]
You can change DefaultVIServerMode
from single to multiple with the following command:
PowerCLI C:\> Set-PowerCLIConfiguration -DefaultVIServerMode Multiple
-Scope User
All of the servers that you are currently connected to are stored in the variable $global:DefaultVIServers
. If DefaultVIServerMode
is set to multiple
, your PowerCLI cmdlets will run against all servers stored in the $global:DefaultVIServers
variable.
The last server you are connected to is stored in the variable $global:DefaultVIServer
. If DefaultVIServerMode
is set to single
, your PowerCLI cmdlets will only run against the server stored in the $global:DefaultVIServer
variable.
Suppressing certificate warnings
If your vCenter Server does not have valid server certificates, the Connect-VIserver
cmdlet will display some warning messages, as shown in the following screenshot:
It is a good practice to supply your vCenter Server and ESXi servers with certificates signed by a certificate authority (CA). You can find information on how to do this in the VMware Knowledge Base article, Replacing default certificates with CA signed SSL certificates in vSphere 6.x (2111219) at http://kb.vmware.com/kb/2111219.
If you don't have valid certificates, you can suppress the warning messages using the Set-PowerCLIConfiguration
cmdlet with the following command:
PowerCLI C:\ > Set-PowerCLIConfiguration -InvalidCertificateAction
Ignore
The preceding command will modify InvalidCertificationAction
in the AllUsers
scope. You have to run this command using the Run as Administrator PowerCLI session. Otherwise, you will get the following error message:
Set-PowerCLIConfiguration : Only administrators can change settings for all users. At line:1 char:1 + Set-PowerCLIConfiguration -InvalidCertificateAction Ignore + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Set-PowerCLIConfiguration], InvalidArgument + FullyQualifiedErrorId : VMware.VimAutomation.ViCore.Types.V1. ErrorHandling.InvalidArgument, VMware. VimAutomation.ViCore.Cmdlets.Commands. SetVIToolkitConfiguration
Disconnecting from a server
To disconnect from a vSphere server, you have to use the Disconnect-VIServer
 cmdlet. The Disconnect-VIServer
cmdlet has the following syntax:
Disconnect-VIServer [[-Server] <VIServer[]>] [-Force]
[-WhatIf] [-Confirm] [<CommonParameters>]
To disconnect all of your server connections, type the following command:
PowerCLI C:\> Disconnect-VIServer -Server * -Force
The output of the preceding command is as follows:
Confirm Are you sure you want to perform this action? Performing operation "Disconnect VIServer" on Target "User: VSPHERE.LOCAL\Administrator, Server: 192.168.0.132, Port: 443". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
Type Y or Enter to disconnect from the server.
If you don't want to be prompted with Are you sure you want to perform this action?, you can use the -Confirm:$false
option as follows:
PowerCLI C:\> Disconnect-VIServer -Server * -Force
-Confirm:$false
It may be that you want to disconnect only one session and not all. In that case, specify the server name or IP address of the server you want to disconnect. The following command only disconnects the latest session from server 192.168.0.132
:
PowerCLI C:\> Disconnect-VIServer -Server 192.168.0.132
Disconnecting one or more sessions will also change the value of the $global:DefaultVIServers
and $global:DefaultVIServer
variables.
Retrieving the PowerCLI configuration
To see the current setting of InvalidCertificationAction
, you can use the Get-PowerCLIConfiguration
cmdlet. The syntax of this cmdlet is as follows:
Get-PowerCLIConfiguration [-Scope <ConfigurationScope>]
[<CommonParameters>]
The following example will retrieve the PowerCLI configuration and shows the InvalidCertificateAction
value for all scopes:
PowerCLI C:\> Get-PowerCLIConfiguration | >> Select-Object -Property Scope, InvalidCertificateAction, DisplayDeprecationWarnings | >> Format-Table -AutoSize >> Scope InvalidCertificateAction DisplayDeprecationWarnings ----- ------------------------ -------------------------- Session Unset True User AllUsers
As you can see in the output, there are three different scopes for which you can modify the PowerCLI configuration: Session
, User
, and AllUsers
. The Set-PowerCLIConfiguration
cmdlet will modify the AllUser
scope if you don't specify a scope.
The DisplayDeprecationWarnings
property shown in the preceding output will be discussed in the section,Â
Suppressing deprecated warnings
, later in this chapter.