Monthly Archives: March 2013

How to login into machine using c# and System.Management library

Have to do periodic checks if it is still possible to login into every Virtual Server using domain account. So I had to put some code togather:

using System;
using System.Collections.Generic;
using VMware.Vim;
using System.Collections.Specialized;
using System.Management;

namespace vSphereCommand
{
internal class Program
{
private static void Main(string[] args)
{
var c = new VimClient();
var sc = c.Connect("https:///sdk");
var us = c.Login("", "");

IList vms = c.FindEntityViews(typeof (VirtualMachine), null, null, null);

foreach (var tmp in vms)
{
var vm = (VirtualMachine)tmp;
try
{
if (vm.Guest.GuestState == "running")
{
var serverName = vm.Guest.HostName;
var domainName = "";
var userName = "";
var password = "";

var connectionOptions = new ConnectionOptions();
connectionOptions.Username = userName;
connectionOptions.Password = password;
connectionOptions.Authority = string.Format("ntlmdomain:{0}", domainName);
connectionOptions.Authentication = AuthenticationLevel.PacketPrivacy;
var scope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", serverName), connectionOptions);
scope.Connect();
Console.WriteLine(" ***OK*** Name: " + vm.Guest.HostName + " IP:" + vm.Guest.IpAddress + " state:" + vm.Guest.GuestState);
}
else
{
Console.WriteLine(" Not Runing. Name: " + vm.Guest.HostName + " IP:" + vm.Guest.IpAddress + " state:" + vm.Guest.GuestState);
}
}
catch(Exception ex)
{
Console.WriteLine("ERROR. Failed to connect. Name: " + vm.Guest.HostName + " IP:" + vm.Guest.IpAddress + " state:" + vm.Guest.GuestState);
}
}
}
}
}

How to revert Virtual Machine Snapshot by Virtual Appliance name and Snapshot Name on VMVare vSphere using vSphere Power CLI API

Was looking for code like this for some time and could not find anything useful and in one place on the internet. At the end I came up with something like this:

using System;
using System.Collections.Generic;
using VMware.Vim;
using System.Collections.Specialized;

namespace vSphereCommand
{
internal class Program
{
private static void Main(string[] args)
{
var c = new VimClient();
var sc = c.Connect("https:///sdk");
var us = c.Login("", "");

NameValueCollection filter = new NameValueCollection();
filter.Add("name", "");
IList vApps = c.FindEntityViews(typeof (VirtualApp), null, filter, null);

foreach (var tmp in vApps)
{
var vApp = tmp as VirtualApp;
if (vApp != null)
{
Console.WriteLine(vApp.Name);
IList vms = vApp.Vm;
foreach (var vm in vms)
{
var vmView = (VirtualMachine) c.GetView(vm, null);
RevertToSnapshot("", vmView, c);
}
}
vApp.PowerOnVApp();
}
}

public static void RevertToSnapshot(string sSnapshotName, VirtualMachine vm, VimClient c)
{
ManagedObjectReference snapshot = FindSnapshot(vm.Snapshot.RootSnapshotList, sSnapshotName);
VirtualMachineSnapshot vms = new VirtualMachineSnapshot(c, snapshot);
var host = (HostSystem) c.GetView(vm.Runtime.Host, null);
vms.RevertToSnapshot(host.MoRef, false);
}

private static ManagedObjectReference FindSnapshot(VirtualMachineSnapshotTree[] root, string sSnapshotName)
{
foreach (VirtualMachineSnapshotTree tree in root)
{
if (tree.Name.Equals(sSnapshotName, StringComparison.OrdinalIgnoreCase))
return tree.Snapshot;
}

foreach (VirtualMachineSnapshotTree tree in root)
{
ManagedObjectReference snap = FindSnapshot(tree.ChildSnapshotList, sSnapshotName);

if (snap != null)
return snap;
}

return null;
}
}
}

How to convert your footnotes and endnotes into Smashwords.com compatible bookmarks

Lately I had a lot of trouble with one e-book. I had to format Microsoft word document to comply with Smashwords.com standards. The biggest problem was to translate footnotes and endnotes into Smashwords.com compatible cross referenced bookmarks/ links. 

 this is what you get if you have problems with Footnotes/Endnotes:

Footnote Error – Smashwords does not support automatic footnoting using Word’s footnoting field codes. If you need footnotes or endnotes, please use plain text in brackets, and then use Word’s “Bookmark” feature, described in the Smashwords Style Guide to create your hyperlinks to your footnotes and endnotes. For example, a paragraph could end with the following footnote… [234] (and then for your footnote or end note you could manually enter [234] followed by the footnote information.)

 Key tip is to put “ref_” in front of every bookmark if you do not want it to appear on the table of contents of your epub.

My book had ~500 endnotes. To do it by hand is a nightmare. To easy my pain i did two word macros – one inserts a bookmark and a link to the bookmark and the other one inserts a link back. (Unfortunately I inserted bookmarks next to endnotes by hand before I realized I need some macro). But is should not be to complex to modify my code to insert that bloody bookmark also.  Just look at the code and find out yourself which macro does what 🙂 So here is the code:


Sub BookamrkMagic()
x = Selection
Selection.MoveLeft Unit:=wdCharacter, Count:=2
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="ref_" + x
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
SubAddress:="ref_x_" + x, ScreenTip:="", TextToDisplay:=x
End Sub

Sub BookamrkMagicX()
x = Selection
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
SubAddress:="ref_" + x, ScreenTip:="", TextToDisplay:=x
End Sub