Link to home
Start Free TrialLog in
Avatar of ICCNetworkAdmin
ICCNetworkAdminFlag for United States of America

asked on

How to run a Power Shell script using PowerCLI?

I am trying to create a power shell script to return results of snapshots, create snapshots, and delete snapshots.

I can run all of these commands in the PowerCLI command prompt interface, but cannot seem to figure out the process in which you can double click on a ps1 or bat file and have the script run, and return the results.

Can someone give me the steps that are required to run a single file to return results for creating, deleting(commiting), and showing snapshots on an ESX server?

I tried putting this into a bat file... but the command  indicates that the file doesn't end with psc1, because the quotes aren't working correctly. It looks directly at "C:\Program" (For the second part of the command)

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\VMware\Infrastructure\VSphere PowerCLI\vim.psc1” -NoExit –Command C:\showsnaps.ps1

Inside my ps1 file is this:

connect-VIServer -Server esxserver1 -User myusername -Password mysupersecretpassword
Get-VM * | Get-Snapshot
Avatar of AussieClint
AussieClint
Flag of Australia image

To the end of your command use one of the ConvertTo- cmdlets and then pipe out to a file
e.g. to output in a html table to a html file you could do something like this:

$LogFile = "C:\Data\Snapshots.html"
Get-VM | Get-SnapShot | ConvertTo-HTML | Out-File $LogFile -Append -Encoding ASCII
ASKER CERTIFIED SOLUTION
Avatar of AussieClint
AussieClint
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luciano Patrão
Hi

First to run Powershell on VMware using VMware Powershell commands you need VMware vSphere™ PowerCLI

Download from here:
http://communities.vmware.com/community/vmtn/vsphere/automationtools/powercli

Some commands examples:
http://blogs.vmware.com/vipowershell/

Powershell scripts run as ps1 files, so just run the powercli console and run your script(ps1 file) inside, or use PowerShell Gui Editor, to change, save or run the script.

After installing VMware PowerCli I also install PowerGui. Have good packages and works great.
http://powergui.org/downloads.jspa

I will add here a snapshot script example:

Hope this can help

Jail
###########################################################################################
# TScript: List All Snaphots for each VM												  #
# Created by: Luciano Patrão													       	  #
# Date: 01-12-2010   						 	     	                	       		  #
###########################################################################################

$server = "Enter vCenter IP or VMware host IP"
$user = "Enter user"
$pwd = "Enter password"

Connect-VIServer $server -User $user -Password $pwd

$AllVirtualMachines = Get-VM

foreach ($VirtualMachine in $AllVirtualMachines)
	{$AllSnapshots=Get-Snapshot -VM $VirtualMachine
		foreach ($Snapshot in $AllSnapshots)
	 		{If ($Snapshot.ID -like "VirtualMachineSnapshot-*")
  			{Write-Host "VM-> " $VirtualMachine.Name, 
				" - Snapshot Name-> " $Snapshot.Name, 
				" - Snapshot Description-> "$Snapshot.Description}}}

Open in new window

Avatar of ICCNetworkAdmin

ASKER

Excellent. You gave me exactly what I needed to know and the HTML exporting is perfect for what I want to accomplish. Thanks!
Thanks BestWay, that helps me with the scripting process as well. With that said, AussieClint resolved my issue by pointing out that I was using -Command instead of -File, so I will be awarding him the points.

While we are on the topic though, what if I wanted to query all snapshots for all of my ESX servers? Would I just use the Disconnect-VIServer command after I return results for each ESX server? Is there an easier way, such as perform a For Each command?
Hi

You do a foreach or each host, but as an example, in my script if you have 2 or more hosts connected will list all VMs and Snaphsots from all the hosts.

PS: About the points, no problem, just here for the help

Jail