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:

 csharp |  copy code |? 
01
using System;
02
using System.Collections.Generic;
03
using VMware.Vim;
04
using System.Collections.Specialized;
05
using System.Management;
06
 
07
namespace vSphereCommand
08
{
09
    internal class Program
10
    {
11
        private static void Main(string[] args)
12
        {
13
            var c = new VimClient();
14
            var sc = c.Connect("https://<vSphere Server>/sdk");
15
            var us = c.Login("<username>", "<password>");
16
 
17
            IList<EntityViewBase> vms = c.FindEntityViews(typeof (VirtualMachine), null, null, null);
18
 
19
            foreach (var tmp in vms)
20
            {
21
                var vm = (VirtualMachine)tmp;
22
                try
23
                {
24
                    if (vm.Guest.GuestState == "running")
25
                    {
26
                        var serverName = vm.Guest.HostName;
27
                        var domainName = "<YOURDOMAIN>";
28
                        var userName = "<domainUserName>";
29
                        var password = "<domainUserPassword>";
30
 
31
                        var connectionOptions = new ConnectionOptions();
32
                        connectionOptions.Username = userName;
33
                        connectionOptions.Password = password;
34
                        connectionOptions.Authority = string.Format("ntlmdomain:{0}", domainName);
35
                        connectionOptions.Authentication = AuthenticationLevel.PacketPrivacy;
36
                        var scope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", serverName), connectionOptions);
37
                        scope.Connect();
38
                        Console.WriteLine("    ***OK***    Name: " + vm.Guest.HostName + " IP:" + vm.Guest.IpAddress + " state:" + vm.Guest.GuestState);
39
                    }
40
                    else
41
                    {
42
                        Console.WriteLine("        Not Runing. Name: " + vm.Guest.HostName + " IP:" + vm.Guest.IpAddress + " state:" + vm.Guest.GuestState);
43
                    }
44
                }
45
                catch(Exception ex)
46
                {
47
                    Console.WriteLine("ERROR. Failed to connect. Name: " + vm.Guest.HostName + " IP:" + vm.Guest.IpAddress + " state:" + vm.Guest.GuestState);
48
                }
49
            }
50
       }
51
    }
52
}
53

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:

 csharp |  copy code |? 
01
using System;
02
using System.Collections.Generic;
03
using VMware.Vim;
04
using System.Collections.Specialized;
05
 
06
 
07
namespace vSphereCommand
08
{
09
    internal class Program
10
    {
11
        private static void Main(string[] args)
12
        {
13
            var c = new VimClient();
14
            var sc = c.Connect("https://<vsphere-server>/sdk");
15
            var us = c.Login("<user>", "<password>");
16
 
17
            NameValueCollection filter = new NameValueCollection();
18
            filter.Add("name", "<vApp Name>");
19
            IList<EntityViewBase> vApps = c.FindEntityViews(typeof (VirtualApp), null, filter, null);
20
 
21
            foreach (var tmp in vApps)
22
            {
23
                var vApp = tmp as VirtualApp;
24
                if (vApp != null)
25
                {
26
                    Console.WriteLine(vApp.Name);
27
                    IList<ManagedObjectReference> vms = vApp.Vm;
28
                    foreach (var vm in vms)
29
                    {
30
                        var vmView = (VirtualMachine) c.GetView(vm, null);
31
                        RevertToSnapshot("<Snap name>", vmView, c);
32
                    }
33
                }
34
                vApp.PowerOnVApp();
35
            }
36
        }
37
 
38
        public static void RevertToSnapshot(string sSnapshotName, VirtualMachine vm, VimClient c)
39
        {
40
            ManagedObjectReference snapshot = FindSnapshot(vm.Snapshot.RootSnapshotList, sSnapshotName);
41
            VirtualMachineSnapshot vms = new VirtualMachineSnapshot(c, snapshot);
42
            var host = (HostSystem) c.GetView(vm.Runtime.Host, null);
43
            vms.RevertToSnapshot(host.MoRef, false);
44
        }
45
 
46
        private static ManagedObjectReference FindSnapshot(VirtualMachineSnapshotTree[] root, string&nbsp;<!--DVFMTSC-->sSnapshotName)
47
        {
48
            foreach (VirtualMachineSnapshotTree tree in root)
49
            {
50
                if (tree.Name.Equals(sSnapshotName, StringComparison.OrdinalIgnoreCase))
51
                    return tree.Snapshot;
52
            }
53
 
54
            foreach (VirtualMachineSnapshotTree tree in root)
55
            {
56
                ManagedObjectReference snap = FindSnapshot(tree.ChildSnapshotList, sSnapshotName);
57
 
58
                if (snap != null)
59
                    return snap;
60
            }
61
 
62
            return null;
63
        }
64
    }
65
}
66

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:

 vb |  copy code |? 
01
02
Sub BookamrkMagic()
03
    x = Selection
04
    Selection.MoveLeft Unit:=wdCharacter, Count:=2
05
    With ActiveDocument.Bookmarks
06
        .Add Range:=Selection.Range, Name:="ref_" + x
07
        .DefaultSorting = wdSortByName
08
        .ShowHidden = True
09
    End With
10
    Selection.MoveRight Unit:=wdCharacter, Count:=1
11
    Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
12
    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
13
        SubAddress:="ref_x_" + x, ScreenTip:="", TextToDisplay:=x
14
End Sub
15
 
16
Sub BookamrkMagicX()
17
    x = Selection
18
    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
19
        SubAddress:="ref_" + x, ScreenTip:="", TextToDisplay:=x
20
End Sub
21