Since VMware 6.0 I have started to use VMware PowerCli module to automate task and checks that I do daily or for large task that would take a long time to do manually. I am going to go through installing PowerCli and some of the useful command and script that can be use to check VMware.
To install PowerCli there are some pre-req’s
OS Type |
64-Bit
|
Server |
|
Workstation |
|
- Windows PowerShell 3.0, 4.0, 5.0, or 5.1
- .NET Framework 4.5, 4.5.x, 4.6, or 4.6.x
I would recommend installing the latest version of PowerShell which is currently 5.1
To check current version of PowerShell run $PSVersionTable
To install the latest version install the latest Windows Management Framework 5.1 link to download page is below.
https://www.microsoft.com/en-us/download/details.aspx?id=54616
Step 1 is to install PowerCli
The old PowerCli was a PowerShell snap in and required downloading an exe to install, the new version is module based and can be installed directly from the PowerShell console. To install run the below command.
Install-Module VMware.PowerCli
If you need to update the module to a new release run
Update-Module VMWare.PowerCli
If a path is not specified the default location that the module files will be placed in is
C:\Program Files\WindowsPowerShell\Modules
Once the install has finished to verify that PowerCli is installed run the below command
Step 2 is to connect to vCenter
To connect to vCenter open an elevated PowerShell console and import the VMware Module
Import-Module VMware.PowerCli
Connect-VIServer lab-VC vCenterServer
Once connected we can now start to run command against vCenter.
To get host information run
Get-VMHost
To find all VM’s that have snapshots over a certain date. I want to get all snapshots older than 1 day to change this just edit the $date variable.
$date = (Get-Date).AddDays(-1)
$Snapshot = get-vm | get-snapshot
$Snapshot | where {$_.Created -lt $date}
To check datastores you can run
Get-Datastore
To get additional info you can do some math’s and use arrays to get % free space of the datastores. The below will get all datastores that have less than 25% free space.
