SAMPLE WORKFLOW: Virtual Machine Snapshot Age
VMware provides a relatively easy way to script virtual machine management using PowerShell with PowerCLI. Unfortunately there is still a significant learning curve for someone with minimal PowerShell experience; not realizing a connection to the virtual center or ESX host is required before executing other commands, or that failure to close that connection in a script leaves the connection open indefinitely can be very frustrating.
Fortunately PowerWF simplifies this process significantly. One problem VMware administrators face is finding old virtual machines snapshots that are wasting storage space. The following example takes advantage of both PowerShell and PowerCLI to find outdated snapshots and email the results. To begin, create a new workflow project named, “SnapshotAge”.
- To begin, create a new workflow project named, “SnapshotAge”.
- In many cases, a quick search of the web will yields a PowerCLI/PowerShell script to do most of the tasks in a workflow. The following script is one such example:
Get-VM * | Get-Snapshot | Select VM, Created, Name, Description - If the PowerShell console is not already enabled, use the view tab on the ribbon bar to enable it.

- Enter the PowerShell script into the PowerShell Console and click the Insert Script button

- PowerWF automatically converts the PowerShell script into a PowerWF workflow. A connectVIserver activity was not in the initial PowerShell script and should be added. The exclamation point icon next to the activity indicates that one or more required properties are not properly configured. Select the connectVIServer1 activity and fill in the appropriate values for the server (VirtualCenter or ESX Host), user, and password.

- At this point, visualization can be added and the script tested. The desired output for this example is an HTML table, so in the toolbox search, type “html”. This will list all HTML related activities. Drag the “toHTML” activity to the workflow canvass and drop at the bottom of the “runspaceSequence1” activity.

- Once the output is converted to HTML, it can be displayed using a web browser. Click the
next to the search box to show all activity packs. Drop the webBrowser activity from Visualization into the workflow below the “toHTML1” activity.
- Press the Start button in the ribbon bar to run the workflow. In its current state, the workflow finds the snapshots for the virtual machines and displays them as an HTML table.

- The results would be more meaningful if the data was sorted based on the creation date and VM name so that the oldest snapshots appeared at the top of the table. Typing “sort” in the search bar finds the “sortObject” activity. Drop this in the pipelineSequence below the “selectObject1” activity.

- The sort object requires selecting one or more columns to sort on and the sort order (ascending or descending). Add two sort properties, Created and VM.

- Press the Start button in the ribbon bar to run the workflow. The output is now in a meaningful format.

- The “WebBrowser” activity can now be deleted and replaced with the “SendMailBySMTP”. This activity can be found by typing “mail” in the search bar. The “SendMailBySMTP” requires slightly more configuration than some other activities. Set the mail server host, user,password and SMTP Port and EnableSSL if necessary.

- It is also necessary to configure who the email is to and from, and the subject of the email. Set the “To” email address to the target recipient and the “From” email address as the user whose credentials are used to connect to the mail server. The Subject should be set to something meaningful, such as “Virtual Machine Snapshot Age Summary”.

- The final steps are to set the “BodyHTML” property to True to configure the output of the “toHTML1” activity as the Body of the email. Click on the
button and expand the “connection Runspace1 – toHTML”1 and select “Output” This will bind the output from the “toHTML1” activity to the “Body” of the “sendMailBySMTP1” activity.
Up to this point PowerWF connected the output from one step in the workflow to the input of the next item in the workflow. The “sendMailBySMTP” activity is a little different because the upstream outputs could be used as the Subject, the Body, or even as a User or Password. - Press the Start button in the ribbon bar to run the workflow. If everything is configured correctly, an email with snapshot ages should be sent to the target recipient.

2 years ago - link

