Difference between revisions of "Getting Started (PowerCLI)"

Jump to navigation Jump to search
Updated Syntax Highlighting
(Updated Syntax Highlighting)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
== The Basics ==
== The Basics ==
Before being able to use any the following script you need to install...
Before being able to use any the following script you need to install...
* [http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx Windows PowerShell V1]
* [http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx Windows PowerShell V1]
* [http://support.microsoft.com/kb/968929 Windows Powershell V2]
* [http://support.microsoft.com/kb/968929 Windows Powershell V2] - Available for Win7 or Win2008
* [http://www.vmware.com/go/powershell VI Toolkit]
* [http://www.vmware.com/go/powershell PowerCLI] - Formerly known as the VI Toolkit
** Note that versions are broadly speaking, backwards compatible.  So even if you've only got the v4 vCentre's and ESX's, install the latest v5 version of PowerCLI


On the first run you need to allow the Toolkit to run properly by running (you need to run this command as an administrator, so if you're using Windows 7, for example, you'll need to start the Powershell console as an administrator, regardless of whether you're currently logged in as an admin)...
=== Set Execution Policy ===
On the first run you need to allow PowerCLI to run properly by running (you need to run this command as an administrator, so if you're using Windows 7, for example, you'll need to start the PowerShell console as an administrator, regardless of whether you're currently logged in as an admin)...
<source lang="powershell"> Set-ExecutionPolicy RemoteSigned </source>
<source lang="powershell"> Set-ExecutionPolicy RemoteSigned </source>


If you don't do this you'll get the following error when the PowerCLI Snap-In attempts to load into your session.
* '''...Initialize-PowerCLIEnvironment.ps1 cannot be loaded because the execution of scripts is disabled on this system. ...'''
If it takes a long time (minutes) for the PowerCLI console to start, see [[Getting_Started_(PowerShell)#Slow_Start-Up|Slow Start-Up]].
=== Connect to Infrastructure ===
Connect to the Virtual Centre (or ESX) server using the following command using your normal username and password (same as you'd use to log into the VI Client).  You will need access to the servers on TCP 443.
Connect to the Virtual Centre (or ESX) server using the following command using your normal username and password (same as you'd use to log into the VI Client).  You will need access to the servers on TCP 443.
<source lang="powershell"> Connect-VIServer -Server <server> -User <user> -Password <pass> </source>
<source lang="powershell"> Connect-VIServer -Server <server> -User <user> -Password <pass> </source>


Once connected you can do real work with the PowerCLI, to see what's available, login to https://server/mob.  This is effectively where you end up once you've completed the <code> Connect-VIServer </code> command.
Once connected you can do real work with the PowerCLI, to see what's available, login to https://server/mob.  This is effectively where you end up once you've completed the <syntaxhighlight lang="powershell" inline> Connect-VIServer </syntaxhighlight> command.
 
Try some of the following...
<source lang="powershell">
Get-Cluster
Get-VMHost
Get-VM
</source>


Be aware that PowerShell commands generally return objects, rather than text, and that the textual representation of the return object is often abbreviated for simplicity.  To see the entire return for a command, pipe the result into <code>Format-List</code>.  To complicate matters further, some return objects contain further objects, see examples below
Be aware that PowerShell commands generally return objects, rather than simple variables containing text or numbers.  So the textual representation of the return object is often abbreviated for simplicity.  To see the entire return for a command, pipe the result into <syntaxhighlight lang="powershell" inline>Format-List</syntaxhighlight>.  To complicate matters further, some return objects contain further objects, see examples below


<source lang="powershell">
<source lang="powershell">
Line 91: Line 105:
Id              : VirtualMachine-vm-697/4002
Id              : VirtualMachine-vm-697/4002
Name            : Network Adapter 3
Name            : Network Adapter 3
</source>
== Import into PowerShell ==
You can just run PowerCLI by running the shell directly.  In order to be able to use PowerCLI CmdLets in a normal PowerShell session you need to import the module in...
<source lang="powershell">
Add-PSSnapin VMware.VimAutomation.Core
</source>
</source>


== Installed Version ==
== Installed Version ==
To determine the version of PowerCLI currently installed use;
To determine the version of PowerCLI currently installed use;
* <code> Get-PowerCLIversion </code>
 
<source lang="powershell">Get-PowerCLIversion </source>


== Scheduling ==
== Scheduling ==

Navigation menu