Пример #1
0
        public async Task <IVolume> GetVolume()
        {
            Log.Debug("Getting volume of {Partition}", this);

            var results = await PowerShellMixin.ExecuteCommand("Get-Volume",
                                                               ("Partition", await this.GetPsPartition()));

            var result = results.FirstOrDefault()?.ImmediateBaseObject;

            if (result == null)
            {
                return(null);
            }

            var driveLetter = (char?)result.GetPropertyValue("DriveLetter");
            var vol         = new Volume(this)
            {
                Size  = new ByteSize(Convert.ToUInt64(result.GetPropertyValue("Size"))),
                Label = (string)result.GetPropertyValue("FileSystemLabel"),
                Root  = driveLetter != null?PathExtensions.GetRootPath(driveLetter.Value) : null,
                            FileSystemFormat = FileSystemFormat.FromString((string)result.GetPropertyValue("FileSystem")),
            };

            Log.Debug("Obtained {Volume}", vol);

            return(vol);
        }
Пример #2
0
        public async Task <IList <IPartition> > GetPartitions()
        {
            var results = await PowerShellMixin.ExecuteScript($"Get-Partition -DiskNumber {Number}");

            var wmiPartitions = results
                                .Select(x => x.ImmediateBaseObject)
                                .Select(ToWmiPartition);

            ReadOnlyCollection <Core.FileSystem.Gpt.Partition> gptPartitions;

            using (var context = await GptContextFactory.Create(Number, FileAccess.Read))
            {
                gptPartitions = context.Partitions;
            }

            var partitions = wmiPartitions
                             .Join(gptPartitions, x => x.Guid, x => x.Guid, (wmi, gpt) => (IPartition) new Partition(this)
            {
                Name          = gpt.Name,
                Root          = wmi.Root,
                Number        = wmi.Number,
                Guid          = wmi.Guid,
                PartitionType = wmi.PartitionType,
                UniqueId      = wmi.UniqueId,
                Size          = wmi.Size,
            });

            return(partitions.ToList());
        }
Пример #3
0
        public async Task SetGuid(Guid guid)
        {
            Log.Verbose("Changing disk Guid {Guid} to {Disk}", guid, this);
            var cmd = $@"Set-Disk -Number {Number} -Guid ""{{{guid}}}""";
            await PowerShellMixin.ExecuteScript(cmd);

            Log.Verbose("Disk Guid changed", guid, this);
        }
Пример #4
0
        public async Task <ICollection <DriverMetadata> > GetDrivers()
        {
            var results = await PowerShellMixin.ExecuteScript($"Get-WindowsDriver -Path {Root}");

            var disks = results
                        .Select(ToDriverMetadata);

            return(disks.ToList());
        }
Пример #5
0
        public async Task RemoveDriveLetter()
        {
            var part = await this.GetPsPartition();

            await PowerShellMixin.ExecuteCommand("Remove-PartitionAccessPath",
                                                 ("InputObject", part),
                                                 ("AccessPath", Root)
                                                 );
        }
Пример #6
0
        public async Task <IList <IDisk> > GetDisks()
        {
            var results = await PowerShellMixin.ExecuteScript("Get-Disk");

            var disks = results
                        .Select(x => x.ImmediateBaseObject)
                        .Select(ToDisk);

            return(disks.ToList());
        }
Пример #7
0
        public async Task Format(FileSystemFormat fileSystemFormat, string label)
        {
            var part = await this.Partition.GetPsPartition();

            await PowerShellMixin.ExecuteCommand("Format-Volume",
                                                 ("Partition", part),
                                                 ("Force", null),
                                                 ("Confirm", false),
                                                 ("FileSystem", fileSystemFormat.Moniker),
                                                 ("NewFileSystemLabel", label)
                                                 );
        }
Пример #8
0
        public static async Task <PSObject> GetPsPartition(this IPartition partition)
        {
            var psDataCollection = await PowerShellMixin.ExecuteScript($"Get-Partition -DiskNumber {partition.Disk.Number} -Number {partition.Number}");

            var psPartition = psDataCollection.FirstOrDefault();

            if (psPartition == null)
            {
                throw new ApplicationException($"Could not get PS Partition for {partition}");
            }

            return(psPartition);
        }
Пример #9
0
        public async Task <char> AssignDriveLetter()
        {
            var part = await this.GetPsPartition();

            var letter = GetFreeDriveLetter();
            await PowerShellMixin.ExecuteCommand("Set-Partition",
                                                 ("InputObject", part),
                                                 ("NewDriveLetter", letter)
                                                 );

            Root = PathExtensions.GetRootPath(letter);

            return(letter);
        }
Пример #10
0
        public async Task Resize(ByteSize size)
        {
            if (size.MegaBytes < 0)
            {
                throw new InvalidOperationException($"The partition size cannot be negative: {size}");
            }

            var sizeBytes = (ulong)size.Bytes;

            Log.Verbose("Resizing partition {Partition} to {Size}", this, size);

            var psPart = await this.GetPsPartition();

            await PowerShellMixin.ExecuteCommand("Resize-Partition", ("InputObject", psPart), ("Size", sizeBytes));
        }
Пример #11
0
        public async Task <IPartition> CreatePartition(ByteSize size, PartitionType partitionType, string name = "")
        {
            if (size.Equals(ByteSize.MaxValue))
            {
                await PowerShellMixin.ExecuteScript($"New-Partition -DiskNumber {Number} -UseMaximumSize");
            }
            else
            {
                await PowerShellMixin.ExecuteScript($"New-Partition -DiskNumber {Number} -Size {size.Bytes}");
            }

            var partitions = await GetPartitions();

            return(partitions.Last());
        }
Пример #12
0
        public async Task <IDisk> GetDisk(int n)
        {
            Log.Verbose("Getting disk by index {Id}", n);
            var results = await PowerShellMixin.ExecuteScript($"Get-Disk -Number {n}");

            var disks = results
                        .Select(x => x.ImmediateBaseObject)
                        .Select(ToDisk);

            var disk = disks.First();

            Log.Verbose("Returned disk {Disk}", disk);

            return(disk);
        }
Пример #13
0
        public async Task SetGptType(GptType gptType)
        {
            Log.Verbose("Setting new GPT partition type {Type} to {Partition}", gptType, this);

            if (Equals(GptType, gptType))
            {
                return;
            }

            var part = await this.GetPsPartition();

            await PowerShellMixin.ExecuteCommand("Set-Partition",
                                                 ("InputObject", part),
                                                 ("GptType", $"{{{gptType.Guid}}}")
                                                 );

            await Disk.Refresh();

            Log.Verbose("New GPT type set correctly", gptType, this);
        }
Пример #14
0
        public async Task <IList <IPartition> > GetPartitions()
        {
            var results = await PowerShellMixin.ExecuteScript($"Get-Partition -DiskNumber {Number}");

            var fromWmi = GetWmiPartitions(results);

            var fromGpt = await GetGptPartitions();

            IPartition FirstSelector(PartitionData wmi) =>
            new Partition(this)
            {
                Root     = wmi.Root,
                Number   = wmi.Number,
                Guid     = wmi.Guid,
                GptType  = wmi.GptType,
                UniqueId = wmi.UniqueId,
                Size     = wmi.Size,
            };

            IPartition BothSelector(PartitionData wmi, PartitionData gpt) =>
            new Partition(this)
            {
                Name     = gpt.Name,
                Root     = wmi.Root,
                Number   = wmi.Number,
                Guid     = wmi.Guid,
                GptType  = wmi.GptType,
                UniqueId = wmi.UniqueId,
                Size     = wmi.Size,
            };

            var partitions = MoreLinq.MoreEnumerable.LeftJoin(fromWmi, fromGpt,
                                                              pd => pd.Guid, FirstSelector, BothSelector);

            return(partitions.ToList());
        }
Пример #15
0
        public async Task Remove()
        {
            var part = await this.GetPsPartition();

            await PowerShellMixin.ExecuteCommand("Remove-Partition", ("InputObject", part), ("Confirm", false));
        }
Пример #16
0
 public async Task PrepareForRemoval()
 {
     var script = $"SELECT DISK {Number}\nOFFLINE DISK\nONLINE DISK";
     await PowerShellMixin.ExecuteScript($@"""{script}"" | & diskpart.exe");
 }
Пример #17
0
 public async Task ToggleOnline(bool isOnline)
 {
     await PowerShellMixin.ExecuteCommand("Set-Disk",
                                          ("Number", Number),
                                          ("IsOffline", !isOnline));
 }
Пример #18
0
 public async Task Refresh()
 {
     await PowerShellMixin.ExecuteScript($@"Update-HostStorageCache");
 }