Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // display welcome screen
            DisplayWelcome();

            // parse parameters
            if (!ParseParameters(args))
            {
                DisplayUsage();
                return;
            }

            // connect WMI
            wmi = new Wmi(computer, ns);

            try
            {
                // run command
                if (String.Equals(command, "MountVHD", StringComparison.CurrentCultureIgnoreCase))
                    MountVHD();
                else if (String.Equals(command, "UnmountVHD", StringComparison.CurrentCultureIgnoreCase))
                    UnmountVHD();
                else
                {
                    Console.WriteLine("Unknown command: " + command);
                    DisplayUsage();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("\nError: " + ex.Message);
                if (ex.InnerException != null)
                    Console.WriteLine("System message: " + ex.InnerException.Message);
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // display welcome screen
            DisplayWelcome();

            // parse parameters
            if (!ParseParameters(args))
            {
                DisplayUsage();
                return;
            }

            // connect WMI
            wmi = new Wmi(computer, ns);

            try
            {
                // run command
                if (String.Equals(command, "MountVHD", StringComparison.CurrentCultureIgnoreCase))
                {
                    MountVHD();
                }
                else if (String.Equals(command, "UnmountVHD", StringComparison.CurrentCultureIgnoreCase))
                {
                    UnmountVHD();
                }
                else
                {
                    Console.WriteLine("Unknown command: " + command);
                    DisplayUsage();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("\nError: " + ex.Message);
                if (ex.InnerException != null)
                {
                    Console.WriteLine("System message: " + ex.InnerException.Message);
                }
            }
        }
Exemplo n.º 3
0
        private static void MountVHD()
        {
            // check parameters
            AssertParameter("path");
            string vhdPath = Parameters["path"];

            ManagementObject objImgSvc = GetImageManagementService();

            Console.WriteLine("Mount VHD: " + vhdPath);

            // get method params
            ManagementBaseObject inParams = objImgSvc.GetMethodParameters("Mount");
            inParams["Path"] = vhdPath;

            ManagementBaseObject outParams = (ManagementBaseObject)objImgSvc.InvokeMethod("Mount", inParams, null);
            JobResult result = CreateJobResultFromWmiMethodResults(outParams);

            // load storage job
            if (result.ReturnValue != ReturnCode.JobStarted
                || result.Job.JobState == ConcreteJobState.Exception)
            {
                throw new Exception("Mount job failed to start with the following message: " + result.Job.ErrorDescription);
            }

            ManagementObject objJob = wmi.GetWmiObject("msvm_StorageJob", "InstanceID = '{0}'", result.Job.Id);

            if (JobCompleted(result.Job))
            {
                // load output data
                ManagementObject objImage = wmi.GetRelatedWmiObject(objJob, "Msvm_MountedStorageImage");

                int pathId = Convert.ToInt32(objImage["PathId"]);
                int portNumber = Convert.ToInt32(objImage["PortNumber"]);
                int targetId = Convert.ToInt32(objImage["TargetId"]);
                int lun = Convert.ToInt32(objImage["Lun"]);

                string diskAddress = String.Format("Port{0}Path{1}Target{2}Lun{3}", portNumber, pathId, targetId, lun);

                 // find mounted disk using VDS
                Vds.Advanced.AdvancedDisk advancedDisk = null;
                Vds.Pack diskPack = null;

                // first attempt
                System.Threading.Thread.Sleep(3000);
                Console.WriteLine("Querying mounted disk...");
                FindVdsDisk(diskAddress, out advancedDisk, out diskPack);

                // second attempt
                if (advancedDisk == null)
                {
                    System.Threading.Thread.Sleep(20000);
                    Console.WriteLine("Querying mounted disk - second attempt...");
                    FindVdsDisk(diskAddress, out advancedDisk, out diskPack);
                }

                if (advancedDisk == null)
                    throw new Exception("Could not find mounted disk");

                List<string> volumes = new List<string>();

                Console.WriteLine("Disk flags: " + advancedDisk.Flags);

                // clear READONLY
                if ((advancedDisk.Flags & Vds.DiskFlags.ReadOnly) == Vds.DiskFlags.ReadOnly)
                {
                    Console.Write("Clearing disk Read-Only flag...");
                    advancedDisk.ClearFlags(Vds.DiskFlags.ReadOnly);

                    while ((advancedDisk.Flags & Vds.DiskFlags.ReadOnly) == Vds.DiskFlags.ReadOnly)
                    {
                        System.Threading.Thread.Sleep(100);
                        advancedDisk.Refresh();
                    }
                    Console.WriteLine("Done");
                }

                // bring disk ONLINE
                if (advancedDisk.Status == Vds.DiskStatus.Offline)
                {
                    Console.Write("Bringing disk online...");
                    advancedDisk.Online();

                    while (advancedDisk.Status == Vds.DiskStatus.Offline)
                    {
                        System.Threading.Thread.Sleep(100);
                        advancedDisk.Refresh();
                    }

                    Console.WriteLine("Done");
                }

                // determine disk index for DiskPart
                Wmi cimv2 = new Wmi(computer, "root\\CIMV2");
                ManagementObject objDisk = cimv2.GetWmiObject("win32_diskdrive",
                    "Model='Msft Virtual Disk SCSI Disk Device' and ScsiTargetID={0} and ScsiLogicalUnit={1} and scsiPort={2}",
                    targetId, lun, portNumber);

                if (objDisk != null)
                {
                    Console.WriteLine("DiskPart disk index: " + Convert.ToInt32(objDisk["Index"]));
                }

                // find volume
                diskPack.Refresh();
                foreach (Vds.Volume volume in diskPack.Volumes)
                {
                    volumes.Add(volume.DriveLetter.ToString());
                }

                if (volumes.Count == 0 && objDisk != null)
                {
                    // find volumes using WMI
                    foreach (ManagementObject objPartition in objDisk.GetRelated("Win32_DiskPartition"))
                    {
                        foreach (ManagementObject objVolume in objPartition.GetRelated("Win32_LogicalDisk"))
                        {
                            volumes.Add(objVolume["Name"].ToString().TrimEnd(':'));
                        }
                    }
                }

                foreach (string volume in volumes)
                {
                    Console.WriteLine("Volume found: " + volume);
                }

                //// find disk index
                //Wmi win32 = new Wmi(computer, @"root\cimv2");

                //System.Threading.Thread.Sleep(1000); // small pause

                //ManagementObject objDisk = win32.GetWmiObject("win32_DiskDrive",
                //    "Model='Msft Virtual Disk SCSI Disk Device' and ScsiTargetID={0} and ScsiLogicalUnit={1} and ScsiPort={2}",
                //    targetId, lun, portNumber);

                //int diskIndex = Convert.ToInt32(objDisk["Index"]);

                Console.WriteLine("\nDisk has been mounted.\n");
                //Console.WriteLine("Disk index: " + advancedDisk.);
            }
        }
Exemplo n.º 4
0
        private static void MountVHD()
        {
            // check parameters
            AssertParameter("path");
            string vhdPath = Parameters["path"];

            ManagementObject objImgSvc = GetImageManagementService();

            Console.WriteLine("Mount VHD: " + vhdPath);

            // get method params
            ManagementBaseObject inParams = objImgSvc.GetMethodParameters("Mount");

            inParams["Path"] = vhdPath;

            ManagementBaseObject outParams = (ManagementBaseObject)objImgSvc.InvokeMethod("Mount", inParams, null);
            JobResult            result    = CreateJobResultFromWmiMethodResults(outParams);

            // load storage job
            if (result.ReturnValue != ReturnCode.JobStarted ||
                result.Job.JobState == ConcreteJobState.Exception)
            {
                throw new Exception("Mount job failed to start with the following message: " + result.Job.ErrorDescription);
            }

            ManagementObject objJob = wmi.GetWmiObject("msvm_StorageJob", "InstanceID = '{0}'", result.Job.Id);

            if (JobCompleted(result.Job))
            {
                // load output data
                ManagementObject objImage = wmi.GetRelatedWmiObject(objJob, "Msvm_MountedStorageImage");

                int pathId     = Convert.ToInt32(objImage["PathId"]);
                int portNumber = Convert.ToInt32(objImage["PortNumber"]);
                int targetId   = Convert.ToInt32(objImage["TargetId"]);
                int lun        = Convert.ToInt32(objImage["Lun"]);

                string diskAddress = String.Format("Port{0}Path{1}Target{2}Lun{3}", portNumber, pathId, targetId, lun);

                // find mounted disk using VDS
                Vds.Advanced.AdvancedDisk advancedDisk = null;
                Vds.Pack diskPack = null;

                // first attempt
                System.Threading.Thread.Sleep(3000);
                Console.WriteLine("Querying mounted disk...");
                FindVdsDisk(diskAddress, out advancedDisk, out diskPack);

                // second attempt
                if (advancedDisk == null)
                {
                    System.Threading.Thread.Sleep(20000);
                    Console.WriteLine("Querying mounted disk - second attempt...");
                    FindVdsDisk(diskAddress, out advancedDisk, out diskPack);
                }

                if (advancedDisk == null)
                {
                    throw new Exception("Could not find mounted disk");
                }

                List <string> volumes = new List <string>();

                Console.WriteLine("Disk flags: " + advancedDisk.Flags);

                // clear READONLY
                if ((advancedDisk.Flags & Vds.DiskFlags.ReadOnly) == Vds.DiskFlags.ReadOnly)
                {
                    Console.Write("Clearing disk Read-Only flag...");
                    advancedDisk.ClearFlags(Vds.DiskFlags.ReadOnly);

                    while ((advancedDisk.Flags & Vds.DiskFlags.ReadOnly) == Vds.DiskFlags.ReadOnly)
                    {
                        System.Threading.Thread.Sleep(100);
                        advancedDisk.Refresh();
                    }
                    Console.WriteLine("Done");
                }

                // bring disk ONLINE
                if (advancedDisk.Status == Vds.DiskStatus.Offline)
                {
                    Console.Write("Bringing disk online...");
                    advancedDisk.Online();

                    while (advancedDisk.Status == Vds.DiskStatus.Offline)
                    {
                        System.Threading.Thread.Sleep(100);
                        advancedDisk.Refresh();
                    }

                    Console.WriteLine("Done");
                }

                // determine disk index for DiskPart
                Wmi cimv2 = new Wmi(computer, "root\\CIMV2");
                ManagementObject objDisk = cimv2.GetWmiObject("win32_diskdrive",
                                                              "Model='Msft Virtual Disk SCSI Disk Device' and ScsiTargetID={0} and ScsiLogicalUnit={1} and scsiPort={2}",
                                                              targetId, lun, portNumber);

                if (objDisk != null)
                {
                    Console.WriteLine("DiskPart disk index: " + Convert.ToInt32(objDisk["Index"]));
                }

                // find volume
                diskPack.Refresh();
                foreach (Vds.Volume volume in diskPack.Volumes)
                {
                    volumes.Add(volume.DriveLetter.ToString());
                }

                if (volumes.Count == 0 && objDisk != null)
                {
                    // find volumes using WMI
                    foreach (ManagementObject objPartition in objDisk.GetRelated("Win32_DiskPartition"))
                    {
                        foreach (ManagementObject objVolume in objPartition.GetRelated("Win32_LogicalDisk"))
                        {
                            volumes.Add(objVolume["Name"].ToString().TrimEnd(':'));
                        }
                    }
                }

                foreach (string volume in volumes)
                {
                    Console.WriteLine("Volume found: " + volume);
                }

                //// find disk index
                //Wmi win32 = new Wmi(computer, @"root\cimv2");

                //System.Threading.Thread.Sleep(1000); // small pause

                //ManagementObject objDisk = win32.GetWmiObject("win32_DiskDrive",
                //    "Model='Msft Virtual Disk SCSI Disk Device' and ScsiTargetID={0} and ScsiLogicalUnit={1} and ScsiPort={2}",
                //    targetId, lun, portNumber);

                //int diskIndex = Convert.ToInt32(objDisk["Index"]);

                Console.WriteLine("\nDisk has been mounted.\n");
                //Console.WriteLine("Disk index: " + advancedDisk.);
            }
        }