public void SetCopyGuestFile(DataDef.SetCopyVMGuestFile copyFileInfo) { try { //var PSScript = string.Format("Copy-VMGuestFile -Source {} -Destination ""{copyFileInfo.Destination}"" -LocalToGuest -VM ""{copyFileInfo.VMName}"" -GuestUser {copyFileInfo.GuestUser} -GuestPassword '{copyFileInfo.GuestPassword}' -Force"); using (PowerCli powerCli = new PowerCli()) { Commands.Add($@"Copy-VMGuestFile -Source {copyFileInfo.Source} -Destination {copyFileInfo.Destination} -LocalToGuest -VM {copyFileInfo.VMName} -GuestUser {copyFileInfo.GuestUser} -GuestPassword {copyFileInfo.GuestPassword} -Force"); powerCli.Run(Commands); } } catch (Exception) { throw; } }
public void SetResource(List <DataDef.SetResourceChange> resourceChangesList) { try { using (PowerCli powerCli = new PowerCli()) { for (int i = 0; resourceChangesList != null && i < resourceChangesList?.Count; i++) { if (string.IsNullOrEmpty(resourceChangesList?[i].VMName)) { continue; } Commands.Add($@"$HotAddVMs = Get-Datacenter | Get-VM -Name {resourceChangesList?[i].VMName} | Select Name, @{{N=""CpuHotAddEnabled"";E={{$_.ExtensionData.Config.CpuHotAddEnabled}}}}, @{{N=""MemoryHotAddEnabled"";E={{$_.ExtensionData.Config.MemoryHotAddEnabled}}}}"); string Command_VMHotAddEnabled = string.Empty; Command_VMHotAddEnabled += $@" foreach($HotAddVM in $HotAddVMs) {{ #if ($HotAddVM.CpuHotAddEnabled -Eq $False -Or $HotAddVM.MemoryHotAddEnabled -Eq $False) {{ #VM 전원 강제 종료함 (HotAddEnabled가 적용안되어 있는것. 본사에서는 HotAddEbaled 속성이 되어 있어도 즉시 반영이 되지 않아 일단 종료함) Stop-VM -VM $HotAddVM.Name -Confirm:$False }} }} #본사에서는 강제 종료함 Stop-VM -VM {string.Join(", ", resourceChangesList?.Where(x => !string.IsNullOrEmpty(x.VMName)).ToList())}"; Commands.Add($@"{Command_VMHotAddEnabled}"); } { //본사에서는 강제 종료하여 10초 기다림 //Wait 10 Commands.Add($@"Start-Sleep -s 10"); } for (int i = 0; resourceChangesList != null && i < resourceChangesList?.Count; i++) { string AppendCommand = string.Empty; AppendCommand += resourceChangesList?[i].vCPU % 2 == 0 ? $@" -CoresPerSocket 2" : string.Empty; AppendCommand += resourceChangesList?[i].vCPU >= 1 ? $@" -NumCpu {resourceChangesList?[i].vCPU}" : string.Empty; AppendCommand += resourceChangesList?[i].MemoryMB >= 1024 ? $@" -MemoryMB {resourceChangesList?[i].MemoryMB}" : string.Empty; Commands.Add($@"Get-VM -Name {resourceChangesList?[i].VMName} | Set-VM {AppendCommand} -Confirm:$False"); for (int j = 0; j < resourceChangesList?[i].Disks.Count; j++) { if (!string.IsNullOrEmpty(resourceChangesList?[i].Disks[j].DiskName) && resourceChangesList?[i].Disks[j]?.DiskAction == DataDef.SetResourceChange.Disk.DiskActionKind.Change) { Commands.Add($@"$hdd_{i}_{j} = Get-VM -Name {resourceChangesList?[i].VMName} | Get-HardDisk -Name '{resourceChangesList?[i]?.Disks[j].DiskName}'"); Commands.Add($@"Set-HardDisk -HardDisk $hdd_{i}_{j} -CapacityKB {resourceChangesList?[i]?.Disks[j]?.DiskCapacityKB} -Confirm:$False"); } else if (resourceChangesList?[i].Disks[j]?.DiskAction == DataDef.SetResourceChange.Disk.DiskActionKind.Add) { Commands.Add($@"Get-VM -Name {resourceChangesList?[i].VMName} | New-HardDisk -CapacityKB {resourceChangesList?[i]?.Disks[j]?.DiskCapacityKB} -Persistence persistent"); } else if (resourceChangesList?[i].Disks[j]?.DiskAction == DataDef.SetResourceChange.Disk.DiskActionKind.Delete) { Commands.Add($@"Get-VM -Name {resourceChangesList?[i].VMName} | Get-HardDisk -Name '{resourceChangesList?[i]?.Disks[j].DiskName}' | Remove-HardDisk -Confirm:$False"); } } } powerCli.Run(Commands); } } catch { throw; } }
public void SetPower(List <string> VMLists, PowerActionKind powerActionKind) { try { using (PowerCli powerCli = new PowerCli()) { string VMs = string.Join(", ", VMLists?.Where(x => !string.IsNullOrEmpty(x)).ToList()); switch (true) { case bool _ when powerActionKind == PowerActionKind.PowerOn: { Commands.Add($@"Start-VM -VM {VMs}"); break; } case bool _ when powerActionKind == PowerActionKind.ForceShutdown: { Commands.Add($@"Stop-VM -VM {VMs} -Confirm:$False"); break; } case bool _ when powerActionKind == PowerActionKind.ForceReboot: { Commands.Add($@"Restart-VM -VM {VMs} -Confirm:$False"); break; } case bool _ when powerActionKind == PowerActionKind.ForceSuspend: { Commands.Add($@"Suspend-VM -VM {VMs} -Confirm:$False"); break; } case bool _ when powerActionKind == PowerActionKind.GuestReboot: { Commands.Add($@"Restart-VMGuest -VM {VMs} -Confirm:$False"); break; } case bool _ when powerActionKind == PowerActionKind.GuestShutdown: { Commands.Add($@"Shutdown-VMGuest -VM {VMs} -Confirm:$False"); break; } case bool _ when powerActionKind == PowerActionKind.GuestStandby: { Commands.Add($@"Suspend-VMGuest -VM {VMs} -Confirm:$False"); break; } } powerCli.Run(Commands); } } catch { throw; } }