📜 ⬆️ ⬇️

PowerShell + Hyper-V

I can not already through gui ...


Something like this was the beginning of my conversation with a friend who, for a short time, had to export virtual machines from Hyper-V several times in a row. Hyper-V Manager (HVM), which is installed with the Hyper-V role under Windows Server 2008 R2, is typically used for this. I must admit that the interface of this program does not cause me any negative emotions. Among all the management programs that Microsoft supplies to Server, this seems to me the most convenient and understandable (for example, I compare with the IIS Manager, which causes confusion among new users and violent indignation among those who used IIS 6 in 2003 Server). However, if you need to export or import a virtual machine in the number of N-pieces in a period of t-time, then using the Hyper-V Manager, you can break the button at the mouse and you will terribly hate Hyper-V. This is where PowerShell comes to the rescue.

Import-Module HyperV

... And of course there is no such module in PowerShell by default. Microsoft decided that no one would write cmdlets to manage Hyper-V (really a wild idea). On the other hand, this world is full of people who are able and willing to simplify life for themselves and other users. This is how the PowerShell management Library for Hyper-V was born.
The first thing you need to do is download this module + documentation to your liking. (Both are available at: http://pshyperv.codeplex.com/releases )
Then you need to install this module. The process is not complicated (the install file that is launched gives itself off).
Now you can work. Run PowerShell, import the module (what is written in the subtitle).
And we have access to all the functions that we could use via HVM, here are some of them:

Connect to a virtual machine
New-VMConnectSession

Manipulating the state of virtual machines
Get-VMState, Set-VMState, Convert-VmState,
Ping-VM, Test-VMHeartBeat, Shutdown-VM, Start-VM, Stop-VM, Suspend-VM
Get-VMKVP, Add-KVP, Remove-KVP, Get-VMJPEG
')
The ability to make backups, exporting virtual machines and taking snapshots
Export-VM, Import-VM, Get-VMSnapshot, Choose-VMSnapshot, Apply-VMSnapshot, New-VMSnapshot, Remove-VMSnapshot, Rename-VMSnapShot, Update-VMSnapshot, Get-VMSnapshotTree, Get-VmBackupScript

Adding and removing Wirth. machines, setting their properties
New-VM, Remove-VM, Set-VM, Get-VMCPUCount, Set-VMCPUCount, Get-VMMemory, Set-VMMemory, Set-VMSerialPort

Disk Controller Management
Get-VMDiskController
Add-VMSCSIController, Remove-VMSCSIcontroller
Get-VMDriveByController, Add-VMDRIVE, Remove-VMdrive
Get-VMDiskByDrive, Add-VMDISK, Set-VMDisk, Get-VMDisk
Get-VMFloppyDisk, Add-VMFloppyDisk
Add-VMNewHardDisk

Network Interface Management
Get-VMNic, List-VMNic, Choose-VMNIC, Add-VMNIC, Remove-VMNIC, Set-VMNICAddress, Set-VMNICConnection, Get-VMNicport,
Get-VMnicSwitch, Choose-VMSwitch, New-VMSwitchPort, Get-VMByMACaddress, Choose-VMExternalEthernet,
New-VMExternalSwitch, New-VMInternalSwitch, New-VmPrivateSwitch

Work with VHD files
Get-VHDDefaultPath, Get-VHDInfo, New-VHD, Compact-VHD, Test-VHD, Convert-VHD, Merge-VHD, Mount-VHD, Unmount-VHD

All the commands listed speak for themselves.
For example, to get the state of the virtual machine, just enter:
List-VMState .
and as a result:
image
In my example, there is only one virtual machine => I did not specify any parameters, but if you have a lot of them, then you should probably indicate its name explicitly, if you don’t want to then wade through the console output.
It is also easy, for example, to learn about the used network interfaces for virtual machines, just enter:
Get-VMNic
and we get:
image
Well and the most, that was necessary for my friend - an opportunity to do export in a background, according to the schedule. To do this, you can write a cmdlet which then stuff in Scheduler. For example, for me, this cmdlet stops a virtual machine, exports it, starts it again and sends me a letter about the error or success of the cmdlet execution. After that, control is transferred to another script that archives and sends a backup to a remote ftp.
Export: export-vm -vm MyVM1 -path D:\backups\VM -copystate -wait -force
The -copystate parameter says that you need to copy everything (including virtual disk => export size on the output can be very large)

Instead of conclusion

As you can see, managing your virtual machines from PowerShell can be very convenient and relaxed. Just read the parameters of the commands you need and write your own script.

PS I don’t pretend to “discover America” at all. I just decided that someone would find the information about this useful module useful. I have described only the most simple options for which you can use the PowerShell management library for Hyper-V. If someone knows or wrote their own more convenient tools to optimize the work with Hyper-V, I will be only too happy to learn about them.

Source: https://habr.com/ru/post/120575/


All Articles