Home > XenServer > Powershell script for cleaning up space after deleting snapshots from VM’s

Powershell script for cleaning up space after deleting snapshots from VM’s

September 1, 2010 Leave a comment Go to comments

You remember my two posts about the snapshot and missing free space on the storage after deleting a snapshot. With XenServer 5.6 Citrix changed the command to free space to a xe command. This makes it easy to write a script, that frees space for all VM’s on your XenHost after deleting snapshots. Here it is:

################################################################

# Script to free space after deleting snapshots for all VM’s
################################################################
# Define variables

$xenserver_ip = “172.27.10.99”
$xenserver_user = “root”
$xenserver_pwd = “password”
$xenserver_uuid = “6f4751af-9d55-4856-bb38-1985fecea06c”
$xe_path = “‘C:\Program Files\Citrix\XenCenter\xe.exe'”

################################################################
# Script

# Make VM líst
$uuid_list = Invoke-Expression  (“& ” + $xe_path + ” -s ” + $xenserver_ip + ” -u ” + $xenserver_user + ” -pw ” + $xenserver_pwd + ” vm-list –minimal”)

# Split
$uuid_list = $uuid_list.split(“,”)

ECHO “********************************************”

# One passage for each VM
foreach ($item in $uuid_list)

{
ECHO $item
# Command
Invoke-Expression (“& ” + $xe_path + ” -s ” + $xenserver_ip + ” -u ” + $xenserver_user + ” -pw ” + $xenserver_pwd + ” host-call-plugin host-uuid=” + $xenserver_uuid +  ” plugin=coalesce-leaf fn=leaf-coalesce args:vm_uuid=” + $item)

ECHO “********************************************”
}

So all you need is a client or server with XenCenter 5.6 and Powershell installed. Copy the lines in a file and name it “Free_space_after_deleting_snapshot.ps1” if you want. Edit the variables and execute the script. You have to allow unsigned Powershell scripts!

Hope you like it!

Advertisements
Categories: XenServer
  1. Mustapha
    July 14, 2011 at 17:00

    Hi Dennis,

    I tried using your script, but I am getting the following error:

    Method invocation failed because [System.Object[]] doesn’t contain a method named ‘split’.
    At C:\PS\Free_space_after_deleting_snapshot.ps1:20 char:30
    + $uuid_list = $uuid_list.split <<<< (",")
    + CategoryInfo : InvalidOperation: (split:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

    ********************************************
    Syntax error: Unknown switch: –minimal
    Error code: XENAPI_PLUGIN_EXCEPTION
    Error params: leaf-coalesce, Exception, VM not found
    ********************************************
    For usage run: 'xe help'
    Error code: XENAPI_PLUGIN_EXCEPTION
    Error params: leaf-coalesce, Exception, VM not found
    ********************************************

    Am I missing something?

    Thanks,

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: