private static Either <PowershellFailure, TypedPsObject <VirtualMachineInfo> > EjectConfigDriveDisk( string configDriveIsoPath, TypedPsObject <VirtualMachineInfo> vmInfo, IPowershellEngine engine) { var res = vmInfo.GetList(l => l.DVDDrives, drive => configDriveIsoPath.Equals(drive.Path, StringComparison.OrdinalIgnoreCase)) .Apply(dvdDriveList => { var array = dvdDriveList.ToArray(); if (array.Length > 1) { array.Take(array.Length - 1).Iter(r => engine.Run(PsCommandBuilder.Create() .AddCommand("Remove-VMDvdDrive") .AddParameter("VMDvdDrive", r.PsObject))); } return(vmInfo.GetList(l => l.DVDDrives, drive => configDriveIsoPath.Equals(drive.Path, StringComparison.OrdinalIgnoreCase))); }).Apply(driveInfos => { return(driveInfos.Map(driveInfo => engine.Run(PsCommandBuilder.Create() .AddCommand("Set-VMDvdDrive") .AddParameter("VMDvdDrive", driveInfo.PsObject) .AddParameter("Path", null))) .Traverse(x => x).Map( x => vmInfo.Recreate())); }); return(res); }
public static async Task <Either <PowershellFailure, TypedPsObject <VirtualMachineInfo> > > Disk( VirtualMachineDiskConfig diskConfig, IPowershellEngine engine, TypedPsObject <VirtualMachineInfo> vmInfo, MachineConfig vmConfig, Func <string, Task> reportProgress) { if (!File.Exists(diskConfig.Path)) { await reportProgress($"Create VHD: {diskConfig.Name}").ConfigureAwait(false); await engine.RunAsync(PsCommandBuilder.Create().Script( $"New-VHD -Path \"{diskConfig.Path}\" -ParentPath \"{diskConfig.Template}\" -Differencing"), reportProgress : p => ReportPowershellProgress($"Create VHD {diskConfig.Name}", p, reportProgress)).ConfigureAwait(false); } await GetOrCreateInfoAsync(vmInfo, i => i.HardDrives, disk => diskConfig.Path.Equals(disk.Path, StringComparison.OrdinalIgnoreCase), async() => { await reportProgress($"Add VHD: {diskConfig.Name}").ConfigureAwait(false); return(await engine.GetObjectsAsync <HardDiskDriveInfo>(PsCommandBuilder.Create() .AddCommand("Add-VMHardDiskDrive") .AddParameter("VM", vmInfo.PsObject) .AddParameter("Path", diskConfig.Path)).ConfigureAwait(false)); }).ConfigureAwait(false); return(vmInfo.Recreate()); }
public static Task <Either <PowershellFailure, TypedPsObject <VirtualMachineInfo> > > RenameVirtualMachine( IPowershellEngine engine, TypedPsObject <VirtualMachineInfo> vmInfo, string newName) { return(engine.RunAsync(PsCommandBuilder.Create() .AddCommand("Rename-VM") .AddParameter("VM", vmInfo.PsObject) .AddParameter("NewName", newName) ).MapAsync(u => vmInfo.Recreate())); }
public static async Task <Either <PowershellFailure, TypedPsObject <VirtualMachineInfo> > > NetworkAdapter( VirtualMachineNetworkAdapterConfig networkAdapterConfig, IPowershellEngine engine, TypedPsObject <VirtualMachineInfo> vmInfo, MachineConfig machineConfig, Func <string, Task> reportProgress) { var optionalAdapter = await GetOrCreateInfoAsync(vmInfo, i => i.NetworkAdapters, adapter => networkAdapterConfig.Name.Equals(adapter.Name, StringComparison.OrdinalIgnoreCase), async() => { await reportProgress($"Add Network Adapter: {networkAdapterConfig.Name}").ConfigureAwait(false); return(await engine.GetObjectsAsync <VMNetworkAdapter>(PsCommandBuilder.Create() .AddCommand("Add-VmNetworkAdapter") .AddParameter("Passthru") .AddParameter("VM", vmInfo.PsObject) .AddParameter("Name", networkAdapterConfig.Name) .AddParameter("StaticMacAddress", UseOrGenerateMacAddress(networkAdapterConfig, vmInfo)) .AddParameter("SwitchName", networkAdapterConfig.SwitchName)).ConfigureAwait(false)); }).ConfigureAwait(false); return(optionalAdapter.Map(_ => vmInfo.Recreate())); //optionalAdapter.Map(async (adapter) => //{ // if (!adapter.Value.Connected || adapter.Value.SwitchName != networkConfig.SwitchName) // { // await reportProgress($"Connect Network Adapter {adapter.Value.Name} to switch {networkConfig.SwitchName}").ConfigureAwait(false); // await engine.RunAsync( // PsCommandBuilder.Create().AddCommand("Connect-VmNetworkAdapter") // .AddParameter("VMNetworkAdapter", adapter.PsObject) // .AddParameter("SwitchName", networkConfig.SwitchName)).ConfigureAwait(false); // } //}); return(vmInfo.Recreate()); }