These days I'm speding some time learning VMware vSphere PowerCLI. I think vSphere PowerCLI is a great tool for automating tasks among vSphere environments. It's so useful to automate repetitive tasks, it allows IT admins to save huge amounts of time performing common tasks using scripts.
To start using VMware vSphere PowerCLI simply go here:
vSphere PowerCLI: Windows PowerShell interface for managing vSphere
You need to download and install VMware vSphere PowerCLI on your Windows machine in order to start scripting.
Here is the official vSphere PowerCLI Documentation
Another really useful PowerCLI document to read is Getting Started with PowerCLI 4.1 – Automating Your vSphere Environment which contains a lot of useful commands to perform basic tasks.
Last link I would like to share is the VMware PowerCLI 5.1 Poster which presents all PowerCLI cmdlets in a single panel grouping them by function (Network Operations, Logs/Reporting, etc.)
So, let's start with PowerCLI 101 post...
Connecting/Disconnecting to vCenter Server:
To start using PowerCLI you need to connect to a vCenter Server or to an ESXi host
Connect-VIServer -Server <VCENTER_IP_ADDRESS> -User <USERNAME> -Password <PASSWORD>
Example: Connect-VIServer -Server 192.168.116.155 -User root -Password vmware
To disconnect:
Disconnect-VIServer
Available Commands:
To list all available commands:
Get-Command
Example: Get-Command *VM* -> To list all commands containing word "VM" in their name.
To get more infos on a command
Get-Help <COMMAND_NAME> -Detailed
Selecting Columns:
<cmdlet> | Select-Object <COLUMNS_TO_SHOW>
Example: Get-VM | Select-Object Name, PowerState -> To display only name and power state of every VM
Prevent truncated columns:
If a column contains many data it could happen that output it's truncated. To prevent this:
<cmdlet> | Select-Object <COLUMNS_TO_SHOW> | Ft -autosize
Redirecting Output:
<cmdlet> > <OUTPUT_FILE>
Example: Get-VM > C:\List_of_VMs.txt -> To create a file named "List_of_VMs.txt" which contains the list of all our VMs
Exporting CSVs:
<cmdlet> | Export-Csv <OUTPUT_FILE>
Example: Get-VM | Export-Csv C:\List_of_VMs.csv -> To export to CSV infos about VMs
Combining Cmdlets:
<cmdlet> | <cmdlet>
Example: Get-VMHost esx1.testdomain.com | Get-VM -> To show all VMs running on esx1.testdomain.com host
Nested Expressions:
<cmdlet> | Select-Object @{N="<COLUMN_NAME>"; E={<EXPRESSION>}}
Example: Get-VM | Select-Object Name, @{N="Datastore"; E={$_ | Get-Datastore}} -> Display VM name and datastore on which each VM resides
In the next post I will provide some PowerCLI useful scripts.
That's all!!
Nessun commento:
Posta un commento