Exemplo n.º 1
0
        private void resizeVhdFile(long FileSize)
        {
            try
            {
                ManagementScope scope = new ManagementScope(@"\root\virtualization\V2");

                long FullFileSize = FileSize + 512;


                using (ManagementObject imageManagementService =
                           StorageUtilities.GetImageManagementService(scope))
                {
                    createBackUp(this.LocalFilePath.FullName);
                    WriteVerbose("Resizing VHD file");
                    long sizeBefore = this.LocalFilePath.Length;
                    using (ManagementBaseObject inParams =
                               imageManagementService.GetMethodParameters("ResizeVirtualHardDisk"))
                    {
                        inParams["Path"]            = this.LocalFilePath.FullName;
                        inParams["MaxInternalSize"] = FileSize;
                        using (ManagementBaseObject outParams = imageManagementService.InvokeMethod(
                                   "ResizeVirtualHardDisk", inParams, null))
                        {
                            ManagementPath   path            = new ManagementPath((string)outParams["Job"]);
                            ManagementObject job             = new ManagementObject(path);
                            string           jobStatus       = (string)job["JobStatus"];
                            ushort           percentComplete = (ushort)job["PercentComplete"];
                            while (jobStatus == "Job is running" && percentComplete < 100)
                            {
                                Program.SyncOutput.ProgressHyperV(percentComplete, "Resizing VHD");
                                Thread.Sleep(1000);
                                job.Get();
                                jobStatus       = (string)job["JobStatus"];
                                percentComplete = (ushort)job["PercentComplete"];
                            }
                            Program.SyncOutput.ProgressHyperV(percentComplete, "Resizing VHD");
                            WmiUtilities.ValidateOutput(outParams, scope);
                        }
                    }
                    WriteVerbose("Resized " + this.LocalFilePath + " from " + sizeBefore + " bytes to " + FullFileSize + " bytes.");
                    this.LocalFilePath   = new FileInfo(this.LocalFilePath.FullName);
                    temporaryFileCreated = true;
                    FixedSize            = FullFileSize;
                }
            }
            catch (System.Management.ManagementException ex)
            {
                if (ex.Message == "Invalid namespace ")
                {
                    Exception outputEx = new Exception("Failed to resize VHD file. Hyper-V Platform is not found.\nFollow this link to enable Hyper-V or resize file manually: https://aka.ms/usingAdd-AzVhd");
                    ThrowTerminatingError(new ErrorRecord(
                                              outputEx,
                                              "Hyper-V is unavailable",
                                              ErrorCategory.InvalidOperation,
                                              null));
                }
                else
                {
                    throw ex;
                }
            }
        }
Exemplo n.º 2
0
        private FileInfo ConvertVhd()
        {
            CheckOS();
            WriteWarning("The VHDX file needs to be converted to VHD. During the conversion process, the cmdlet will temporarily create a resized file in the same directory as the provided VHDX file.");

            string                     ConvertedPath = ReturnAvailExtensionName(vhdFileToBeUploaded.FullName, "_converted", ".vhd");
            FileInfo                   vhdFileInfo   = new FileInfo(ConvertedPath);
            ManagementScope            scope         = new ManagementScope(@"\root\virtualization\V2");
            VirtualHardDiskSettingData settingData   = new VirtualHardDiskSettingData(VirtualHardDiskType.DynamicallyExpanding, VirtualHardDiskFormat.Vhd, vhdFileInfo.FullName, null, 0, 0, 0, 0);

            try
            {
                using (ManagementObject imageManagementService =
                           StorageUtilities.GetImageManagementService(scope))
                {
                    WriteVerbose("Converting VHDX file to VHD file.");
                    using (ManagementBaseObject inParams =
                               imageManagementService.GetMethodParameters("ConvertVirtualHardDisk"))
                    {
                        inParams["SourcePath"]             = vhdFileToBeUploaded.FullName;
                        inParams["VirtualDiskSettingData"] =
                            settingData.GetVirtualHardDiskSettingDataEmbeddedInstance(null, imageManagementService.Path.Path);

                        using (ManagementBaseObject outParams = imageManagementService.InvokeMethod(
                                   "ConvertVirtualHardDisk", inParams, null))
                        {
                            ManagementPath   path            = new ManagementPath((string)outParams["Job"]);
                            ManagementObject job             = new ManagementObject(path);
                            string           jobStatus       = (string)job["JobStatus"];
                            ushort           percentComplete = (ushort)job["PercentComplete"];
                            while (jobStatus == "Job is running" && percentComplete < 100)
                            {
                                Program.SyncOutput.ProgressHyperV(percentComplete, "Converting to VHD");
                                Thread.Sleep(1000);
                                job.Get();
                                jobStatus       = (string)job["JobStatus"];
                                percentComplete = (ushort)job["PercentComplete"];
                            }
                            Program.SyncOutput.ProgressHyperV(percentComplete, "Converting to VHD");

                            WmiUtilities.ValidateOutput(outParams, scope);
                        }
                    }
                    temporaryFileCreated = true;
                    WriteVerbose("Converted file: " + ConvertedPath);
                    return(new FileInfo(vhdFileInfo.FullName));
                }
            }
            catch (System.Management.ManagementException ex)
            {
                if (ex.Message == "Invalid namespace ")
                {
                    Exception outputEx = new Exception("Failed to convert VHDX file. Hyper-V Platform is not found.\nFollow this link to enable Hyper-V or convert file manually: https://aka.ms/usingAdd-AzVhd");
                    ThrowTerminatingError(new ErrorRecord(
                                              outputEx,
                                              "Hyper-V is unavailable",
                                              ErrorCategory.InvalidOperation,
                                              null));
                }
                throw ex;
            }
        }
        private void convertDynamicVhdToStatic()
        {
            string                     FixedSizePath = returnAvailExtensionName(this.LocalFilePath.FullName, "_FixedSize", ".vhd");
            FileInfo                   FixedFileInfo = new FileInfo(FixedSizePath);
            ManagementScope            scope         = new ManagementScope(@"\root\virtualization\V2");
            VirtualHardDiskSettingData settingData   = new VirtualHardDiskSettingData(VirtualHardDiskType.FixedSize, VirtualHardDiskFormat.Vhd, FixedFileInfo.FullName, null, 0, 0, 0, 0);

            try
            {
                using (ManagementObject imageManagementService =
                           StorageUtilities.GetImageManagementService(scope))
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Converting dynamically sized VHD to fixed size VHD.");
                    Console.ResetColor();

                    using (ManagementBaseObject inParams =
                               imageManagementService.GetMethodParameters("ConvertVirtualHardDisk"))
                    {
                        inParams["SourcePath"]             = this.LocalFilePath.FullName;
                        inParams["VirtualDiskSettingData"] =
                            settingData.GetVirtualHardDiskSettingDataEmbeddedInstance(null, imageManagementService.Path.Path);

                        using (ManagementBaseObject outParams = imageManagementService.InvokeMethod(
                                   "ConvertVirtualHardDisk", inParams, null))
                        {
                            ManagementPath   path            = new ManagementPath((string)outParams["Job"]);
                            ManagementObject job             = new ManagementObject(path);
                            string           jobStatus       = (string)job["JobStatus"];
                            ushort           percentComplete = (ushort)job["PercentComplete"];
                            while (jobStatus == "Job is running" && percentComplete < 100)
                            {
                                Program.SyncOutput.ProgressHyperV(percentComplete, "Converting dynamically sized VHD to fixed size VHD");
                                Thread.Sleep(1000);
                                job.Get();
                                jobStatus       = (string)job["JobStatus"];
                                percentComplete = (ushort)job["PercentComplete"];
                            }
                            Program.SyncOutput.ProgressHyperV(percentComplete, "Converting dynamically sized VHD to fixed size VHD");
                            WmiUtilities.ValidateOutput(outParams, scope);
                        }
                        Console.WriteLine("Fixed VHD file: " + FixedSizePath);
                        this.LocalFilePath = FixedFileInfo;
                    }
                }
            }
            catch (System.Management.ManagementException ex)
            {
                if (ex.Message == "Invalid namespace ")
                {
                    Exception outputEx = new Exception("Failed to convert VHD file to fixed size. Hyper-V Platform is not found.\nFollow this link to enabled Hyper-V or convert file manually: https://aka.ms/usingAdd-AzVhd");
                    ThrowTerminatingError(new ErrorRecord(
                                              outputEx,
                                              "Hyper-V is unavailable",
                                              ErrorCategory.InvalidOperation,
                                              null));
                }
                else
                {
                    throw ex;
                }
            }
        }