Information Technology Reference
In-Depth Information
login. If your account has vCenter access, no credentials are required. Many organizations fol-
low the practice of separate administrative accounts, and as such it is important to demonstrate
ways to protect credentials. To solve this problem, you can securely prompt for credentials in
your scripts and store them encrypted in a variable using the Get-Credential cmdlet. When
you call Get-Credential in a script, it will prompt the user for a username and password. It
then stores them in the pscredential object, which can then be used by Connect-VIServer as
follows:
$credential = Get-Credential
Connect-VIServer -server <vCenter Server Name> -Credential $credential
The best part of this method, aside from security, is that the $credential variable infor-
mation remains as long as your PowerCLI session is opened or until you replace it with new
information. This means you can have the same variable set to different credentials in different
instances of PowerCLI. This is ideal for running multiple scripts simultaneously on the same
system and prevents people from seeing your virtual environment credentials if they're not sup-
posed to.
You can also connect to multiple vCenter Server instances or ESXi hosts in the same session
by separating them with a comma:
Connect-VIServer -server vCenter1,vCenter2 -Credential $credential
Here is one i nal Connect-VIServer tip for vSphere administrators running multiple vCenter
Server instances in linked mode. If you have common permissions for an account across vCen-
ter servers in linked mode, you can connect to all of those instances with PowerCLI using
the -AllLinked parameter:
Connect-VIServer -server vCenter1 -Credential $credential -AllLinked:$true
Before we leave the Connect-VIServer cmdlet, it's important to know its counterpart,
Disconnect-VIServer. If you run Disconnect-VIServer by itself in the PowerCLI console, you
will prompted for verii cation that you want to disconnect from the server. You can press Enter
and it will disconnect from the active server (identii ed by VIServer). If you are connected to
multiple systems, this command will not disconnect from all of them by default. You can accom-
plish this by specifying the name of the systems you wish to disconnect from or using a wild-
card *. Use the -Confirm parameter to prevent being prompted to disconnect:
Disconnect-VIServer -Server vCenter1,vCenter2 -Confirm:$false
Disconnect-VIServer * -Confirm:$false
You may wish to verify what vCenter servers or ESXi hosts you are connected to. Do this
by simply typing in the variable $DefaultVIServers . You can also identify the current default
system by typing $DefaultVIServer . The default server is the one disconnected if no server is
specii ed when using Disconnect-VIServer.
Your First One-Liner: Reporting
The most common use of PowerCLI is reporting. A vSphere administrator can gather tremen-
dous amounts of data about their environment in a very short period of time. Previously, in
the discussion about the pipeline, we showed you how quickly you can identify the hosts in
your environment that are in maintenance mode. That small script is called a one-liner. One-
liners are scripts that can be written out and executed in a single line using the pipeline to pass
Search WWH ::




Custom Search