Tag Archives: VmWare

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;
}
}
}

Programmatically rejoin active directory domain after virtual machines snapshot is reverted

There is a problem with active directory computer accounts in combination with virtual machine snapshots. After virtual machine is added into domain computer account’s password gets updated in AD once in a while. In case you are coppying, moving, snapshoting or cloning machine which is joined to a domain you must have domain membership problems on those machines very often. It happens if password which is storred on your virtual machine and password on the account object in AD are out of sync. There are some administrative ways to fix that.

For example:

a) changing computer account password configuration policies in AD,

b) rejoining the domain by hand everytime domain membership is broken,

c) snapshoting not only virtual machine but Domain controller and restoring both snapshots in one atomic action

After I become tired of fixing those issues manually I decided to search for programmatic way to join the domain.

I wrote c#.net console application which is able to identify if the machine has broken domain membership and if it has it is able to fix it.

You can download the msi file here.

https://i0.wp.com/are.ehibou.com/wp-content/plugins/downloads-manager/img/icons/default.gif?w=625 download: FixDomainMembership.msi (450.5KB)
added: 22/05/2012
clicks: 436
description: Command Line Utility to fix domain membership

After the executable is installed on the machine with broken domain membership execute the command with command line parameter –help and follow the manual …