VMware

This Question is Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
4 Replies Last post: Nov 4, 2009 7:42 AM by herreraj  

VM expiration script - Automation problem posted: Oct 16, 2009 3:45 PM

Click to view herreraj's profile Lurker 5 posts since
Aug 8, 2007

Good day fellows,

I am seeking your assistance in troubleshooting the attached script. This what i'm trying to accomplish:

  • Get created time for the VM in order to see how many days the VM has been in production to-date
  • Send an email to the business owner if the VM has been deployed for more than a year based on information from Custom Fields. The server (s) Business Owners (BO) are contacted only if the server has been deployed over 365 days AND if they have not been contacted (see Custom Field below, with a value of either "Yes" or "No)

Information on the Custom Fields:

  • Application
  • Contacted
  • Decommission_date
  • Business_Owner
  • BO_email
  • Business_Owner2

Issue:

  • Unable to retrieve the value of the "Contacted' CustomField. Though i know how to query the value, i cant get it to compare against another string
  • Same as above except that's for the email address value

I've attached the script i've been working on for your review. Your assistance on this will greatly appreciated!!
Attachments:

Re: VM expiration script - Automation problem

1. Oct 18, 2009 8:36 PM in response to: herreraj
Click to view harkamal's profile Hot Shot 221 posts since
Mar 19, 2009

Here you go mate..

$vm = Get-VM myVm | Get-View
$myTag = "Contacted"
$myKey = $vm.AvailableField | ? { $_.Name -eq $myTag  } | select Key
$myValue = $vm.CustomValue | ? { $_.Key -eq $myKey } | select Value

Write-Host $myValue


Hope that was helpfull


Re: VM expiration script - Automation problem

3. Oct 19, 2009 8:45 PM in response to: herreraj
Click to view harkamal's profile Hot Shot 221 posts since
Mar 19, 2009
It works for me...create a new annonation/custom field for a vm named "myTag" and set its value
Function getCustomFieldValue {
       param([string]$Field = $(throw "Field name is required"))
       $myKey = $vm.AvailableField | ? { $_.Name -eq $Field } | Select Key
       $myValue = $vm.CustomValue | ? { $_.Key -eq $myKey.Key } | Select Value

       return $myValue.Value
}


$vm = Get-VM "New Virtual Machine" | Get-View
getCustomFieldValue "myTag"

VMware Developer

SDKs, APIs, Videos, Learn and much more in the Developer community.

Learn More

Developer Sample Code

Increase your developer productivity with VMware API sample code.

Learn More

VMworld Sessions & Labs

Online access to the latest VMworld Sessions & Labs and online services.

Learn more

Purchase PSO Credits Online

Purchase credits to redeem training and consulting services online.

Buy Now

Community Hardware Software

View reported configurations or report your own.

Learn More

VMware vSphere

Come witness the next giant leap in virtualization.

Register Today

Communities