<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>danejeffrey.com &#187; vbscript</title>
	<atom:link href="http://danejeffrey.com/blog/tag/vbscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://danejeffrey.com/blog</link>
	<description>Good ways to break things.</description>
	<lastBuildDate>Tue, 06 Jul 2010 01:26:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>VMware ESX Snapshot &amp; State Function</title>
		<link>http://danejeffrey.com/blog/2009/07/16/vmware-esx-snapshot-state-function/</link>
		<comments>http://danejeffrey.com/blog/2009/07/16/vmware-esx-snapshot-state-function/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 06:42:51 +0000</pubDate>
		<dc:creator>danejeff</dc:creator>
				<category><![CDATA[VMware]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[esx]]></category>
		<category><![CDATA[vbscript]]></category>

		<guid isPermaLink="false">http://danejeffrey.com/blog/?p=140</guid>
		<description><![CDATA[This function uses the (legacy) VmCom API for VBScript to connect to one or more ESX servers, enumerate the VM&#8217;s currently owned by that ESX server and determine both the VM state and whether it has any current snapshots. The &#8216;SENDMAIL&#8217; flag at the end is a boolean that was used to determine if the [...]]]></description>
			<content:encoded><![CDATA[<p>This function uses the (legacy) <a href="http://communities.vmware.com/community/developer/legacyapi">VmCom</a> API for VBScript to connect to one or more ESX servers, enumerate the VM&#8217;s currently owned by that ESX server and determine both the VM state and whether it has any current snapshots. The &#8216;SENDMAIL&#8217; flag at the end is a boolean that was used to determine if the returned data was worthy of emailing out or not (i.e. if there were any current snapshots or any VM&#8217;s were powered off, suspended, etc).</p>
<p><code><br />
Function CheckServer(strServer)<br />
	On Error Resume Next<br />
	bSendMailFlag = False<br />
	Set ConnectParams = CreateObject("VmCOM.VmConnectParams")<br />
	ConnectParams.username = "root"<br />
	ConnectParams.password = "password"<br />
	ConnectParams.hostname = strServer<br />
	Set ESXServer = CreateObject("VmCOM.VmServerCtl")</p>
<p>	' ----- Connect to the server -----<br />
	ESXServer.Connect ConnectParams<br />
	If Err.number = 0 Then<br />
		strVMConnect =  "Connected to: " &#038; strServer &#038; vbCrLf<br />
	Else<br />
		strVMConnect =  "Error occured connecting to " &#038; strServer &#038; ": " &#038; Err.Description &#038; " (" &#038; Err.Number &#038; ")"<br />
		Err.Clear<br />
		bSendMailFlag = True<br />
		strVMConnect = "SENDMAIL&#038;" &#038; strVMConnect<br />
		CheckServer = strVMConnect<br />
	End If</p>
<p>	' ---- Get a list of all VMs from the server ----<br />
	Set vmCollection = ESXServer.RegisteredVmNames<br />
	strVMConnect = "VMware ESX Server: " &#038; strServer &#038; " (Total VM's: " &#038; vmCollection.Count &#038; ")" &#038; vbCrLf<br />
	j = 1<br />
	For Each vmObject in vmCollection<br />
	   ConfigPath = vmCollection.Item(j)<br />
	   ' Connect to the VM<br />
	   Set vm = CreateObject("VmCOM.VmCtl")<br />
	   vm.Connect ConnectParams, ConfigPath<br />
	   If Err.Number <> 0 Then<br />
		strVMConnect = strVMConnect &#038;  "Could not connect to VM " &#038; vm.ConfigFileName &#038; ": " &#038; Err.Description &#038; " (" &#038; Err.Number &#038; ")" &#038; vbCrLf<br />
		Err.Clear<br />
	   Else<br />
		' ----- Check state of the VM ----<br />
		Select Case vm.ExecutionState<br />
		    Case 1<br />
		     fVMState = "ON"<br />
		    Case 2<br />
		     fVMState = "OFF"<br />
		     bSendMailFlag = True<br />
		    Case 3<br />
		     fVMState = "SUSPENDED"<br />
		     bSendMailFlag = True<br />
		    Case 4<br />
		     fVMState = "STUCK"<br />
		     bSendMailFlag = True<br />
		    Case Else<br />
		     fVMState = "UNKNOWN"<br />
		     bSendMailFlag = True<br />
		End Select<br />
		' ---- Check if the VM has snapshots or not ----<br />
		If vm.HasSnapshot Then<br />
			strVMConnect = strVMConnect &#038; "VM " &#038; j &#038; ": " &#038; vmObject &#038; " - State: " &#038; fVMState &#038; ", Snapshot(s): YES" &#038; vbCrLf<br />
			bSendMailFlag = True<br />
		Else<br />
			strVMConnect = strVMConnect &#038; "VM " &#038; j &#038; ": " &#038; vmObject &#038; " - State: " &#038; fVMState &#038; ", Snapshot(s): NO" &#038; vbCrLf<br />
		End If<br />
	   End If<br />
	   j = j+1<br />
	Next<br />
	Set vm = Nothing<br />
	Set ESXServer = Nothing<br />
	Set ConnectParams = Nothing<br />
	If bSendMailFlag Then<br />
		strVMConnect = "SENDMAIL&#038;" &#038; strVMConnect &#038; vbCrLf<br />
	Else<br />
		strVMConnect = "&#038;" &#038; strVMConnect &#038; vbCrLf<br />
	End If<br />
	CheckServer = strVMConnect<br />
End Function<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://danejeffrey.com/blog/2009/07/16/vmware-esx-snapshot-state-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
