Exemplo n.º 1
0
        public static bool SetSerialPort(this ManagementObject VM, string PipeName, int PortNumber = 1)
        {
            // Sanity Check
            if (VM == null || !VM["__CLASS"].ToString().Equals(VMStrings.ComputerSystem, StringComparison.InvariantCultureIgnoreCase))
            {
                return(false);
            }

            ManagementObject SP = VM.GetDevices().FirstOrDefault(D => D["ResourceSubType"].ToString()
                                                                 .Equals(ResourceSubTypes.SerialPort, StringComparison.InvariantCultureIgnoreCase) &&
                                                                 D["Caption"].ToString().EndsWith("" + PortNumber));

            if (SP == null)
            {
                return(false);
            }

            SP["Connection"] = new string[] { PipeName };
            return(VM.ModifyDevice(SP));
        }
Exemplo n.º 2
0
        public static IEnumerable <string> GetVHDs(this ManagementObject VM)
        {
            List <string> vhds = new List <string>();

            switch (VM["__CLASS"].ToString().ToUpperInvariant())
            {
            case "MSVM_COMPUTERSYSTEM":
            case "MSVM_VIRTUALSYSTEMSETTINGDATA":
                vhds.AddRange(VM.GetDevices().Where(Dev => (Dev["ElementName"].ToString().Equals(
                                                                "Hard Disk Image"))).Select(HD => ((String[])(HD["Connection"])).First()));
                break;

            case "MSVM_RESOURCEALLOCATIONSETTINGDATA":
                if (VM["ElementName"].ToString().Equals("Hard Disk Image", StringComparison.InvariantCultureIgnoreCase))
                {
                    vhds.Add(((String[])(VM["Connection"])).First());
                }
                break;

            default:
                break;
            }
            return(vhds);
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="VM"></param>
        /// <param name="SourceVHD"></param>
        /// <param name="ControllerDevice">Controller device to attach to.  If null, will attach to first available contoller discovered on VM.</param>
        /// <param name="ControllerAddress">Assign VHD to this address on the Controller Device.</param>
        /// <returns></returns>
        public static bool AttachVHD(this ManagementObject VM, string SourceVHD, ManagementObject ControllerDevice = null, int ControllerAddress = -1)
        {
            switch (VM["__CLASS"].ToString().ToUpperInvariant())
            {
            case "MSVM_COMPUTERSYSTEM":
                ControllerDevice = ControllerDevice ??
                                   (VM.GetDevices()
                                    .Where(
                                        device =>
                                        device["ResourceSubType"].ToString()
                                        .Equals(ResourceSubTypes.ControllerIDE)
                                        ||
                                        device["ResourceSubType"].ToString()
                                        .Equals(ResourceSubTypes.ControllerSCSI))).Where(each =>
                {
                    var drives =
                        VM.GetDevices()
                        .Where(
                            dev =>
                            ushort.Parse(dev["ResourceType"].ToString()) ==
                            (ushort)ResourceTypes.Disk &&
                            String.Equals(dev["Parent"].ToString(), each.Path.Path));
                    return
                    (each["ResourceSubType"].ToString()
                     .Equals(ResourceSubTypes.ControllerIDE)
                                                           ? drives.Count() < 2
                                                           : drives.Count() < 64);
                })
                                   .FirstOrDefault();
                if (ControllerDevice == null)
                {
                    return(false);     // Could not locate an open controller to attach to.
                }
                // Locate the address on the controller we need to assign to...
                IEnumerable <ManagementObject> set;
                ControllerAddress = ControllerAddress >= 0
                                            ? ControllerAddress
                                            : (set = (VM.GetDevices().Where(each => (ushort)each["ResourceType"] == (ushort)ResourceTypes.Disk &&
                                                                            String.Equals(each["Parent"].ToString(), ControllerDevice.Path.Path))
                                                      .OrderBy(each => int.Parse(each["Address"].ToString())))).Any()
                                                                   ? Task <int> .Factory.StartNew(() =>
                {
                    var set2 = set.ToArray();
                    if (
                        int.Parse(
                            set.Last()["Address"].ToString()) ==
                        set.Count() - 1)
                    {
                        return(set.Count());
                    }
                    for (int i = 0; i < set2.Length; i++)
                    {
                        if (
                            int.Parse(
                                set2[i]["Address"].ToString()) >
                            i)
                        {
                            return(i);
                        }
                    }
                    return(set2.Length);
                }, TaskCreationOptions.AttachedToParent).Result
                                    //int.Parse(set.Last()["Address"].ToString()) + 1
                                                                   : 0;



                // Need to add a Synthetic Disk Drive to connect the vhd to...
                ManagementObject SynDiskDefault = VM.NewResource(ResourceTypes.Disk, ResourceSubTypes.SyntheticDisk);
                SynDiskDefault["Parent"]  = ControllerDevice.Path.Path;
                SynDiskDefault["Address"] = ControllerAddress;
                SynDiskDefault["Limit"]   = 1;
                var SynDisk = VM.AddDevice(SynDiskDefault);
                if (SynDisk == null)
                {
                    return(false);
                }


                ManagementObject drive = VM.NewResource(ResourceTypes.StorageExtent, ResourceSubTypes.VHD);
                drive["Connection"] = new string[] { SourceVHD };
                drive["Parent"]     = SynDisk.Path.Path;
                var ret = VM.AddDevice(drive);
                return(ret != null);

            default:
                return(false);
            }
        }