Exemplo n.º 1
0
        public static double AverageCPUUsage(VMVirtualMachine vm)
        {
            double average = 0;
            double total   = 0;

            foreach (object cpu in (IEnumerable)((System.Array)vm.Accountant.CPUUtilizationHistory))
            {
                total = total + double.Parse(cpu.ToString());
            }
            average = Math.Floor(total / 60);

            return(average);
        }
Exemplo n.º 2
0
        public frmDupVM(string currentVS, string currentVM)
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
            vs = Utility.getVS(currentVS);
            vm = Utility.getVM(currentVS, currentVM);
        }
Exemplo n.º 3
0
        public static VMVirtualMachine getVM(VMVirtualServer vs, string ServerDisplayName)
        {
            VMVirtualMachine vm = null;

            try
            {
                vm = vs.FindVirtualMachine(ServerDisplayName);
            }
            catch (Exception err)
            {
                //MessageBox.Show(err.Message, "Virtual Machine Error");
            }

            return(vm);
        }
Exemplo n.º 4
0
        private bool GetVirtualMachine()
        {
            if (string.IsNullOrEmpty(this.Name))
            {
                this.Log.LogError("Name is required.");
                return(false);
            }

            this.virtualMachine = this.virtualPC.FindVirtualMachine(this.Name);
            if (this.virtualMachine == null)
            {
                this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "Virtual Machine: {0} not found", this.Name));
                return(false);
            }

            return(true);
        }
Exemplo n.º 5
0
        public static VMVirtualMachine getCurrentVM(int tabIndex)
        {
            VMVirtualMachine vm       = null;
            XmlNodeList      nodeList = frmMain.xmlConfig.SelectNodes("//VirtualServer");

            if (nodeList.Count > 0)
            {
                foreach (XmlNode xmlNode in nodeList)
                {
                    if (xmlNode.SelectSingleNode("TabIndex").InnerText == tabIndex.ToString())
                    {
                        vm = getVM(xmlNode.SelectSingleNode("ServerAddress").InnerText, xmlNode.SelectSingleNode("ServerDisplayName").InnerText);
                    }
                }
            }

            return(vm);
        }
Exemplo n.º 6
0
        private bool GetVirtualMachine()
        {
            if (string.IsNullOrEmpty(this.Name))
            {
                this.Log.LogError("Name is required.");
                return false;
            }

            this.virtualMachine = this.virtualPC.FindVirtualMachine(this.Name);
            if (this.virtualMachine == null)
            {
                this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "Virtual Machine: {0} not found", this.Name));
                return false;
            }

            return true;
        }
Exemplo n.º 7
0
        private void btnDupVM_Click(object sender, System.EventArgs e)
        {
            // Create new VM folder
            lblProgress.Text = "Creating VM folder";
            lblProgress.Update();
            if (Directory.Exists(txtNewVMPath.Text))
            {
                DialogResult result;
                result = MessageBox.Show("Destination VM Folder exists, Overwrite?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (result == DialogResult.No)
                {
                    return;
                }
            }

            try
            {
                Directory.CreateDirectory(txtNewVMPath.Text);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                lblProgress.Text = "Duplication failed: Unable to create VM folder.";
                return;
            }

            pbarMain.Value = 10;



            // Copy VM files
            lblProgress.Text = "Copying VM files";
            lblProgress.Update();

            try
            {
                for (int i = 1; i <= vm.HardDiskConnections.Count; i++)
                {
                    FileInfo vhdfi = new FileInfo(vm.HardDiskConnections[i].HardDisk.File);
                    File.Copy(vm.HardDiskConnections[i].HardDisk.File, txtNewVMPath.Text + @"\" + vhdfi.Name, true);

                    FileInfo undofi = new FileInfo(vm.HardDiskConnections[i].UndoHardDisk.File);
                    File.Copy(vm.HardDiskConnections[i].UndoHardDisk.File, txtNewVMPath.Text + @"\" + undofi.Name, true);
                }

                FileInfo vmfi = new FileInfo(vm.File);
                File.Copy(vm.File, txtNewVMPath.Text + @"\" + txtNewVMName.Text + ".vmc", true);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                lblProgress.Text = "Duplication failed: Unable to copy VM files.";
                return;
            }


            pbarMain.Value = 40;


            // Adjust new VM settings
            lblProgress.Text = "Adjust new VM settings";

            pbarMain.Value = 60;


            // Register new VM
            if (ckbRegisterVM.Checked)
            {
                lblProgress.Text = "Register VM...";
                lblProgress.Update();
                Console.WriteLine(txtNewVMPath.Text + @"\" + txtNewVMName.Text + ".vmc");
                try
                {
                    newvm = vs.RegisterVirtualMachine(txtNewVMName.Text, txtNewVMPath.Text);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                    lblProgress.Text = "Duplication failed: Unable to register VM.";
                    return;
                }
            }
            pbarMain.Value = 80;


            // Turn On VM
            if (ckbTurnOnVM.Checked)
            {
                lblProgress.Text = "Turning on VM...";
                lblProgress.Update();
                try
                {
                    VMTask task = newvm.Startup();
                    while (!task.IsComplete)
                    {
                        Thread.Sleep(200);
                    }
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                    lblProgress.Text = "Duplication failed: Unable to turn on VM.";
                    return;
                }
            }

            pbarMain.Value = 100;

            lblProgress.Text = "Duplication Finished Successfully.";
        }
Exemplo n.º 8
0
        public static DataGrid getVSViewGrid(VMVirtualServer vs, DataGrid dg)
        {
            DataTable dt           = new DataTable("VSView");
            ArrayList bmpCPUUsage  = new ArrayList();
            ArrayList bmpThumbnail = new ArrayList();



            //create a datatable with columns
            dt.Columns.Add(new DataColumn("Thumbnail", typeof(int)));
            dt.Columns.Add(new DataColumn("VM"));
            dt.Columns.Add(new DataColumn("CPUUsage", typeof(int)));
            dt.Columns.Add(new DataColumn("Info"));

            if (vs.VirtualMachines.Count > 0)
            {
                // Get each row
                VMVirtualMachine vm = null;
                for (int i = 0; i < vs.VirtualMachines.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    vm = vs.VirtualMachines[i + 1];
                    bmpCPUUsage.Add(Utility.GenerateCPUUsage(vm, vs, CPUUsageSize.Normal));
                    bmpThumbnail.Add(Utility.GenerateThumbnail(vm.Display));

                    dr[0] = i;
                    dr[1] = "\n" + vm.Name + "\n(" + Utility.ConvertVMStateToString(vm.State) + ")";
                    dr[2] = i;
                    dr[3] = "\n" + "RAM: " + vm.Memory.ToString() + " MB" +
                            "\n" + "Running Time: " + Utility.ConvertUptimeToString(vm.Accountant.UpTime);
                    dt.Rows.Add(dr);
                }
            }
            dt.DefaultView.AllowNew = false;


//			if ( dt.Rows.Count>0)
//			{
            // Define table style
            DataGridTableStyle tableStyle = new DataGridTableStyle();

            tableStyle.MappingName = "VSView";

            // Add column styles
            // Graphic column
            DataGridImageCell imgtbc;
            // TextBox Column
            DataGridTextBoxColumn tbc;

            // Thumbnail column
            imgtbc             = new DataGridImageCell();
            imgtbc.MappingName = "Thumbnail";
            imgtbc.HeaderText  = "Thumbnail";
            imgtbc.Alignment   = HorizontalAlignment.Center;
            imgtbc.theImages   = bmpThumbnail;
            imgtbc.Width       = 64;
            tableStyle.GridColumnStyles.Add(imgtbc);

            // VM Name column
            tbc                       = new DataGridTextBoxColumn();
            tbc.MappingName           = "VM";
            tbc.HeaderText            = "VM";
            tbc.Alignment             = HorizontalAlignment.Center;
            tbc.TextBox.Multiline     = true;
            tbc.TextBox.WordWrap      = true;
            tbc.TextBox.AcceptsReturn = true;
            tbc.TextBox.Enabled       = false;
            tbc.TextBox.TextAlign     = HorizontalAlignment.Left;
            tbc.Width                 = 200;
            tableStyle.GridColumnStyles.Add(tbc);

            // CPU Usage column
            imgtbc             = new DataGridImageCell();
            imgtbc.MappingName = "CPUUsage";
            imgtbc.HeaderText  = "CPUUsage";
            imgtbc.Alignment   = HorizontalAlignment.Center;
            imgtbc.theImages   = bmpCPUUsage;
            imgtbc.Width       = 120;
            tableStyle.GridColumnStyles.Add(imgtbc);

            // VM Info column
            tbc                       = new DataGridTextBoxColumn();
            tbc.MappingName           = "Info";
            tbc.HeaderText            = "Info";
            tbc.Alignment             = HorizontalAlignment.Center;
            tbc.TextBox.Multiline     = true;
            tbc.TextBox.WordWrap      = true;
            tbc.TextBox.AcceptsReturn = true;
            tbc.TextBox.Enabled       = false;
            tbc.TextBox.TextAlign     = HorizontalAlignment.Right;                                      // Overwritten by tbc.Alignment
            tbc.Width                 = 368;
            tableStyle.GridColumnStyles.Add(tbc);
            tableStyle.PreferredRowHeight = 50;
            tableStyle.AllowSorting       = false;
            dt.DefaultView.AllowNew       = false;

            dg.DataSource = null;
            dg.TableStyles.Clear();
            dg.TableStyles.Add(tableStyle);
            dg.DataSource   = dt;
            dg.AllowSorting = false;
            dg.ReadOnly     = true;
            dg.Enabled      = true;

//			}

            return(dg);
        }
Exemplo n.º 9
0
        public static Bitmap GenerateCPUUsage(VMVirtualMachine vm, VMVirtualServer vs, CPUUsageSize enum_scale)
        {
            Bitmap bmpCPUUsage = null;
            int    scale       = (int)enum_scale;

            double ReservedCPU = Math.Floor(double.Parse(vm.Accountant.reservedSystemCapacity.ToString())) * vs.HostInfo.LogicalProcessorCount;

            ReservedCPU = ReservedCPU * 0.5 * scale;
            if (ReservedCPU > 50 * scale - 1)
            {
                ReservedCPU = 50 * scale - 1;
            }

            double MaximumCPU = Math.Floor(double.Parse(vm.Accountant.MaximumSystemCapacity.ToString())) * vs.HostInfo.LogicalProcessorCount;

            MaximumCPU = MaximumCPU * 0.5 * scale;
            if (MaximumCPU > 50 * scale - 1)
            {
                MaximumCPU = 50 * scale - 1;
            }

            int[] CPUHistory = new int[122];

            // Get the first 2 values
            int count = 0;

            foreach (object cpu in ((System.Array)vm.Accountant.CPUUtilizationHistory))
            {
                CPUHistory[count] = int.Parse(cpu.ToString()) / 2 * scale;
                break;
            }

            // Create value array
            count = 1;
            foreach (object cpu in (IEnumerable)((System.Array)vm.Accountant.CPUUtilizationHistory))
            {
                CPUHistory[count + 1] = int.Parse(cpu.ToString()) / 2 * scale;
                CPUHistory[count]     = (CPUHistory[count + 1] + CPUHistory[count - 1]) / 2;
                count += 2;
            }
            CPUHistory[120] = CPUHistory[119];

            // Get a byte array to store the bitmap. Initial values are 0.
            // Every element has 3 bytes. Plus a 54 bytes Bitmap header.

            int bitmapSize = 18000 * scale + 54;

            byte[] bitmapdata = new Byte[bitmapSize];

            // Bitmap header.
            bitmapdata[0]  = 0x42;                                              // Header ("BM")
            bitmapdata[1]  = 0x4D;                                              // Header
            bitmapdata[2]  = (byte)(bitmapSize & 0xFF);                         // File size lowbit (Image size + Header size)
            bitmapdata[3]  = (byte)(bitmapSize & 0xFF00 / 0x100);               // File size highbit
            bitmapdata[10] = 0x36;                                              //
            bitmapdata[14] = 0x28;                                              // specifies the size of the BITMAPINFOHEADER structure, in bytes.
            bitmapdata[18] = 0x78;                                              // Image width 120 pixels
            bitmapdata[22] = (byte)(50 * scale);                                // Image height 100 pixels (0-99)
            bitmapdata[26] = 1;                                                 // Frame count
            bitmapdata[28] = 24;                                                // Bit depth
            bitmapdata[34] = (byte)((bitmapSize - 54) & 0xFF);                  // Image size lowbit
            bitmapdata[35] = (byte)((bitmapSize - 54) & 0xFF00 / 0x100);        // Image size highbit

            System.IO.MemoryStream streamBitmap = new  System.IO.MemoryStream(bitmapdata);
            bmpCPUUsage = new Bitmap((Bitmap)Image.FromStream(streamBitmap));

            Graphics hG1 = Graphics.FromImage(bmpCPUUsage);
            Pen      pen = new Pen(Color.Yellow);

            for (int i = 0; i < 120; i++)
            {
                // Baseline
                bmpCPUUsage.SetPixel(i, 10 * scale, Color.Green);
                bmpCPUUsage.SetPixel(i, 20 * scale, Color.Green);
                bmpCPUUsage.SetPixel(i, 30 * scale, Color.Green);
                bmpCPUUsage.SetPixel(i, 40 * scale, Color.Green);

                // Reserved and Maximum Line
                bmpCPUUsage.SetPixel(i, (int)ReservedCPU, Color.Blue);
                bmpCPUUsage.SetPixel(i, (int)MaximumCPU, Color.Blue);

                // CPU Usage
                hG1.DrawLine(pen, i, CPUHistory[i] + 1, i + 1, CPUHistory[i + 1] + 1);
            }

            // Rotate and flip the image
            bmpCPUUsage.RotateFlip(RotateFlipType.Rotate180FlipX);



            return(bmpCPUUsage);
        }
Exemplo n.º 10
0
        private void ServiceTick()
        {
            try
            {
                string  strServer = Environment.MachineName;
                DataSet ds        = oWorkstation.GetRemoteVirtuals(strServer);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int    intID          = Int32.Parse(dr["id"].ToString());
                    int    intEnvironment = Int32.Parse(dr["environment"].ToString());
                    string strINI         = @"\\citrixfile.ntl-city.com\flexcfg$\vwcfg\VW.INI";
                    if (intEnvironment != 4)
                    {
                        strINI = @"\\citrixfiletest.tstctr.ntl-city.net\flexcfg$\vwcfg\VW.INI";
                    }
                    string strImageScan = @"\\citrixfile.ntl-city.com\flexcfg$\vwcfg\BuildScripts\VWH_IMAGESCAN_SECURITY.vbs";
                    if (intEnvironment != 4)
                    {
                        strImageScan = @"\\citrixfiletest.tstctr.ntl-city.net\flexcfg$\vwcfg\BuildScripts\VWH_IMAGESCAN_SECURITY.vbs";
                    }
                    AD     oAD              = new AD(0, "", intEnvironment);
                    int    intAnswer        = Int32.Parse(dr["answerid"].ToString());
                    string strHost          = dr["hostname"].ToString();
                    string strVirtualDir    = dr["virtualdir"].ToString();
                    string strZipPath       = dr["gzippath"].ToString();
                    string strImage         = dr["image"].ToString();
                    string strName          = dr["name"].ToString();
                    string strSerial        = dr["serial"].ToString();
                    int    intStep          = Int32.Parse(dr["step"].ToString());
                    int    intRam           = Int32.Parse(dr["ram"].ToString());
                    string strManager       = dr["manager"].ToString();
                    string strGroupRemote   = "GSGu_WKS" + strName + "RemoteA";
                    string strGroupRemote2K = "GSGu_WKS" + strName + "Remote2K";
                    string strGroupAdmin    = "GSGu_WKS" + strName + "Adm";
                    string strGroupW        = "GSGw_" + strName;
                    string strResult        = "";
                    string strVBS           = strScripts + "scripts\\" + intID + ".vbs";
                    string strVBSImageScan  = strScripts + "scripts\\" + intID + "_imagescan.vbs";
                    bool   bool2000         = (strName.ToUpper().StartsWith("T2K") == true || strName.ToUpper().StartsWith("W2K") == true);
                    if (intStep > 0)
                    {
                        Type   typeVSClass           = typeof(VMVirtualServerClass);
                        Type   typeDCOM              = Type.GetTypeFromCLSID(typeVSClass.GUID, strHost, true);
                        object objDCOM               = Activator.CreateInstance(typeDCOM);
                        VMVirtualServerClass oServer = (VMVirtualServerClass)System.Runtime.InteropServices.Marshal.CreateWrapperOfType(objDCOM, typeVSClass);
                        switch (intStep)
                        {
                        case -1:
                            // Rebuild Workstation
                            if (intLogging > 0)
                            {
                                oLog.WriteEntry(String.Format("Starting rebuild of workstation " + strName), EventLogEntryType.Information);
                            }
                            VMVirtualMachine oMachineDown = oServer.FindVirtualMachine(strName);
                            if (oMachineDown.State != VMVMState.vmVMState_TurningOff && oMachineDown.State != VMVMState.vmVMState_TurnedOff)
                            {
                                oMachineDown.TurnOff();
                            }
                            if (oMachineDown.State == VMVMState.vmVMState_TurnedOff)
                            {
                                string strDirectory = strVirtualDir + "\\" + strName;
                                Directory.Delete(strDirectory);
                                oWorkstation.NextRemoteVirtual(intID);
                                oWorkstation.NextRemoteVirtual(intID);
                                oWorkstation.NextRemoteVirtual(intID);
                                oWorkstation.NextRemoteVirtual(intID);
                            }
                            break;

                        case 1:         // Step 1 - Selecting Host
                            // Should be taken care of when adding remote workstation
                            break;

                        case 2:         // Step 2 - Configuring Accounts
                            // User intervention required
                            break;

                        case 3:         // Step 3 - Initiating Build
                            if (Directory.Exists(strVirtualDir + "\\" + strName) == false)
                            {
                                Directory.CreateDirectory(strVirtualDir + "\\" + strName);
                            }
                            oWorkstation.NextRemoteVirtual(intID);
                            break;

                        case 4:         // Step 4 - Building VHD
                            if (strZipPath == "")
                            {
                                Zip oZip = new Zip();
                                oZip.ExtractZipFile(strImage, strVirtualDir + "\\" + strName);
                                oWorkstation.NextRemoteVirtual(intID);
                            }
                            else
                            {
                                if (intLogging > 0)
                                {
                                    oLog.WriteEntry(String.Format("Starting step to create VHD for " + strName), EventLogEntryType.Information);
                                }
                                string strDirectory = strVirtualDir + "\\" + strName + "\\";
                                string strFile      = strDirectory + "DriveCXP.vhd";
                                if (bool2000 == true)
                                {
                                    strFile = strDirectory + "DriveC2K.vhd";
                                }
                                string strRename = strDirectory + "DriveCXP.rename";
                                if (bool2000 == true)
                                {
                                    strRename = strDirectory + "DriveC2K.rename";
                                }
                                if (Process.GetProcessesByName("gzip").GetLength(0) == 0)
                                {
                                    // GZIP not running - check to see if we have already extracted this file
                                    if (File.Exists(strFile))
                                    {
                                        // File exists - let's move on
                                        oWorkstation.NextRemoteVirtual(intID);
                                    }
                                    else
                                    {
                                        // Add code to only run if between 9PM and 6AM
                                        if (boolProduction == false || DateTime.Now.Hour >= 21 || DateTime.Now.Hour <= 6)
                                        {
                                            if (intLogging > 0)
                                            {
                                                oLog.WriteEntry(String.Format("Beginning extraction of VHD for " + strName), EventLogEntryType.Information);
                                            }
                                            // File does not exist, begin unzip
                                            StreamWriter oWriter = new StreamWriter(strVBS);
                                            oWriter.WriteLine("Dim objShell");
                                            oWriter.WriteLine("Set objShell = CreateObject(\"WScript.Shell\")");
                                            // FORMAT: objShell.Run("cmd.exe /c F:\WXP_SysPrep\gzip -d -k -c ""F:\WXP_SysPrep\WXP Pro SP2 CORPTEST.vhd.gz"" > R:\vsfiles\LUN01\WXP\TESTHEALY\DriveCXP.vhd")
                                            oWriter.WriteLine("objShell.Run(\"cmd.exe /c " + strZipPath + " -d -k -c \"\"" + strImage + "\"\" > " + strFile + "\")");
                                            oWriter.WriteLine("Set objShell = Nothing");
                                            oWriter.Flush();
                                            oWriter.Close();
                                            ILaunchScript oScript = new SimpleLaunchWsh(strVBS, "", true) as ILaunchScript;
                                            oScript.Launch();
                                            if (intLogging > 0)
                                            {
                                                oLog.WriteEntry(String.Format("FINISHED extraction of VHD for " + strName), EventLogEntryType.Information);
                                            }
                                        }
                                        else if (intLogging > 0)
                                        {
                                            oLog.WriteEntry(String.Format("Waiting until 9PM to start extraction of VHD for " + strName), EventLogEntryType.Information);
                                        }
                                    }
                                }
                                else if (intLogging > 0)
                                {
                                    oLog.WriteEntry(String.Format("GZIP already running - will not create VHD for " + strName), EventLogEntryType.Information);
                                }
                            }
                            break;

                        case 5:         // Step 5 - Creating Virtual Machine
                            oServer.CreateVirtualMachine(strName, strVirtualDir + "\\" + strName);
                            oWorkstation.NextRemoteVirtual(intID);
                            //    break;
                            //case 6:   // Step 6 - Modifying RAM
                            VMVirtualMachine oMachine = oServer.FindVirtualMachine(strName);
                            oMachine.Memory = intRam;
                            oMachine.ShutdownActionOnQuit = VMShutdownAction.vmShutdownAction_Shutdown;
                            oWorkstation.NextRemoteVirtual(intID);
                            //    break;
                            //case 7:   // Step 7 - Adding CD Drive
                            oMachine.AddDVDROMDrive(VMDriveBusType.vmDriveBusType_IDE, 1, 0);
                            oWorkstation.NextRemoteVirtual(intID);
                            //    break;
                            //case 8:   // Step 8 - Connecting Virtual Disks
                            VMSCSIController oSCSI = oMachine.AddSCSIController();
                            oSCSI.Configure(false, 7);
                            if (bool2000 == true)
                            {
                                oMachine.AddHardDiskConnection(strVirtualDir + "\\" + strName + "\\DriveC2K.vhd", VMDriveBusType.vmDriveBusType_SCSI, 0, 0);
                            }
                            else
                            {
                                oMachine.AddHardDiskConnection(strVirtualDir + "\\" + strName + "\\DriveCXP.vhd", VMDriveBusType.vmDriveBusType_SCSI, 0, 0);
                            }
                            oWorkstation.NextRemoteVirtual(intID);
                            break;

                        case 9:         // Step 9 - Configuring Network
                            VMVirtualMachine oMachineNetwork = oServer.FindVirtualMachine(strName);
                            foreach (VMNetworkAdapter oAdap in oMachineNetwork.NetworkAdapters)
                            {
                                foreach (VMVirtualNetwork oNet in oServer.VirtualNetworks)
                                {
                                    if (oNet.Name.ToUpper() == strNetwork.ToUpper())
                                    {
                                        oAdap.AttachToVirtualNetwork(oNet);
                                    }
                                }
                            }
                            oWorkstation.NextRemoteVirtual(intID);
                            break;

                        case 10:        // Step 10 - Updating Serial Number
                            VMVirtualMachine oMachineSerial = oServer.FindVirtualMachine(strName);
                            oMachineSerial.BaseBoardSerialNumber = strSerial;
                            oMachineSerial.ChassisSerialNumber   = strSerial;
                            oMachineSerial.BIOSSerialNumber      = strSerial;
                            oWorkstation.NextRemoteVirtual(intID);
                            break;

                        case 11:        // Step 11 - Creating AD groups
                            // Create RemoteA group in OUg_Resources/OUc_WksMgmt (not for Windows 2000 workstations)
                            if (bool2000 == false)
                            {
                                strResult = oAD.CreateGroup(strGroupRemote, "Remote access group for virtual workstation", "Requires approval from one of the following:" + Environment.NewLine + "               " + strManager, "OU=OUc_WksMgmt,OU=OUg_Resources,", "GG", "S");
                                if (strResult.Trim() != "")
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                }
                                if (intLogging > 0)
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                }
                            }
                            else
                            {
                                strResult = oAD.CreateGroup(strGroupRemote2K, "Remote access group for virtual workstation", "Requires approval from one of the following:" + Environment.NewLine + "               " + strManager, "OU=OUc_WksMgmt,OU=OUg_Resources,", "GG", "S");
                                if (strResult.Trim() != "")
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                }
                                if (intLogging > 0)
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                }
                            }
                            if (intEnvironment != (int)CurrentEnvironment.CORPDMN && intEnvironment != (int)CurrentEnvironment.PNCNT_PROD)     // Not Production
                            {
                                // Create Adm group in OUc_Orgs/OUg_Admins
                                strResult = oAD.CreateGroup(strGroupAdmin, "Virtual Workstation; " + strManager, "", "OU=OUg_Admins,OU=OUc_Orgs,", "GG", "S");
                                if (strResult.Trim() != "")
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                }
                                if (intLogging > 0)
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                }
                                // Create GSGw group in OUg_Resources
                                strResult = oAD.CreateGroup(strGroupW, "Virtual Workstation; " + strManager, "", "OU=OUg_Resources,", "GG", "S");
                                if (strResult.Trim() != "")
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                }
                                if (intLogging > 0)
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                }
                                // Create Workstation Object in OUc_DmnCptrs/OUc_Wksts/OUw_Standard
                                strResult = oAD.CreateWorkstation(strName, "Virtual Workstation; " + strManager, "", "OU=OUw_Standard,OU=OUc_Wksts,OU=OUc_DmnCptrs,");
                                if (strResult.Trim() != "")
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                }
                                if (intLogging > 0)
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                }
                                // Add workstation to GSGw group
                                strResult = oAD.JoinGroup(strName, strGroupW, 0);
                                if (strResult.Trim() != "")
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                }
                                if (intLogging > 0)
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                }
                            }
                            oWorkstation.NextRemoteVirtual(intID);
                            break;

                        case 12:
                            // Step 12 - Adding AD accounts
                            DataSet dsAccounts = oWorkstation.GetRemoteAccounts(strName);
                            foreach (DataRow drAccount in dsAccounts.Tables[0].Rows)
                            {
                                string strID = drAccount["xid"].ToString();
                                if (intEnvironment != (int)CurrentEnvironment.CORPDMN && intEnvironment != (int)CurrentEnvironment.PNCNT_PROD)     // Not Production
                                {
                                    strID = "E" + strID.Substring(1);
                                    if (oAD.Search(strID, false) == null)
                                    {
                                        strID = "T" + strID.Substring(1);
                                        if (oAD.Search(strID, false) == null)
                                        {
                                            strResult = oAD.CreateUser(strID, strID, strID, "Abcd1234", "", "", "");
                                            if (strResult.Trim() != "")
                                            {
                                                oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                            }
                                            if (intLogging > 0)
                                            {
                                                oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                            }
                                        }
                                    }
                                    if (drAccount["admin"].ToString() == "1")
                                    {
                                        strResult = oAD.JoinGroup(strID, strGroupAdmin, 0);
                                        if (strResult.Trim() != "")
                                        {
                                            oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                        }
                                        if (intLogging > 0)
                                        {
                                            oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                        }
                                    }
                                }
                                if (drAccount["remote"].ToString() == "1")
                                {
                                    if (bool2000 == false)
                                    {
                                        strResult = oAD.JoinGroup(strID, strGroupRemote, 0);
                                        if (strResult.Trim() != "")
                                        {
                                            oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                        }
                                        if (intLogging > 0)
                                        {
                                            oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                        }
                                    }
                                    else
                                    {
                                        strResult = oAD.JoinGroup(strID, strGroupRemote2K, 0);
                                        if (strResult.Trim() != "")
                                        {
                                            oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                        }
                                        if (intLogging > 0)
                                        {
                                            oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                        }
                                    }
                                }
                            }
                            oWorkstation.NextRemoteVirtual(intID);
                            break;

                        case 13:
                            // Step 13 - Starting Machine
                            VMVirtualMachine oMachineStart = oServer.FindVirtualMachine(strName);
                            oMachineStart.Startup();
                            oWorkstation.NextRemoteVirtual(intID);
                            // Update INI File
                            DataSet dsINI = oWorkstation.GetRemoteAccounts(strName);
                            foreach (DataRow drINI in dsINI.Tables[0].Rows)
                            {
                                string strID = drINI["xid"].ToString();
                                if (intEnvironment != (int)CurrentEnvironment.CORPDMN && intEnvironment != (int)CurrentEnvironment.PNCNT_PROD)     // Not Production
                                {
                                    strID = "E" + strID.Substring(1);
                                    if (oAD.Search(strID, false) == null)
                                    {
                                        strID = "T" + strID.Substring(1);
                                    }
                                }
                                if (drINI["remote"].ToString() == "1")
                                {
                                    // FORMAT: EMJY13C=TXPVM001,XP,OHCLEVWH4004
                                    bool         boolAdd = true;
                                    string       strLine = strID + "=" + strName + "," + (bool2000 ? "2000" : "XP") + "," + strHost;
                                    StreamReader oINIr   = new StreamReader(strINI);
                                    while (oINIr.Peek() != -1)
                                    {
                                        string strCompare = oINIr.ReadLine();
                                        if (strCompare == strLine)
                                        {
                                            boolAdd = false;
                                            break;
                                        }
                                    }
                                    oINIr.Close();
                                    if (boolAdd == true)
                                    {
                                        StreamWriter oINI = new StreamWriter(strINI, true);
                                        oINI.WriteLine(strLine);
                                        oINI.Flush();
                                        oINI.Close();
                                    }
                                }
                            }
                            break;

                        case 14:
                            // Step 14 - ZEUS
                            break;

                        case 15:
                            // Step 15 - Installing Applications
                            break;

                        case 16:
                            // Step 16 - Complete Request
                            // Run ImageScan script
                            File.Copy(strImageScan, strVBSImageScan, true);
                            ILaunchScript oScriptImageScan = new SimpleLaunchWsh(strVBSImageScan, "", true) as ILaunchScript;
                            oScriptImageScan.Launch();
                            // Delete Files
                            if (File.Exists(strVBS) == true)
                            {
                                File.Delete(strVBS);
                            }
                            if (File.Exists(strVBSImageScan) == true)
                            {
                                File.Delete(strVBSImageScan);
                            }
                            // Reboot the workstation
                            VMVirtualMachine oMachineRestart = oServer.FindVirtualMachine(strName);
                            oMachineRestart.Reset();
                            oWorkstation.UpdateRemoteVirtualCompleted(intID);
                            break;
                        }
                    }
                }


                ds = oWorkstation.GetRemoteVirtualDecoms(strServer);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int    intID          = Int32.Parse(dr["id"].ToString());
                    int    intEnvironment = Int32.Parse(dr["environment"].ToString());
                    string strINI         = @"\\citrixfile.ntl-city.com\flexcfg$\vwcfg\VW.INI";
                    if (intEnvironment != 4)
                    {
                        strINI = @"\\citrixfiletest.tstctr.ntl-city.net\flexcfg$\vwcfg\VW.INI";
                    }
                    string strINITemp = @"\\citrixfile.ntl-city.com\flexcfg$\vwcfg\VW_TEMP.INI";
                    if (intEnvironment != 4)
                    {
                        strINITemp = @"\\citrixfiletest.tstctr.ntl-city.net\flexcfg$\vwcfg\VW_TEMP.INI";
                    }
                    AD     oAD                    = new AD(0, "", intEnvironment);
                    string strVirtualDir          = dr["virtualdir"].ToString();
                    string strName                = dr["name"].ToString();
                    string strGroupRemote         = "GSGu_WKS" + strName + "RemoteA";
                    string strGroupRemote2K       = "GSGu_WKS" + strName + "Remote2K";
                    string strGroupAdmin          = "GSGu_WKS" + strName + "Adm";
                    string strGroupW              = "GSGw_" + strName;
                    string strResult              = "";
                    bool   bool2000               = (strName.ToUpper().StartsWith("T2K") == true || strName.ToUpper().StartsWith("W2K") == true);
                    Type   typeVSClass            = typeof(VMVirtualServerClass);
                    Type   typeDCOM               = Type.GetTypeFromCLSID(typeVSClass.GUID, strServer, true);
                    object objDCOM                = Activator.CreateInstance(typeDCOM);
                    VMVirtualServerClass oServer  = (VMVirtualServerClass)System.Runtime.InteropServices.Marshal.CreateWrapperOfType(objDCOM, typeVSClass);
                    VMVirtualMachine     oMachine = oServer.FindVirtualMachine(strName);
                    if (oMachine.State == VMVMState.vmVMState_TurnedOff)
                    {
                        // Remove Workstation
                        oServer.DeleteVirtualMachine(oMachine);
                        if (intLogging > 0)
                        {
                            oLog.WriteEntry("Workstation " + strName + " has been deleted", EventLogEntryType.Information);
                        }
                        // Delete Folder
                        DeleteDirectory(strVirtualDir + "\\" + strName);
                        if (intLogging > 0)
                        {
                            oLog.WriteEntry("The directory for workstation " + strName + " has been deleted", EventLogEntryType.Information);
                        }
                        // Delete AD Objects
                        DirectoryEntry oGroup;
                        if (bool2000 == false)
                        {
                            oGroup = oAD.GroupSearch(strGroupRemote);
                            if (oGroup != null)
                            {
                                strResult = oAD.Delete(oGroup);
                                if (strResult.Trim() != "")
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                }
                                if (intLogging > 0)
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                }
                            }
                        }
                        else
                        {
                            oGroup = oAD.GroupSearch(strGroupRemote2K);
                            if (oGroup != null)
                            {
                                strResult = oAD.Delete(oGroup);
                                if (strResult.Trim() != "")
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                }
                                if (intLogging > 0)
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                }
                            }
                        }
                        if (intEnvironment != (int)CurrentEnvironment.CORPDMN && intEnvironment != (int)CurrentEnvironment.PNCNT_PROD) // Not Production
                        {
                            oGroup = oAD.GroupSearch(strGroupAdmin);
                            if (oGroup != null)
                            {
                                strResult = oAD.Delete(oGroup);
                                if (strResult.Trim() != "")
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                }
                                if (intLogging > 0)
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                }
                            }
                            oGroup = oAD.GroupSearch(strGroupW);
                            if (oGroup != null)
                            {
                                strResult = oAD.Delete(oGroup);
                                if (strResult.Trim() != "")
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                }
                                if (intLogging > 0)
                                {
                                    oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                }
                            }
                            SearchResultCollection oComputers = oAD.ComputerSearch(strName);
                            if (oComputers.Count > 0)
                            {
                                oGroup = oComputers[0].GetDirectoryEntry();
                                if (oGroup != null)
                                {
                                    strResult = oAD.Delete(oGroup);
                                    if (strResult.Trim() != "")
                                    {
                                        oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Warning);
                                    }
                                    if (intLogging > 0)
                                    {
                                        oLog.WriteEntry(String.Format(strResult), EventLogEntryType.Information);
                                    }
                                }
                            }
                        }
                        if (intLogging > 0)
                        {
                            oLog.WriteEntry("The AD groups for workstation " + strName + " have been deleted", EventLogEntryType.Information);
                        }
                        // Update the INI File
                        string       strCompare = "=" + strName + "," + (bool2000 ? "2000" : "XP") + "," + strServer;
                        StreamWriter oINI       = new StreamWriter(strINITemp, true);
                        StreamReader oINIr      = new StreamReader(strINI);
                        while (oINIr.Peek() != -1)
                        {
                            string strLine = oINIr.ReadLine();
                            if (strLine.EndsWith(strCompare) == false)
                            {
                                oINI.WriteLine(strLine);
                            }
                        }
                        oINIr.Close();
                        oINI.Flush();
                        oINI.Close();
                        if (File.Exists(strINI) == true)
                        {
                            File.Delete(strINI);
                        }
                        File.Copy(strINITemp, strINI, true);
                        if (File.Exists(strINITemp) == true)
                        {
                            File.Delete(strINITemp);
                        }
                        if (intLogging > 0)
                        {
                            oLog.WriteEntry("The INI file [" + strINI + "] was updated", EventLogEntryType.Information);
                        }
                        oWorkstation.UpdateRemoteVirtualDecomCompleted(intID);
                        if (intLogging > 0)
                        {
                            oLog.WriteEntry("The workstation " + strName + " has been decommissioned", EventLogEntryType.Information);
                        }
                    }
                    else if (oMachine.State != VMVMState.vmVMState_TurningOff)
                    {
                        oMachine.TurnOff();
                        if (intLogging > 0)
                        {
                            oLog.WriteEntry("The workstation " + strName + " is turning off", EventLogEntryType.Information);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                oLog.WriteEntry(String.Format(ex.Message) + " [" + System.Environment.UserName + "]", EventLogEntryType.Error);
            }
        }