VMware

This Question is Possibly Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
13 Replies Last post: Nov 4, 2009 8:55 AM by LucD  

how to make that Customfields automatic insert values? posted: Nov 3, 2009 1:10 AM

Click to view shatztal's profile Enthusiast 102 posts since
Feb 4, 2008

i want to make that every time a new ivrtual machine is created to a VM is Deployed or a Clone is be made

that some of my custom attributes are automatic insert with info , For example:

Domain attribute with but the Domain name of that machine

Os attribute with the Os Information like it is written in the summarry.

and another thing is it possible to enter value of custom attributes in the Customization specification?

Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
The problem is more complex than it looks at first sight.
You can easily run a script when a VM is created through an Alarm with a trigger of "VM created".
But since your custom attribute contains the Domain you will have to wait to run the script until the VM has joined the domain.

The Alarm script can wait in a loop till it sees the new VM in the domain.

Or you could use the "Run once"script but I'm not really sure this will run after your VM has joined the domain.

I would go for an Alarm that loops until the new VM is seen in the domain and then extract the information (partially via Invoke-VMScript or WMI) to populate the Custom Fields.
Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
If you can live with the fact that the custom fields can be updated once a day from a central location, it's even easier.

You can use the following snippet to get the domainname of the domain to which the VM belongs.
You could include some further testing if nothing is returned, which would mean the guest is not a member of the domain.

The code can handle the situation where the VM name (as seen in vCenter) is different from the hostname in the guest's OS.

This code doesn't require the installation of any snapins to handle the domain queries.
$vmName = <vmName>

$strGuest = (Get-VM $vmName | Get-VMGuest).HostName.Split(".")[0]

$objRootDSE = New-Object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE")
$strRootDN = $objRootDSE.Get("rootDomainNamingContext")

$objRootDomain = New-Object System.DirectoryServices.DirectoryEntry("GC://" + $strRootDN)
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objRootDomain

$objSearcher.Filter = ("(&(objectClass=computer)(name=$strGuest))")

$intProperties = $objSearcher.PropertiesToLoad.Add("canonicalName")

$objResults = $objSearcher.FindAll()
if ($objResults.Count -eq 1) {
	$strDomain = [string]($objResults[0].Properties.canonicalname)
	$strDomain = $strDomain.SubString(0, $strDomain.IndexOf("/"))
}
$strDomain

Since there are some square brackets in the code I attached the script.
Attachments:
Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
You could get the OS Attribute like this
$vmName = <vm-name>
(Get-VM $vmName | Get-VMGuest).OSFullName


Is the new guest created under the userid of the "owner" ?
In that case you could use the Get-ViEvent cmdlet to obtain the userid of the one that created the guest.
With the userid you can launch an AD search to get the Full Name or Display Name.

What do you want to see under Description ?
The "Computer description" as it appears in the System control panel applet.
Or the AD Description ?
Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
The R2 indication will be in the OSFullName property
To get the SP level you could use the Get-WmiObject cmdlet.
Get-WmiObject Win32_OperatingSystem –computerName <hostname>

and use the BuildNumber or Version property.
Have a look at Powershell script to retrieve the OperatingSystem and Service Pack for more details
Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
What does
$objResults.Count

return just before the If statement ?
Is it 1 ?
Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
That means there was no AD entry found with that hostname.
Is the hostname correct ? Have a look in the $strGuest variable.
Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
I suspect the domainname can also be extracted from
(Get-VM $vmName | Get-VMGuest).HostName

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