Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning PowerCLI for VMware VSphere

You're reading from   Learning PowerCLI for VMware VSphere Automate your Vmware vSphere environment by learning how to install and use PowerCLI. This book takes a practical tutorial approach that will have you automating your daily routine tasks in no time.

Arrow left icon
Product type Paperback
Published in Feb 2014
Publisher
ISBN-13 9781782170167
Length 374 pages
Edition Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Robert van den Nieuwendijk Robert van den Nieuwendijk
Author Profile Icon Robert van den Nieuwendijk
Robert van den Nieuwendijk
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Learning PowerCLI
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Introduction to PowerCLI 2. Learning Basic PowerCLI Concepts FREE CHAPTER 3. Working with Objects in PowerShell 4. Managing vSphere Hosts with PowerCLI 5. Managing Virtual Machines with PowerCLI 6. Managing Virtual Networks with PowerCLI 7. Managing Storage with PowerCLI 8. Managing High Availability and Clustering with PowerCLI 9. Managing vCenter with PowerCLI 10. Reporting with PowerCLI Index

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

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 datacenters using the following command:

PowerCLI C:\> Get-Datacenter

The output of the preceding command is as follows:

Get-Datacenter : 12/19/2013 7:29:36 PM    Get-Datacenter        Youare not currently connected to any servers. Please connect firstusing 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 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:

Connect-VIServer [-Server] <String[]> [-Port <Int32>] [-Protocol <String>] [-Credential <PSCredential>] [-User <String>] [-Password <String>] [-Session <String>] [-NotDefault] [-SaveCredentials] [-AllLinked] [-Force] [<CommonParameters>]
Connect-VIServer -Menu [<CommonParameters>]

As you can see, the Connect-VIserver cmdlet has two parameter sets: Default and Menu. In the Default parameter set, the –Server parameter is required. 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 cmdlet:

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 will 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 a later section 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 BYDEFAULT 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 DefaultServerMode with the Get-PowerCLIConfiguration cmdlet:

PowerCLI C:\> Get-PowerCLIConfiguration

If you want to change the DefaultServerMode from single to multiple, you can do that with the Set-PowerCLIConfiguration cmdlet. The cmdlet has the following syntax:

Set-PowerCLIConfiguration [-ProxyPolicy <ProxyPolicy>][-DefaultVIServerMode <DefaultVIServerMode>][-InvalidCertificateAction <BadCertificateAction>] [-DisplayDeprecationWarnings [<Boolean>]] [-WebOperationTimeoutSeconds <Int32>] [-VMConsoleWindowBrowser <String>] [-Scope <ConfigurationScope>] [-WhatIf] [-Confirm] [<CommonParameters>]

Change the DefaultServerMode 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 is to supply your vCenter Server and ESXi servers with valid certificates. You can find information on how to do this in the VMware Knowledge Base article, Implementing CA signed SSL certificates with vSphere 5.x (2034833) at http://kb.vmware.com/kb/2034833.

If you don't have valid certificates, you can easily suppress the warning messages using the Set-PowerCLIConfiguration cmdlet with the following command:

PowerCLI C:\ > Set-PowerCLIConfiguration -InvalidCertificateAction Ignore

This 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: root,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                   Ignore                       True
    User                   Ignore                       True
AllUsers                    Unset 

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 a later section in this chapter.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image