|
You are probably aware that Vmware provides an SDK that's based on a Webservice which is integrated in Virtual Center. When you download the SDK, there are loads of examples but all in perl, java or .NET languages. For those of you, who have been mainly involved in Windows technologies, your primary scripting language of choice was probably vbscript. Also on the Vmware community I have seen quite a few people posting questions asking for examples, as I don’t think the Vmware community is the right place to store loads of lines of codes I decided to write this article and give you some ready to use examples.
Before you start make sure you have your development environment up and running and configured. I assume you have a server with Virtual Center 1.x (obviously with the Webservice enabled) and at least one ESX Server (2.x) with the VC Agent deployed on it.
To test if your Webservice is working, open an Internet Explorer and open the following url: "C:\Documents and Settings\All Users\Application Data\VMware\VMware VirtualCenter\VMA\login\login.html" and make sure you can login using the username and password provided during the installation.
Now that verified the Webservice is working correctly let's get started and write a script that will create a new virtual machine.
Note: Throughout the article I have only posted the Body of the SOAP message to make it more readable. Full source can be downloaded at the end of the article.
First we are going to declare some variables about our environment, the new VM and create references to the ServerXMLHTTP and DOMDocument objects:
Dim username, password, url
Dim vmName, CPU, Mem, OS, ESXHost, ParentPath
Set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Set oXMLDOM = CreateObject("MSXML2.DOMDocument")
username = myuser
password = myuserspassword
url = https://vcserver:8443
Now that we have declared and set our initial values we have to logon to the Webservice, to do that we create a string containing our SOAP Message for loging in to our Webservice:
sXML = “<SOAP-ENV:Body>” & _
“<Login xmlns=""urn:vma1"">” & _
“<userName xmlns:SOAPSDK4=""urn:vma1"">" & sUsername & "</userName>” & _
“<password xmlns:SOAPSDK5=""urn:vma1"">" & sPassword & "</password>” & _
“</Login></SOAP-ENV:Body>” & _
“</SOAP-ENV:Envelope>”
Then we load the string "sXML" containing our SOAP Message into a DomDocument which we will be sending shortly to our Webservice. Next we would have to initialize the request, set the method to "POST", the url to the url of our Webservice and the asynchronous mode to false.
oXMLDOM.LoadXML sXML
oXMLHTTP.Open "POST", url, false
If you are using https to target the Webservice you would also have to set the following option to ignore all certificate errors:
oXMLHTTP.SetOption 2, 13056
Now we can send the HTTP request to the Webservice, receive a response and save our SessionCookie for later so we can re-submit it on subsequent calls.
oXMLHTTP.Send oXMLDOM.XML
sSessionCookie = oXMLHTTP.getResponseHeader("Set-Cookie")
Index = Instr(1, sSessionCookie, "=", 1)
sSessionCookie = Mid(sSessionCookie, (Index + 1), _
(InStr(1, sSessionCookie, ";", 1) - Index - 1))
Now that we are logged on lets sets some values for variables we have defined for our new virtual machine:
vmName = “newVM”
CPU = 1
Mem = 512
OS = “winNetEnterprise”
ESXHost = “ESX01”
ParentPath = “/vcenter/TEST/New Servers”
Note: the ParentPath must always start with “/vcenter” followed by the Farm name and possible sub folders.
Next Page (2/2) 
|