Finding expired vCloud vApps with the PowerCLI Cloud-Search commandlet

When looking at expired vApps in the vCloud UI, the owner’s information isn’t displayed.   This makes it difficult to determine who to contact about the expired vApp.  In the past I ran the following PowerCLI script to display the expired vApps, owner login, owner name, the VMs in the vApp and their organization.


get-civapp | ? { try { ($expirationDate = $_.ExtensionData.Section[0].StorageLeaseExpiration) -ne $null -and (get-date) -gt $expirationDate } catch {} } | `
    select name,`
           @{N = 'Owner'; E = { if ($_.owner.fullname) { $_.Owner.Name + ' - ' + $_.Owner.FullName } else { $_.Owner.Name }}}, `
           @{N = 'Expiration Date'; E = { $expirationDate }}, `
           @{N = 'Org'; E = { $_.Org }}, `
           @{N = 'Description'; E = { $_.Description }}, `
           @{N = 'Virtual Machines' ; E = { (($_ | get-civm | select name) | % { $vms + $_.name }) -join ", " }} | sort 'Expiration Date', org

I dug through the PowerCLI objects and couldn’t find way of determing if a vApp was in an expired state.  If it’s there, please let me know.  However, I did see that if the vApp was expired, vApp.ExtensionData.Section[0].StorageLeaseExpiration would be set to a date (otherwise it would be empty), and if this date was in the past, then the vApp would be in the expired items (assuming you had vCloud configured to not delete them).   The script above worked, but it was slow in our environment (8 minutes to run).  It was put in a nightly report so it wasn’t that big of a deal, but it always bothered me.

I knew about the Search-Cloud PowerCLI commandlet but hadn’t had time to look into it.  I finally decided to play around with it and found that it could query vCloud objects much faster than using a PowerCLI’s ExtensionData method, which I think just gets the objects view and that is basically everything about the object.   I replaced the above script with the following.


# Create lookup tables for orgs and users
$orgs = @{}
$users = @{}

# Populate lookup tables 
# The mappings will ljook like
# Name                           Value
# ----                           -----
# urn:vcloud:org:243d3468-93e... admin
# urn:vcloud:org:cd07ebc8-620... sales
# urn:vcloud:org:b99aa87f-eab... engineering

Search-Cloud -QueryType Organization | % { $orgs[$_.id] = $_.name }
Search-Cloud -QueryType Adminuser | % { $users[$_.name] = $_.fullname }

# Search for expired vApps and select the name, owner's name, owner's full name and org.  We deteremine the org's name by taking the urn that is returned by Search-Cloud and looking it up in the org lookup table to find its friendly name. 
Search-Cloud -QueryType AdminVApp -filter 'IsExpired==True' | select name, 
                                                                     ownerName, 
                                                                     @{N='Fullname';E={$users[$_.ownername]}},
                                                                     @{N='Org';E={$orgs[$_.org]}}

This script completes in about one or two seconds.  Since Search-Cloud doesn’t return everything that ExtentionData does, which is one reason it’s faster, you don’t get back info such as the organization name.  Instead of the org name, you get back the org’s urn (for example, urn:vcloud:org:a56f6a46-ff33-4174-869f-129676d70a4f).

This is easy to work around by building a lookup table that maps a urn to an org/user name.  You just have to call Search-Cloud again and change the query type to organization/adminuser.  The returned info will have the org name and urn.  Once you have the the org/user urn, you can look up the friendly name in the orgs/users lookup tables.  For example, in the select statement, you see the funny looking entry “@{N=’Org’;E={$orgs[$_.org]}}”.  Looking at the E (Expression) section, you see that it equals $orgs[$_.org].  This is taking the current object’s ($_) org urn and looking it up in the orgs lookup table.  For the first object, $_.org will be ‘urn:vcloud:org:243d3468-93e’.  We then take this value and place it into the orgs lookup table.

$orgs[‘urn:vcloud:org:243d3468-93e’] = admin

The result is ‘admin’, which will be displayed as the org name in the Search-Cloud query.

 

Advertisements


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